(chunks: Chunk[])
| 292 | } |
| 293 | |
| 294 | export function jsonReconstruct(chunks: Chunk[]): Record<string, any> { |
| 295 | /** |
| 296 | Takes Chunks outputted by jsonSplitter and reconstructs the original JSON. |
| 297 | So jsonReconstruct(jsonSplitter(anyJson)) === anyJson |
| 298 | **/ |
| 299 | |
| 300 | // Sort by path length (depth) ascending |
| 301 | chunks.sort((a, b) => a.path.length - b.path.length); |
| 302 | |
| 303 | let root: Record<string, any> = {}; |
| 304 | |
| 305 | for (let chunk of chunks) { |
| 306 | let layer = root; |
| 307 | for (let i = 0; i < chunk.path.length; i++) { |
| 308 | let key = chunk.path[i]; |
| 309 | if (i === chunk.path.length - 1) { |
| 310 | layer[key] = chunk.data; |
| 311 | } else if (layer[key] === undefined) { |
| 312 | layer[key] = isNaN(Number(chunk.path[i + 1])) ? {} : []; |
| 313 | } |
| 314 | layer = layer[key]; |
| 315 | } |
| 316 | } |
| 317 | return root; |
| 318 | } |
| 319 | |
| 320 | export function chunksToProperties(chunks: Chunk[]): Properties { |
| 321 | /** |
no outgoing calls
no test coverage detected