zkjson 0.1.21 → 0.1.23

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. package/encoder.js +74 -75
  2. package/package.json +1 -1
  3. package/parse.js +133 -92
  4. package/uint.js +246 -203
package/encoder.js CHANGED
@@ -99,39 +99,39 @@ function encodePath(path) {
99
99
  let str = ""
100
100
  let num = 0
101
101
  for (const s of path) {
102
- if (num === 2 && !(s === "." || s === "[")) throw Error()
103
- if (s === ".") {
104
- if (num === 2) {
102
+ if (num == 2 && !(s == "." || s == "[")) throw Error()
103
+ if (s == ".") {
104
+ if (num == 2) {
105
105
  num = 0
106
106
  } else {
107
107
  parts.push(str)
108
108
  str = ""
109
109
  }
110
- } else if (s === "[") {
111
- if (num !== 2) {
112
- if (str !== "" || parts.length > 0) parts.push(str)
110
+ } else if (s == "[") {
111
+ if (num != 2) {
112
+ if (str != "" || parts.length > 0) parts.push(str)
113
113
  str = ""
114
114
  }
115
115
  num = 1
116
- } else if (s === "]") {
117
- if (num !== 1) throw Error()
116
+ } else if (s == "]") {
117
+ if (num != 1) throw Error()
118
118
  num = 2
119
- if (str === "" || Number.isNaN(+str)) throw Error()
119
+ if (str == "" || Number.isNaN(+str)) throw Error()
120
120
  parts.push(+str)
121
121
  str = ""
122
122
  } else {
123
123
  str += s
124
124
  }
125
125
  }
126
- if (str !== "") parts.push(str)
127
- if (parts.length === 0) parts.push("")
126
+ if (str != "") parts.push(str)
127
+ if (parts.length == 0) parts.push("")
128
128
  let encoded = [parts.length]
129
129
  for (const p of parts) {
130
- if (typeof p === "number") {
130
+ if (typeof p == "number") {
131
131
  encoded = encoded.concat([0, 0, p])
132
132
  } else {
133
133
  let plen = [p.length]
134
- if (p.length === 0) plen.push(1)
134
+ if (p.length == 0) plen.push(1)
135
135
  encoded = encoded.concat([
136
136
  ...plen,
137
137
  ...p.split("").map(c => c.charCodeAt(0)),
@@ -148,9 +148,9 @@ function decodePath(path) {
148
148
  while (path.length > 0) {
149
149
  const type = path.shift()
150
150
  let val = null
151
- if (type === 0) {
151
+ if (type == 0) {
152
152
  const type2 = path.shift()
153
- if (type2 === 0) {
153
+ if (type2 == 0) {
154
154
  val = [type2, path.shift()]
155
155
  } else {
156
156
  val = [type2]
@@ -165,14 +165,14 @@ function decodePath(path) {
165
165
  }
166
166
  let i = 0
167
167
  for (let s of p) {
168
- if (s[0] === 0 && s[1] === 0) {
168
+ if (s[0] == 0 && s[1] == 0) {
169
169
  str += `[${s[2]}]`
170
- } else if (s[0] === 0 && s[1] === 1) {
171
- if (str !== "") str += "."
170
+ } else if (s[0] == 0 && s[1] == 1) {
171
+ if (str != "") str += "."
172
172
  } else {
173
- str += `${i === 0 ? "" : "."}${s
173
+ str += `${i == 0 ? "" : "."}${s
174
174
  .slice(1)
175
- .map(c => String.fromCharCode(c))
175
+ .map(c => String.fromCharCode(Number(c)))
176
176
  .join("")}`
177
177
  }
178
178
  i++
@@ -190,13 +190,13 @@ function flattenPath(path) {
190
190
 
191
191
  function _encode(v, path = []) {
192
192
  let vals = []
193
- if (typeof v === "number") {
193
+ if (typeof v == "number") {
194
194
  vals.push([path, encodeVal(v)])
195
- } else if (typeof v === "boolean") {
195
+ } else if (typeof v == "boolean") {
196
196
  vals.push([path, encodeVal(v)])
197
- } else if (v === null) {
197
+ } else if (v == null) {
198
198
  vals.push([path, encodeVal(v)])
199
- } else if (typeof v === "string") {
199
+ } else if (typeof v == "string") {
200
200
  vals.push([path, encodeVal(v)])
201
201
  } else if (Array.isArray(v)) {
202
202
  let i = 0
@@ -204,12 +204,12 @@ function _encode(v, path = []) {
204
204
  for (const v3 of _encode(v2, [...path, [0, 0, i]])) vals.push(v3)
205
205
  i++
206
206
  }
207
- } else if (typeof v === "object") {
207
+ } else if (typeof v == "object") {
208
208
  for (const k in v) {
209
209
  const key = k.split("").map(c => c.charCodeAt(0))
210
210
  for (let v4 of _encode(v[k], [
211
211
  ...path,
212
- [key.length, ...(key.length === 0 ? [1] : key)],
212
+ [key.length, ...(key.length == 0 ? [1] : key)],
213
213
  ])) {
214
214
  vals.push(v4)
215
215
  }
@@ -220,9 +220,8 @@ function _encode(v, path = []) {
220
220
 
221
221
  function encode(json) {
222
222
  let flattened = _encode(json)
223
-
224
223
  flattened.sort((a, b) => {
225
- const isUndefined = v => typeof v === "undefined"
224
+ const isUndefined = v => typeof v == "undefined"
226
225
  const max = Math.max(a[0].length, b[0].length)
227
226
  if (max > 0) {
228
227
  for (let i = 0; i < max; i++) {
@@ -250,7 +249,7 @@ function encode(json) {
250
249
 
251
250
  return flattened.reduce(
252
251
  (arr, v) => arr.concat([...flattenPath(v[0]), ...v[1]]),
253
- []
252
+ [],
254
253
  )
255
254
  }
256
255
 
@@ -262,14 +261,14 @@ function _decode(arr) {
262
261
  let val = null
263
262
  while (plen > 0) {
264
263
  const plen2 = arr.shift()
265
- if (plen2 === 0) {
264
+ if (plen2 == 0) {
266
265
  const plen3 = arr.shift()
267
- if (plen3 === 1) {
266
+ if (plen3 == 1) {
268
267
  keys.push([plen2, plen3])
269
268
  } else {
270
269
  keys.push([plen2, plen3, arr.shift()])
271
270
  }
272
- } else if (plen2 !== 0) {
271
+ } else if (plen2 != 0) {
273
272
  const plen3 = plen2
274
273
  const key = []
275
274
  for (let i2 = 0; i2 < plen3; i2++) key.push(arr.shift())
@@ -279,13 +278,13 @@ function _decode(arr) {
279
278
  }
280
279
  const type = arr.shift()
281
280
  val = [type]
282
- if (type === 2) {
281
+ if (type == 2) {
283
282
  val.push(arr.shift())
284
283
  val.push(arr.shift())
285
284
  val.push(arr.shift())
286
- } else if (type === 1) {
285
+ } else if (type == 1) {
287
286
  val.push(arr.shift())
288
- } else if (type === 3) {
287
+ } else if (type == 3) {
289
288
  const strlen = arr.shift()
290
289
  val.push(strlen)
291
290
  for (let i2 = 0; i2 < strlen; i2++) val.push(arr.shift())
@@ -297,20 +296,20 @@ function _decode(arr) {
297
296
 
298
297
  function encodeVal(v) {
299
298
  let vals = []
300
- if (typeof v === "number") {
299
+ if (typeof v == "number" || typeof v == "bigint") {
301
300
  const int = Number.isInteger(v)
302
301
  let moved = 0
303
302
  let num = v
304
- while (num % 1 !== 0) {
303
+ while (num % 1 != 0) {
305
304
  num *= 10
306
305
  moved += 1
307
306
  }
308
307
  vals = v < 0 ? [2, 0, moved, -num] : [2, 1, moved, num]
309
- } else if (typeof v === "boolean") {
308
+ } else if (typeof v == "boolean") {
310
309
  vals = [1, v ? 1 : 0]
311
- } else if (v === null) {
310
+ } else if (v == null) {
312
311
  vals = [0]
313
- } else if (typeof v === "string") {
312
+ } else if (typeof v == "string") {
314
313
  vals = [3, v.length, ...v.split("").map(c => c.charCodeAt(0))]
315
314
  } else {
316
315
  vals = [4, ...encode(v)]
@@ -322,21 +321,21 @@ function decodeVal(arr) {
322
321
  const type = arr[0]
323
322
  const _val = arr[1]
324
323
  let val = null
325
- if (type === 0) {
324
+ if (type == 0) {
326
325
  val = null
327
- } else if (type === 1) {
326
+ } else if (type == 1) {
328
327
  val = arr[1] ? true : false
329
- } else if (type === 2) {
330
- val = (arr[1] === 0 ? -1 : 1) * arr[3]
328
+ } else if (type == 2) {
329
+ val = (arr[1] == 0 ? -1 : 1) * arr[3]
331
330
  for (let i = 0; i < arr[2]; i++) {
332
331
  val /= 10
333
332
  }
334
- } else if (type === 3) {
333
+ } else if (type == 3) {
335
334
  val = arr
336
335
  .slice(2)
337
- .map(c => String.fromCharCode(c))
336
+ .map(c => String.fromCharCode(Number(c)))
338
337
  .join("")
339
- } else if (type === 4) {
338
+ } else if (type == 4) {
340
339
  val = decode(arr.slice(1))
341
340
  }
342
341
  return val
@@ -345,31 +344,31 @@ function decodeVal(arr) {
345
344
  function decode(arr) {
346
345
  const decoded = _decode(arr)
347
346
  let json =
348
- decoded[0]?.[0]?.[0]?.[0] === 0 && decoded[0]?.[0]?.[0]?.[1] === 0 ? [] : {}
347
+ decoded[0]?.[0]?.[0]?.[0] == 0 && decoded[0]?.[0]?.[0]?.[1] == 0 ? [] : {}
349
348
  for (const v of decoded) {
350
349
  const keys = v[0].map(v2 => {
351
- if (v2[0] === 0) {
352
- if (v2[1] === 1) return ""
350
+ if (v2[0] == 0) {
351
+ if (v2[1] == 1) return ""
353
352
  return v2[2]
354
353
  } else {
355
354
  return v2
356
355
  .slice(1)
357
- .map(c => String.fromCharCode(c))
356
+ .map(c => String.fromCharCode(Number(c)))
358
357
  .join("")
359
358
  }
360
359
  })
361
- if (keys.length === 0) {
360
+ if (keys.length == 0) {
362
361
  json = decodeVal(v[1])
363
362
  } else {
364
363
  let obj = json
365
364
  let i = 0
366
365
  for (const k of keys) {
367
- if (typeof k === "number") {
368
- if (typeof keys[i + 1] === "undefined") {
366
+ if (typeof k == "number") {
367
+ if (typeof keys[i + 1] == "undefined") {
369
368
  obj[k] = decodeVal(v[1])
370
369
  } else {
371
- if (typeof obj[k] === "undefined") {
372
- if (typeof keys[i + 1] === "string") {
370
+ if (typeof obj[k] == "undefined") {
371
+ if (typeof keys[i + 1] == "string") {
373
372
  obj[k] = {}
374
373
  } else {
375
374
  obj[k] = []
@@ -377,10 +376,10 @@ function decode(arr) {
377
376
  }
378
377
  }
379
378
  } else {
380
- if (typeof obj[k] === "undefined") {
381
- if (typeof keys[i + 1] === "undefined") {
379
+ if (typeof obj[k] == "undefined") {
380
+ if (typeof keys[i + 1] == "undefined") {
382
381
  obj[k] = decodeVal(v[1])
383
- } else if (typeof keys[i + 1] === "string") {
382
+ } else if (typeof keys[i + 1] == "string") {
384
383
  obj[k] = {}
385
384
  } else {
386
385
  obj[k] = []
@@ -421,12 +420,12 @@ function toSignal(arr) {
421
420
  let str = splitEvery(8, n.toString().split(""))
422
421
  let i = 0
423
422
  str = str.map(s => {
424
- const len = i === str.length - 1 ? s.length : 9
423
+ const len = i == str.length - 1 ? s.length : 9
425
424
  i++
426
425
  return len.toString() + s.join("")
427
426
  })
428
427
  return str
429
- })
428
+ }),
430
429
  )
431
430
  let _arr2 = []
432
431
  let one = 0
@@ -434,10 +433,10 @@ function toSignal(arr) {
434
433
  let start = null
435
434
  for (let v of _arr) {
436
435
  _arr2.push(v)
437
- if (v.length - 1 === 1) {
438
- if (start === null) start = i
436
+ if (v.length - 1 == 1) {
437
+ if (start == null) start = i
439
438
  one += v.length - 1
440
- if (one === 9) {
439
+ if (one == 9) {
441
440
  _arr2[start] = `0${one}${_arr2[start][1]}`
442
441
  for (let i2 = start + 1; i2 <= i; i2++) _arr2[i2] = `${_arr2[i2][1]}`
443
442
  one = 0
@@ -462,11 +461,11 @@ function toSignal(arr) {
462
461
  let cur = 0
463
462
  let num = ""
464
463
  for (let v of _arr2) {
465
- if (chain === null && +v[0] === 0) {
464
+ if (chain == null && +v[0] == 0) {
466
465
  chain = +v[1]
467
466
  cur = 1
468
467
  num = v
469
- } else if (chain !== null) {
468
+ } else if (chain != null) {
470
469
  num += v
471
470
  cur++
472
471
  if (chain == cur) {
@@ -479,19 +478,19 @@ function toSignal(arr) {
479
478
  _arr3.push(v)
480
479
  }
481
480
  }
482
- if (chain !== null) _arr3.push(num)
481
+ if (chain != null) _arr3.push(num)
483
482
  let arrs2 = []
484
483
  let len2 = 0
485
484
  let str2 = ""
486
485
  for (let v of _arr3) {
487
486
  if (len2 + v.length > 75) {
488
487
  arrs2.push("1" + str2)
489
- if (+v[0] === 0) {
488
+ if (+v[0] == 0) {
490
489
  let len3 = 75 - len2
491
490
  if (len3 == 2 || len3 == 3) {
492
491
  arrs2[arrs2.length - 1] += `1${v[2]}`
493
492
  let new_len = +v[1] - 1
494
- if (new_len === 2) {
493
+ if (new_len == 2) {
495
494
  v = `1${v[3]}1${v[4]}`
496
495
  } else {
497
496
  v = `0${new_len}${v.slice(3)}`
@@ -499,16 +498,16 @@ function toSignal(arr) {
499
498
  } else if (len3 > 3) {
500
499
  let new_len = +v[1] - 2
501
500
  let old_len = 2
502
- if (len3 === 4) {
501
+ if (len3 == 4) {
503
502
  arrs2[arrs2.length - 1] += `1${v[2]}1${v[3]}`
504
503
  } else {
505
504
  old_len = len3 - 2
506
505
  new_len = +v[1] - old_len
507
506
  arrs2[arrs2.length - 1] += `0${old_len}${v.slice(2, 2 + old_len)}`
508
507
  }
509
- if (new_len === 1) {
508
+ if (new_len == 1) {
510
509
  v = `1${v[old_len + 2]}`
511
- } else if (new_len === 2) {
510
+ } else if (new_len == 2) {
512
511
  v = `1${v[old_len + 2]}1${v[old_len + 3]}`
513
512
  } else {
514
513
  v = `0${new_len}${v.slice(old_len + 2)}`
@@ -521,7 +520,7 @@ function toSignal(arr) {
521
520
  len2 += v.length
522
521
  str2 += v
523
522
  }
524
- if (str2 !== "") arrs2.push("1" + str2)
523
+ if (str2 != "") arrs2.push("1" + str2)
525
524
  return arrs2
526
525
  }
527
526
 
@@ -533,13 +532,13 @@ function fromSignal(arr) {
533
532
  let str = s.split("")
534
533
  while (str.length > 0) {
535
534
  const len = +str.shift()
536
- if (len === 0) {
535
+ if (len == 0) {
537
536
  const len2 = +str.shift()
538
537
  for (let i2 = 0; i2 < len2; i2++) {
539
538
  _arr.push(+str[i2])
540
539
  }
541
540
  str = str.slice(len2)
542
- } else if (len === 9) {
541
+ } else if (len == 9) {
543
542
  prev += str.slice(0, 8).join("")
544
543
  str = str.slice(8)
545
544
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zkjson",
3
- "version": "0.1.21",
3
+ "version": "0.1.23",
4
4
  "description": "Zero Knowledge Provable JSON",
5
5
  "main": "index.js",
6
6
  "license": "MIT",