{"version":3,"file":"id-DbD7K-HL.mjs","names":["stringify","stringify"],"sources":["../src/id/hashSource.ts","../src/id/hashTemplate.ts"],"sourcesContent":["// Functions provided to other GT libraries\n\nimport { JsxChild, JsxChildren, Variable } from '../types';\nimport { stableStringify as stringify } from '../utils/stableStringify';\nimport { sha256 } from '@noble/hashes/sha2.js';\nimport { bytesToHex, utf8ToBytes } from '@noble/hashes/utils.js';\nimport isVariable from '../utils/isVariable';\nimport { HashMetadata } from './types';\n\n// ----- FUNCTIONS ----- //\n/**\n * Calculates a unique hash for a given string using SHA-256.\n *\n * First 16 characters of hash, hex encoded.\n *\n * @param {string} string - The string to be hashed.\n * @returns {string} The resulting hash as a hexadecimal string.\n */\nexport function hashString(string: string): string {\n  return bytesToHex(sha256(utf8ToBytes(string))).slice(0, 16);\n}\n\n/**\n * Calculates a unique ID for the given children objects by hashing their sanitized JSON string representation.\n *\n * @param {any} childrenAsObjects - The children objects to be hashed.\n * @param {string} [context] - The context for the children.\n * @param {string} [id] - The ID for the JSX children object.\n * @param {number} [maxChars] - The maxChars limit for the JSX children object.\n * @param {string} [dataFormat] - The data format of the sources.\n * @param {function} [hashFunction] - Custom hash function.\n * @returns {string} - The unique hash of the children.\n */\nexport function hashSource(\n  {\n    source,\n    context,\n    id,\n    maxChars,\n    dataFormat,\n  }: {\n    source: JsxChildren | string;\n  } & HashMetadata,\n  hashFunction: (string: string) => string = hashString\n): string {\n  let sanitizedSource: SanitizedChildren | string;\n  if (dataFormat === 'JSX') {\n    sanitizedSource = sanitizeJsxChildren(source);\n  } else {\n    sanitizedSource = source as string;\n  }\n  const sanitizedData: {\n    source?: SanitizedChildren;\n  } & HashMetadata = {\n    source: sanitizedSource,\n    ...(id && { id }),\n    ...(context && { context }),\n    ...(maxChars != null && { maxChars: Math.abs(maxChars) }),\n    ...(dataFormat && { dataFormat }),\n  };\n  const stringifiedData = stringify(sanitizedData);\n  return hashFunction(stringifiedData);\n}\n\ntype SanitizedVariable = Omit<Variable, 'i'>;\n\ntype SanitizedElement = {\n  b?: {\n    [k: string]: SanitizedChildren; // Branches\n  };\n  c?: SanitizedChildren; // Children\n  t?: string; // Branch Transformation\n};\ntype SanitizedChild = SanitizedElement | SanitizedVariable | string;\ntype SanitizedChildren = SanitizedChild | SanitizedChild[];\n\n/**\n * Sanitizes a child object by removing the data-_gt attribute and its branches.\n *\n * @param child - The child object to sanitize.\n * @returns The sanitized child object.\n *\n */\nconst sanitizeChild = (child: JsxChild): SanitizedChild => {\n  if (child && typeof child === 'object') {\n    const newChild: SanitizedChild = {};\n    if ('c' in child && child.c) {\n      newChild.c = sanitizeJsxChildren(child.c);\n    }\n    if ('d' in child) {\n      const generaltranslation = child?.d;\n      if (generaltranslation?.b) {\n        // The only thing that prevents sanitizeJsx from being stable is\n        // the order of the keys in the branches object.\n        // We don't sort them because stable-stringify sorts them anyways\n        newChild.b = Object.fromEntries(\n          Object.entries(generaltranslation.b).map(([key, value]) => [\n            key,\n            sanitizeJsxChildren(value as JsxChildren),\n          ])\n        );\n      }\n      if (generaltranslation?.t) {\n        newChild.t = generaltranslation.t;\n      }\n    }\n    if (isVariable(child)) {\n      return {\n        k: child.k,\n        ...(child.v && {\n          v: child.v,\n        }),\n      };\n    }\n    return newChild;\n  }\n  return child;\n};\n\nfunction sanitizeJsxChildren(\n  childrenAsObjects: JsxChildren\n): SanitizedChildren {\n  return Array.isArray(childrenAsObjects)\n    ? childrenAsObjects.map(sanitizeChild)\n    : sanitizeChild(childrenAsObjects);\n}\n","import { hashString } from './hashSource';\nimport { stableStringify as stringify } from '../utils/stableStringify';\n\nexport default function hashTemplate(\n  template: {\n    [key: string]: string;\n  },\n  hashFunction = hashString\n): string {\n  return hashFunction(stringify(template));\n}\n"],"mappings":";;;;;;;;;;;;AAkBA,SAAgB,WAAW,QAAwB;AACjD,QAAO,WAAW,OAAO,YAAY,OAAO,CAAC,CAAC,CAAC,MAAM,GAAG,GAAG;;;;;;;;;;;;;AAc7D,SAAgB,WACd,EACE,QACA,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