remapHashes

tcmts.cli.migrate.remapHashes · #zpmtm4gc · term · runs anywhere

A value with every hash in it replaced as map says (h or #h, wherever it is in it); the rest as it was.

export function remapHashes(v: unknown, map: ReadonlyMap<string, string>): unknown {
  if (typeof v === "string")
    return map.get(v) ?? (v.startsWith("#") && map.has(v.slice(1)) ? `#${map.get(v.slice(1))}` : v);
  if (Array.isArray(v)) return v.map((x) => remapHashes(x, map));
  if (v && typeof v === "object")
    return Object.fromEntries(Object.entries(v).map(([k, x]) => [k, remapHashes(x, map)]));
  return v;
}

used by