{"version":3,"file":"canonicalize.cjs","sources":["../../../src/rdf-and-cd/canonicalize.js"],"sourcesContent":["// The js interface to rify accepts RDF nodes as strings.\n// This module deals with the conversions between the js representation\n// of RDF nodes ({ Iri: 'https://example.com' }) and the rify-js representation\n// AKA strings.\n\nimport { assert } from '../utils/assert';\nimport { assertType, assertValidNode } from './common';\n\n// Convert a Node into a cononicalized string representation.\n//\n// ∀ A, B ∈ Node: canon(A) = canon(B) <-> A = B\nexport function canon(node) {\n  assertValidNode(node);\n  return JSON.stringify(orderKeys(node));\n}\n\n/// Canonicalize all the nodes in a ruleset.\nexport function canonRules(rule) {\n  return rule.map(({ if_all: ifAll, then }) => ({\n    if_all: ifAll.map((claim) => claim.map(canonAtom)),\n    then: then.map((claim) => claim.map(canonAtom)),\n  }));\n}\n\n/// Canonicalize a rule atom.\n/// An atom may be either { Bound: Node } or { Unbound: 'string' }\n// expect(canonAtom({ Bound: { Iri: 'https://example.com' } }))\n//   .toEqual({ Bound: \"{\\\"Iri\\\":\\\"https://example.com\\\"}\" });\n// expect(canonAtom({ Unbound: \"heyo\" }))\n//   .toEqual({ Unbound: \"heyo\" });\nfunction canonAtom(atom) {\n  assert(Object.keys(atom).length === 1, 'enum must have exactly one tag');\n  switch (Object.keys(atom)[0]) {\n    case 'Bound':\n      assertType(atom.Bound, 'object');\n      return { Bound: canon(atom.Bound) };\n    case 'Unbound':\n      assertType(atom.Unbound, 'string');\n      return { Unbound: atom.Unbound };\n    default:\n      throw new TypeError(`expected bound or unbound rule atom got ${atom}`);\n  }\n}\n\n/// Canonicalize all the nodes in a proof.\nexport function canonProof(proof) {\n  return proof.map(({ rule_index: ruleIndex, instantiations }) => ({\n    rule_index: ruleIndex,\n    instantiations: instantiations.map(canon),\n  }));\n}\n\n/// Parse all the nodes in a conaonicalized proof.\nexport function decanonProof(proof) {\n  return proof.map(({ rule_index: ruleIndex, instantiations }) => ({\n    rule_index: ruleIndex,\n    instantiations: instantiations.map(JSON.parse),\n  }));\n}\n\n/// Canonicalize all the nodes in a claimgraph.\nexport function canonClaimGraph(cg) {\n  return cg.map((claim) => claim.map(canon));\n}\n\n/// Parse all the nodes in a canonicalized claimgraph.\nexport function decanonClaimGraph(cg) {\n  return cg.map((claim) => claim.map(JSON.parse));\n}\n\n// recursively lexically sort the keys in an object\n// expect(JSON.stringify(orderKeys(\n//   { b: '', a: '' }\n// ))).toEqual(JSON.stringify(\n//   { a: '', b: '' }\n// ));\n// expect(JSON.stringify(orderKeys(\n//   { b: '', a: { c: '', b: '', a: '' } }\n// ))).toEqual(JSON.stringify(\n//   { a: { a: '', b: '', c: '' }, b: '' }\n// ));\nfunction orderKeys(a) {\n  let keys;\n  let ret;\n  switch (typeof a) {\n    case 'string':\n      return a;\n    case 'object':\n      keys = Object.keys(a);\n      keys.sort();\n      ret = {};\n      for (const k of keys) {\n        ret[k] = orderKeys(a[k]);\n      }\n      return ret;\n    case 'boolean':\n      return a;\n    default:\n      throw new TypeError(\n        `type error: orderKeys() does not accept type ${typeof a}`,\n      );\n  }\n}\n"],"names":["assertValidNode","assert","assertType"],"mappings":";;;;;AAAA;AACA;AACA;AACA;;;AAKA;AACA;AACA;AACO,SAAS,KAAK,CAAC,IAAI,EAAE;AAC5B,EAAEA,+BAAe,CAAC,IAAI,CAAC;AACvB,EAAE,OAAO,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;AACxC;;AAEA;AACO,SAAS,UAAU,CAAC,IAAI,EAAE;AACjC,EAAE,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM;AAChD,IAAI,MAAM,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,KAAK,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;AACtD,IAAI,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,KAAK,KAAK,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;AACnD,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