( json: any, path: (string | number)[] = [], )
| 263 | } |
| 264 | |
| 265 | export function jsonSplitter( |
| 266 | json: any, |
| 267 | path: (string | number)[] = [], |
| 268 | ): Chunk[] { |
| 269 | /** |
| 270 | Breaks down JSON into individual chunks of data. Each "Chunk" is defined by its path |
| 271 | (i.e. where it is positioned in the json). And the data itself. |
| 272 | E.g. The chunk for the field 'b' in the json {a: {b: 1}} => [{path: ["a", "b"], data: 1} |
| 273 | **/ |
| 274 | |
| 275 | if (Array.isArray(json)) { |
| 276 | let chunks: Chunk[] = []; |
| 277 | if (json.length === 0) chunks.push({ path, data: [] }); |
| 278 | for (let i = 0; i < json.length; i++) { |
| 279 | chunks.push(...jsonSplitter(json[i], [...path, i])); |
| 280 | } |
| 281 | return chunks.map((chunk) => ({ ...chunk, dataType: "Array" })); |
| 282 | } else if (typeof json === "object" && json !== null) { |
| 283 | let chunks: Chunk[] = []; |
| 284 | if (Object.keys(json).length === 0) chunks.push({ path, data: {} }); |
| 285 | for (let key in json) { |
| 286 | chunks.push(...jsonSplitter(json[key], [...path, key])); |
| 287 | } |
| 288 | return chunks; |
| 289 | } else { |
| 290 | return [{ path, data: json }]; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | export function jsonReconstruct(chunks: Chunk[]): Record<string, any> { |
| 295 | /** |
no outgoing calls
no test coverage detected