(json: Json | Json[])
| 24 | let idIdx = Object.entries(store).length; |
| 25 | |
| 26 | function findAndReplaceID(json: Json | Json[]) { |
| 27 | if (!json || typeof json !== "object") return; |
| 28 | |
| 29 | // Creates the same iterator for both arrays and objects |
| 30 | const entries = Array.isArray(json) |
| 31 | ? json.entries() |
| 32 | : Object.entries(json as { [key: string]: Json }); |
| 33 | |
| 34 | for (const [key, value] of entries) { |
| 35 | const k = key as string; |
| 36 | if (typeof value === "object") { |
| 37 | findAndReplaceID(value); |
| 38 | } else if (typeof value === "string") { |
| 39 | if (value.includes("/")) { |
| 40 | // If string is a path, operate on individual segments |
| 41 | const urlParts = value |
| 42 | .split("/") |
| 43 | .map((part) => (isID(part) ? getOrGenerateID(part) : part)); |
| 44 | (json as any)[k] = urlParts.join("/"); |
| 45 | } else if (isID(value)) { |
| 46 | (json as any)[k] = getOrGenerateID(value); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | function getOrGenerateID(value: string): string { |
| 53 | let id = store[value]; |
no test coverage detected