zkjson 0.1.20 → 0.1.22
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.
- package/encoder.js +74 -74
- package/package.json +1 -1
- package/parse.js +161 -162
- package/uint.js +255 -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
|
103
|
-
if (s
|
104
|
-
if (num
|
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
|
112
|
-
if (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
|
116
|
+
} else if (s == "]") {
|
117
|
+
if (num != 1) throw Error()
|
118
118
|
num = 2
|
119
|
-
if (str
|
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
|
127
|
-
if (parts.length
|
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
|
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
|
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
|
151
|
+
if (type == 0) {
|
152
152
|
const type2 = path.shift()
|
153
|
-
if (type2
|
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]
|
168
|
+
if (s[0] == 0 && s[1] == 0) {
|
169
169
|
str += `[${s[2]}]`
|
170
|
-
} else if (s[0]
|
171
|
-
if (str
|
170
|
+
} else if (s[0] == 0 && s[1] == 1) {
|
171
|
+
if (str != "") str += "."
|
172
172
|
} else {
|
173
|
-
str += `${i
|
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
|
193
|
+
if (typeof v == "number") {
|
194
194
|
vals.push([path, encodeVal(v)])
|
195
|
-
} else if (typeof v
|
195
|
+
} else if (typeof v == "boolean") {
|
196
196
|
vals.push([path, encodeVal(v)])
|
197
|
-
} else if (v
|
197
|
+
} else if (v == null) {
|
198
198
|
vals.push([path, encodeVal(v)])
|
199
|
-
} else if (typeof v
|
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
|
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
|
212
|
+
[key.length, ...(key.length == 0 ? [1] : key)],
|
213
213
|
])) {
|
214
214
|
vals.push(v4)
|
215
215
|
}
|
@@ -222,7 +222,7 @@ function encode(json) {
|
|
222
222
|
let flattened = _encode(json)
|
223
223
|
|
224
224
|
flattened.sort((a, b) => {
|
225
|
-
const isUndefined = v => typeof v
|
225
|
+
const isUndefined = v => typeof v == "undefined"
|
226
226
|
const max = Math.max(a[0].length, b[0].length)
|
227
227
|
if (max > 0) {
|
228
228
|
for (let i = 0; i < max; i++) {
|
@@ -250,7 +250,7 @@ function encode(json) {
|
|
250
250
|
|
251
251
|
return flattened.reduce(
|
252
252
|
(arr, v) => arr.concat([...flattenPath(v[0]), ...v[1]]),
|
253
|
-
[]
|
253
|
+
[],
|
254
254
|
)
|
255
255
|
}
|
256
256
|
|
@@ -262,14 +262,14 @@ function _decode(arr) {
|
|
262
262
|
let val = null
|
263
263
|
while (plen > 0) {
|
264
264
|
const plen2 = arr.shift()
|
265
|
-
if (plen2
|
265
|
+
if (plen2 == 0) {
|
266
266
|
const plen3 = arr.shift()
|
267
|
-
if (plen3
|
267
|
+
if (plen3 == 1) {
|
268
268
|
keys.push([plen2, plen3])
|
269
269
|
} else {
|
270
270
|
keys.push([plen2, plen3, arr.shift()])
|
271
271
|
}
|
272
|
-
} else if (plen2
|
272
|
+
} else if (plen2 != 0) {
|
273
273
|
const plen3 = plen2
|
274
274
|
const key = []
|
275
275
|
for (let i2 = 0; i2 < plen3; i2++) key.push(arr.shift())
|
@@ -279,13 +279,13 @@ function _decode(arr) {
|
|
279
279
|
}
|
280
280
|
const type = arr.shift()
|
281
281
|
val = [type]
|
282
|
-
if (type
|
282
|
+
if (type == 2) {
|
283
283
|
val.push(arr.shift())
|
284
284
|
val.push(arr.shift())
|
285
285
|
val.push(arr.shift())
|
286
|
-
} else if (type
|
286
|
+
} else if (type == 1) {
|
287
287
|
val.push(arr.shift())
|
288
|
-
} else if (type
|
288
|
+
} else if (type == 3) {
|
289
289
|
const strlen = arr.shift()
|
290
290
|
val.push(strlen)
|
291
291
|
for (let i2 = 0; i2 < strlen; i2++) val.push(arr.shift())
|
@@ -297,20 +297,20 @@ function _decode(arr) {
|
|
297
297
|
|
298
298
|
function encodeVal(v) {
|
299
299
|
let vals = []
|
300
|
-
if (typeof v
|
300
|
+
if (typeof v == "number" || typeof v == "bigint") {
|
301
301
|
const int = Number.isInteger(v)
|
302
302
|
let moved = 0
|
303
303
|
let num = v
|
304
|
-
while (num % 1
|
304
|
+
while (num % 1 != 0) {
|
305
305
|
num *= 10
|
306
306
|
moved += 1
|
307
307
|
}
|
308
308
|
vals = v < 0 ? [2, 0, moved, -num] : [2, 1, moved, num]
|
309
|
-
} else if (typeof v
|
309
|
+
} else if (typeof v == "boolean") {
|
310
310
|
vals = [1, v ? 1 : 0]
|
311
|
-
} else if (v
|
311
|
+
} else if (v == null) {
|
312
312
|
vals = [0]
|
313
|
-
} else if (typeof v
|
313
|
+
} else if (typeof v == "string") {
|
314
314
|
vals = [3, v.length, ...v.split("").map(c => c.charCodeAt(0))]
|
315
315
|
} else {
|
316
316
|
vals = [4, ...encode(v)]
|
@@ -322,21 +322,21 @@ function decodeVal(arr) {
|
|
322
322
|
const type = arr[0]
|
323
323
|
const _val = arr[1]
|
324
324
|
let val = null
|
325
|
-
if (type
|
325
|
+
if (type == 0) {
|
326
326
|
val = null
|
327
|
-
} else if (type
|
327
|
+
} else if (type == 1) {
|
328
328
|
val = arr[1] ? true : false
|
329
|
-
} else if (type
|
330
|
-
val = (arr[1]
|
329
|
+
} else if (type == 2) {
|
330
|
+
val = (arr[1] == 0 ? -1 : 1) * arr[3]
|
331
331
|
for (let i = 0; i < arr[2]; i++) {
|
332
332
|
val /= 10
|
333
333
|
}
|
334
|
-
} else if (type
|
334
|
+
} else if (type == 3) {
|
335
335
|
val = arr
|
336
336
|
.slice(2)
|
337
|
-
.map(c => String.fromCharCode(c))
|
337
|
+
.map(c => String.fromCharCode(Number(c)))
|
338
338
|
.join("")
|
339
|
-
} else if (type
|
339
|
+
} else if (type == 4) {
|
340
340
|
val = decode(arr.slice(1))
|
341
341
|
}
|
342
342
|
return val
|
@@ -345,31 +345,31 @@ function decodeVal(arr) {
|
|
345
345
|
function decode(arr) {
|
346
346
|
const decoded = _decode(arr)
|
347
347
|
let json =
|
348
|
-
decoded[0]?.[0]?.[0]?.[0]
|
348
|
+
decoded[0]?.[0]?.[0]?.[0] == 0 && decoded[0]?.[0]?.[0]?.[1] == 0 ? [] : {}
|
349
349
|
for (const v of decoded) {
|
350
350
|
const keys = v[0].map(v2 => {
|
351
|
-
if (v2[0]
|
352
|
-
if (v2[1]
|
351
|
+
if (v2[0] == 0) {
|
352
|
+
if (v2[1] == 1) return ""
|
353
353
|
return v2[2]
|
354
354
|
} else {
|
355
355
|
return v2
|
356
356
|
.slice(1)
|
357
|
-
.map(c => String.fromCharCode(c))
|
357
|
+
.map(c => String.fromCharCode(Number(c)))
|
358
358
|
.join("")
|
359
359
|
}
|
360
360
|
})
|
361
|
-
if (keys.length
|
361
|
+
if (keys.length == 0) {
|
362
362
|
json = decodeVal(v[1])
|
363
363
|
} else {
|
364
364
|
let obj = json
|
365
365
|
let i = 0
|
366
366
|
for (const k of keys) {
|
367
|
-
if (typeof k
|
368
|
-
if (typeof keys[i + 1]
|
367
|
+
if (typeof k == "number") {
|
368
|
+
if (typeof keys[i + 1] == "undefined") {
|
369
369
|
obj[k] = decodeVal(v[1])
|
370
370
|
} else {
|
371
|
-
if (typeof obj[k]
|
372
|
-
if (typeof keys[i + 1]
|
371
|
+
if (typeof obj[k] == "undefined") {
|
372
|
+
if (typeof keys[i + 1] == "string") {
|
373
373
|
obj[k] = {}
|
374
374
|
} else {
|
375
375
|
obj[k] = []
|
@@ -377,10 +377,10 @@ function decode(arr) {
|
|
377
377
|
}
|
378
378
|
}
|
379
379
|
} else {
|
380
|
-
if (typeof obj[k]
|
381
|
-
if (typeof keys[i + 1]
|
380
|
+
if (typeof obj[k] == "undefined") {
|
381
|
+
if (typeof keys[i + 1] == "undefined") {
|
382
382
|
obj[k] = decodeVal(v[1])
|
383
|
-
} else if (typeof keys[i + 1]
|
383
|
+
} else if (typeof keys[i + 1] == "string") {
|
384
384
|
obj[k] = {}
|
385
385
|
} else {
|
386
386
|
obj[k] = []
|
@@ -421,12 +421,12 @@ function toSignal(arr) {
|
|
421
421
|
let str = splitEvery(8, n.toString().split(""))
|
422
422
|
let i = 0
|
423
423
|
str = str.map(s => {
|
424
|
-
const len = i
|
424
|
+
const len = i == str.length - 1 ? s.length : 9
|
425
425
|
i++
|
426
426
|
return len.toString() + s.join("")
|
427
427
|
})
|
428
428
|
return str
|
429
|
-
})
|
429
|
+
}),
|
430
430
|
)
|
431
431
|
let _arr2 = []
|
432
432
|
let one = 0
|
@@ -434,10 +434,10 @@ function toSignal(arr) {
|
|
434
434
|
let start = null
|
435
435
|
for (let v of _arr) {
|
436
436
|
_arr2.push(v)
|
437
|
-
if (v.length - 1
|
438
|
-
if (start
|
437
|
+
if (v.length - 1 == 1) {
|
438
|
+
if (start == null) start = i
|
439
439
|
one += v.length - 1
|
440
|
-
if (one
|
440
|
+
if (one == 9) {
|
441
441
|
_arr2[start] = `0${one}${_arr2[start][1]}`
|
442
442
|
for (let i2 = start + 1; i2 <= i; i2++) _arr2[i2] = `${_arr2[i2][1]}`
|
443
443
|
one = 0
|
@@ -462,11 +462,11 @@ function toSignal(arr) {
|
|
462
462
|
let cur = 0
|
463
463
|
let num = ""
|
464
464
|
for (let v of _arr2) {
|
465
|
-
if (chain
|
465
|
+
if (chain == null && +v[0] == 0) {
|
466
466
|
chain = +v[1]
|
467
467
|
cur = 1
|
468
468
|
num = v
|
469
|
-
} else if (chain
|
469
|
+
} else if (chain != null) {
|
470
470
|
num += v
|
471
471
|
cur++
|
472
472
|
if (chain == cur) {
|
@@ -479,19 +479,19 @@ function toSignal(arr) {
|
|
479
479
|
_arr3.push(v)
|
480
480
|
}
|
481
481
|
}
|
482
|
-
if (chain
|
482
|
+
if (chain != null) _arr3.push(num)
|
483
483
|
let arrs2 = []
|
484
484
|
let len2 = 0
|
485
485
|
let str2 = ""
|
486
486
|
for (let v of _arr3) {
|
487
487
|
if (len2 + v.length > 75) {
|
488
488
|
arrs2.push("1" + str2)
|
489
|
-
if (+v[0]
|
489
|
+
if (+v[0] == 0) {
|
490
490
|
let len3 = 75 - len2
|
491
491
|
if (len3 == 2 || len3 == 3) {
|
492
492
|
arrs2[arrs2.length - 1] += `1${v[2]}`
|
493
493
|
let new_len = +v[1] - 1
|
494
|
-
if (new_len
|
494
|
+
if (new_len == 2) {
|
495
495
|
v = `1${v[3]}1${v[4]}`
|
496
496
|
} else {
|
497
497
|
v = `0${new_len}${v.slice(3)}`
|
@@ -499,16 +499,16 @@ function toSignal(arr) {
|
|
499
499
|
} else if (len3 > 3) {
|
500
500
|
let new_len = +v[1] - 2
|
501
501
|
let old_len = 2
|
502
|
-
if (len3
|
502
|
+
if (len3 == 4) {
|
503
503
|
arrs2[arrs2.length - 1] += `1${v[2]}1${v[3]}`
|
504
504
|
} else {
|
505
505
|
old_len = len3 - 2
|
506
506
|
new_len = +v[1] - old_len
|
507
507
|
arrs2[arrs2.length - 1] += `0${old_len}${v.slice(2, 2 + old_len)}`
|
508
508
|
}
|
509
|
-
if (new_len
|
509
|
+
if (new_len == 1) {
|
510
510
|
v = `1${v[old_len + 2]}`
|
511
|
-
} else if (new_len
|
511
|
+
} else if (new_len == 2) {
|
512
512
|
v = `1${v[old_len + 2]}1${v[old_len + 3]}`
|
513
513
|
} else {
|
514
514
|
v = `0${new_len}${v.slice(old_len + 2)}`
|
@@ -521,7 +521,7 @@ function toSignal(arr) {
|
|
521
521
|
len2 += v.length
|
522
522
|
str2 += v
|
523
523
|
}
|
524
|
-
if (str2
|
524
|
+
if (str2 != "") arrs2.push("1" + str2)
|
525
525
|
return arrs2
|
526
526
|
}
|
527
527
|
|
@@ -533,13 +533,13 @@ function fromSignal(arr) {
|
|
533
533
|
let str = s.split("")
|
534
534
|
while (str.length > 0) {
|
535
535
|
const len = +str.shift()
|
536
|
-
if (len
|
536
|
+
if (len == 0) {
|
537
537
|
const len2 = +str.shift()
|
538
538
|
for (let i2 = 0; i2 < len2; i2++) {
|
539
539
|
_arr.push(+str[i2])
|
540
540
|
}
|
541
541
|
str = str.slice(len2)
|
542
|
-
} else if (len
|
542
|
+
} else if (len == 9) {
|
543
543
|
prev += str.slice(0, 8).join("")
|
544
544
|
str = str.slice(8)
|
545
545
|
} else {
|