@muze-nl/od-jsontag 0.3.0 → 0.3.1
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/package.json +1 -1
- package/src/parse.mjs +39 -3
- package/src/serialize.mjs +23 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@muze-nl/od-jsontag",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "On Demand JSONTag: parse/serialize large datastructures on demand, useful for sharing data between threads",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": "Auke van Slooten <auke@muze.nl>",
|
package/src/parse.mjs
CHANGED
|
@@ -15,6 +15,25 @@ function stringToSAB(strData) {
|
|
|
15
15
|
return uint8sab
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
function SABtoString(arr) {
|
|
19
|
+
let string = '';
|
|
20
|
+
for (let c of arr) {
|
|
21
|
+
string+= String.fromCharCode(c)
|
|
22
|
+
}
|
|
23
|
+
return string
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
class Slice {
|
|
27
|
+
constructor(start, end) {
|
|
28
|
+
this.start = start;
|
|
29
|
+
this.end = end;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const isSlice = function(r) {
|
|
34
|
+
return r instanceof Slice
|
|
35
|
+
}
|
|
36
|
+
|
|
18
37
|
export default function parse(input, meta, immutable=true)
|
|
19
38
|
{
|
|
20
39
|
if (!meta) {
|
|
@@ -69,7 +88,8 @@ export default function parse(input, meta, immutable=true)
|
|
|
69
88
|
let next = function(c)
|
|
70
89
|
{
|
|
71
90
|
if (c && c!==ch) {
|
|
72
|
-
|
|
91
|
+
let source = SABtoString(input)
|
|
92
|
+
error("Expected '"+c+"' instead of '"+ch+"': "+at+':'+source)
|
|
73
93
|
}
|
|
74
94
|
ch = String.fromCharCode(input.at(at))
|
|
75
95
|
at+=1
|
|
@@ -566,7 +586,11 @@ export default function parse(input, meta, immutable=true)
|
|
|
566
586
|
while(ch) {
|
|
567
587
|
item = value()
|
|
568
588
|
checkUnresolved(item, array, array.length)
|
|
569
|
-
|
|
589
|
+
if (isSlice(item)) {
|
|
590
|
+
array = array.concat(meta.resultArray.slice(item.start, item.end))
|
|
591
|
+
} else {
|
|
592
|
+
array.push(item)
|
|
593
|
+
}
|
|
570
594
|
whitespace()
|
|
571
595
|
if (ch===']') {
|
|
572
596
|
next(']')
|
|
@@ -651,6 +675,15 @@ export default function parse(input, meta, immutable=true)
|
|
|
651
675
|
numString += ch
|
|
652
676
|
next()
|
|
653
677
|
}
|
|
678
|
+
if (ch=='-') {
|
|
679
|
+
next('-')
|
|
680
|
+
let endString = ''
|
|
681
|
+
while(ch>='0' && ch<='9') {
|
|
682
|
+
endString += ch
|
|
683
|
+
next()
|
|
684
|
+
}
|
|
685
|
+
return new Slice(parseInt(numString),parseInt(endString)+1) // +1 because array.slice(start,end) slices upto but not including end
|
|
686
|
+
}
|
|
654
687
|
return parseInt(numString)
|
|
655
688
|
}
|
|
656
689
|
|
|
@@ -954,7 +987,7 @@ export default function parse(input, meta, immutable=true)
|
|
|
954
987
|
return undefined
|
|
955
988
|
}
|
|
956
989
|
firstParse(target)
|
|
957
|
-
Object.defineProperty(target, prop, descriptor)
|
|
990
|
+
return Object.defineProperty(target, prop, descriptor)
|
|
958
991
|
},
|
|
959
992
|
has(target, prop) {
|
|
960
993
|
if (meta.access && !meta.access(target, prop, 'has')) {
|
|
@@ -1027,6 +1060,9 @@ export default function parse(input, meta, immutable=true)
|
|
|
1027
1060
|
whitespace()
|
|
1028
1061
|
if (ch==='~') {
|
|
1029
1062
|
let vOffset = offset()
|
|
1063
|
+
if (isSlice(vOffset)) {
|
|
1064
|
+
return vOffset
|
|
1065
|
+
}
|
|
1030
1066
|
return meta.resultArray[vOffset]
|
|
1031
1067
|
}
|
|
1032
1068
|
if (ch==='<') {
|
package/src/serialize.mjs
CHANGED
|
@@ -90,7 +90,29 @@ export default function serialize(value, options={}) {
|
|
|
90
90
|
prop = typeString + value
|
|
91
91
|
break
|
|
92
92
|
case 'array':
|
|
93
|
-
let entries = value.map(e => stringifyValue(e, true, current))
|
|
93
|
+
let entries = value.map(e => stringifyValue(e, true, current))
|
|
94
|
+
let mergedEntries = []
|
|
95
|
+
let previousIndex = null
|
|
96
|
+
let startSlice = null
|
|
97
|
+
entries.forEach(e => {
|
|
98
|
+
if (e[0]=='~') {
|
|
99
|
+
let currIndex = parseInt(e.substr(1))
|
|
100
|
+
if (startSlice && currIndex === (previousIndex + 1)) {
|
|
101
|
+
mergedEntries.pop()
|
|
102
|
+
mergedEntries.push('~' + startSlice + '-' + currIndex)
|
|
103
|
+
previousIndex = currIndex
|
|
104
|
+
} else {
|
|
105
|
+
mergedEntries.push(e)
|
|
106
|
+
previousIndex = currIndex
|
|
107
|
+
startSlice = currIndex
|
|
108
|
+
}
|
|
109
|
+
} else {
|
|
110
|
+
mergedEntries.push(e)
|
|
111
|
+
previousIndex = null
|
|
112
|
+
startSlice = null
|
|
113
|
+
}
|
|
114
|
+
})
|
|
115
|
+
entries = mergedEntries.join(',')
|
|
94
116
|
prop = typeString + '[' + entries + ']'
|
|
95
117
|
break
|
|
96
118
|
case 'object':
|