(params: {
data: string
maxSize: number
})
| 22 | } |
| 23 | |
| 24 | function splitString(params: { |
| 25 | data: string |
| 26 | maxSize: number |
| 27 | }): Chunk<string>[] { |
| 28 | const { data, maxSize } = params |
| 29 | if (data === '') { |
| 30 | return [{ data: '', length: 2 }] |
| 31 | } |
| 32 | |
| 33 | const chunks: Chunk<string>[] = [] |
| 34 | let currentChunk: Chunk<string> = { data: '', length: 2 } |
| 35 | |
| 36 | if (maxSize < 2) { |
| 37 | for (let i = 0; i < data.length; i++) { |
| 38 | chunks.push({ data: data[i], length: getJsonSize(data[i]) }) |
| 39 | } |
| 40 | return chunks |
| 41 | } |
| 42 | |
| 43 | for (let i = 0; i < data.length; i++) { |
| 44 | const char = data[i] |
| 45 | const charSizeContribution = JSON.stringify(char).length - 2 |
| 46 | let potentialNextSize: number |
| 47 | |
| 48 | potentialNextSize = currentChunk.length + charSizeContribution |
| 49 | |
| 50 | if (potentialNextSize <= maxSize) { |
| 51 | currentChunk.data += char |
| 52 | currentChunk.length = potentialNextSize |
| 53 | } else { |
| 54 | if (currentChunk.data !== '') { |
| 55 | chunks.push(currentChunk) |
| 56 | } |
| 57 | |
| 58 | currentChunk = { data: char, length: 2 + charSizeContribution } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if (currentChunk.data !== '') { |
| 63 | chunks.push(currentChunk) |
| 64 | } |
| 65 | |
| 66 | return chunks |
| 67 | } |
| 68 | |
| 69 | function splitObject(params: { |
| 70 | obj: PlainObject |
no test coverage detected