zkjson 0.4.1 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/encoder-v2.js +17 -9
  2. package/package.json +1 -1
package/encoder-v2.js CHANGED
@@ -460,7 +460,7 @@ function listKeys(v, key = [], keys = []) {
460
460
  listKeys(v2, append(i, key), keys)
461
461
  i++
462
462
  }
463
- } else if (typeof v == "object") {
463
+ } else if (typeof v === "object" && v !== null) {
464
464
  for (const k in v) listKeys(v[k], append(k, key), keys)
465
465
  } else {
466
466
  keys.push(key)
@@ -516,7 +516,8 @@ function applyDic(arr, dic) {
516
516
  })
517
517
  }
518
518
 
519
- function encodePaths(data) {
519
+ function encodePaths(data, index = false) {
520
+ let i = 0
520
521
  for (let v of data) {
521
522
  let path = []
522
523
  for (let v2 of v[0]) {
@@ -530,6 +531,8 @@ function encodePaths(data) {
530
531
  }
531
532
  }
532
533
  v[0] = path
534
+ if (index) v.push(i)
535
+ i++
533
536
  }
534
537
  return data
535
538
  }
@@ -599,7 +602,8 @@ function encodeDic(dict) {
599
602
  return enc
600
603
  }
601
604
 
602
- function encode(json, nodic = false) {
605
+ function encode(_json, nodic = false) {
606
+ let json = clone(_json)
603
607
  let dic = null
604
608
  let dictionary, keyMap
605
609
  if (nodic !== true) {
@@ -607,10 +611,9 @@ function encode(json, nodic = false) {
607
611
  if (dictionary.length > 0) dic = encodeDic(dictionary)
608
612
  }
609
613
  let enc = _encode(json)
610
- if (dic) enc = applyDic(enc, keyMap)
611
-
612
- enc = encodePaths(enc)
613
- enc.sort((a, b) => {
614
+ let _enc = clone(enc)
615
+ _enc = encodePaths(_enc, true)
616
+ _enc.sort((a, b) => {
614
617
  const isUndefined = v => typeof v == "undefined"
615
618
  const max = Math.max(a[0].length, b[0].length)
616
619
  if (max > 0) {
@@ -636,10 +639,14 @@ function encode(json, nodic = false) {
636
639
  }
637
640
  return 0
638
641
  })
642
+ if (dic) enc = applyDic(enc, keyMap)
643
+ enc = encodePaths(enc)
644
+ let enc2 = []
645
+ for (let v of _enc) enc2.push(enc[v[2]])
639
646
  const _dic = dic ? [1, 0, 2, dictionary.length, ...dic] : []
640
647
  return concat(
641
648
  _dic,
642
- enc.reduce((arr, v) => arr.concat([...flattenPath(v[0]), ...v[1]]), []),
649
+ enc2.reduce((arr, v) => arr.concat([...flattenPath(v[0]), ...v[1]]), []),
643
650
  )
644
651
  }
645
652
 
@@ -766,7 +773,8 @@ function decodeVal(arr) {
766
773
  return val
767
774
  }
768
775
 
769
- function decode(arr) {
776
+ function decode(_arr) {
777
+ let arr = clone(_arr)
770
778
  const decoded = _decode(arr)
771
779
  let json =
772
780
  decoded[0]?.[0]?.[0]?.[0] == 0 && decoded[0]?.[0]?.[0]?.[1] == 0 ? [] : {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zkjson",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "description": "Zero Knowledge Provable JSON",
5
5
  "main": "index.js",
6
6
  "license": "MIT",