@muze-nl/od-jsontag 0.3.0 → 0.3.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@muze-nl/od-jsontag",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
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
- error("Expected '"+c+"' instead of '"+ch+"': "+at+':'+input)
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
- array.push(item)
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
 
@@ -665,7 +698,7 @@ export default function parse(input, meta, immutable=true)
665
698
  Object.entries(parent).forEach(([key,entry]) => {
666
699
  if (Array.isArray(entry)) {
667
700
  makeChildProxies(entry)
668
- } else if (JSONTag.getType(entry)==='object') {
701
+ } else if (entry && JSONTag.getType(entry)==='object') {
669
702
  if (entry[isProxy]) {
670
703
  // do nothing
671
704
  } else {
@@ -919,7 +952,7 @@ export default function parse(input, meta, immutable=true)
919
952
  if (meta.access && !meta.access(target, prop, 'set')) {
920
953
  return undefined
921
954
  }
922
- if (JSONTag.getType(value)==='object' && !value[isProxy]) {
955
+ if (value && JSONTag.getType(value)==='object' && !value[isProxy]) {
923
956
  value = getNewValueProxy(value)
924
957
  }
925
958
  target[prop] = value
@@ -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')) {
@@ -993,6 +1026,9 @@ export default function parse(input, meta, immutable=true)
993
1026
  }
994
1027
 
995
1028
  const getNewValueProxy = function(value) {
1029
+ if (value === null) {
1030
+ return null
1031
+ }
996
1032
  let index = meta.resultArray.length
997
1033
  meta.resultArray.push('')
998
1034
  value[getIndex] = index
@@ -1027,6 +1063,9 @@ export default function parse(input, meta, immutable=true)
1027
1063
  whitespace()
1028
1064
  if (ch==='~') {
1029
1065
  let vOffset = offset()
1066
+ if (isSlice(vOffset)) {
1067
+ return vOffset
1068
+ }
1030
1069
  return meta.resultArray[vOffset]
1031
1070
  }
1032
1071
  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)).join(',')
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':