(params: { arr: any[]; maxSize: number })
| 156 | } |
| 157 | |
| 158 | function splitArray(params: { arr: any[]; maxSize: number }): Chunk<any[]>[] { |
| 159 | const { arr, maxSize } = params |
| 160 | const chunks: Chunk<any[]>[] = [] |
| 161 | let currentChunk: Chunk<any[]> = { data: [], length: 2 } |
| 162 | |
| 163 | for (const element of arr) { |
| 164 | const entryArr = [element] |
| 165 | const standaloneEntry: Chunk<any[]> = { |
| 166 | data: entryArr, |
| 167 | length: getJsonSize(entryArr), |
| 168 | } |
| 169 | |
| 170 | if (standaloneEntry.length > maxSize) { |
| 171 | if (currentChunk.length > 2) { |
| 172 | chunks.push(currentChunk) |
| 173 | } |
| 174 | |
| 175 | const items = splitDataWithLengths({ |
| 176 | data: element, |
| 177 | maxChunkSize: maxSize - 2, |
| 178 | }) |
| 179 | |
| 180 | for (const [index, item] of items.entries()) { |
| 181 | if (index < items.length - 1) { |
| 182 | // Try to add to current chunk |
| 183 | const candidateChunkLength = |
| 184 | currentChunk.length + |
| 185 | item.length + |
| 186 | (currentChunk.length === 2 ? 1 : 0) |
| 187 | if (candidateChunkLength <= maxSize) { |
| 188 | currentChunk.data.push(item.data) |
| 189 | currentChunk.length = candidateChunkLength |
| 190 | continue |
| 191 | } |
| 192 | |
| 193 | chunks.push({ data: [item.data], length: item.length + 2 }) |
| 194 | continue |
| 195 | } |
| 196 | |
| 197 | currentChunk = { data: [item.data], length: item.length + 2 } |
| 198 | } |
| 199 | continue |
| 200 | } |
| 201 | |
| 202 | const candidateChunkLength = |
| 203 | currentChunk.length + |
| 204 | standaloneEntry.length - |
| 205 | (currentChunk.length === 2 ? 1 : 2) |
| 206 | |
| 207 | if (candidateChunkLength <= maxSize) { |
| 208 | currentChunk.data.push(element) |
| 209 | currentChunk.length = candidateChunkLength |
| 210 | continue |
| 211 | } |
| 212 | |
| 213 | if (currentChunk.length > 2) { |
| 214 | chunks.push(currentChunk) |
| 215 | currentChunk = standaloneEntry |
no test coverage detected