@muze-nl/od-jsontag 0.2.0 → 0.2.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/serialize.mjs +24 -18
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@muze-nl/od-jsontag",
3
- "version": "0.2.0",
3
+ "version": "0.2.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/serialize.mjs CHANGED
@@ -15,17 +15,8 @@ function stringToSAB(strData) {
15
15
  return uint8sab
16
16
  }
17
17
 
18
- export default function serialize(value, meta, skipLength=false, index=false) {
18
+ export default function serialize(value, options={}) {
19
19
  let resultArray = []
20
- if (!meta) {
21
- meta = {}
22
- }
23
- if (!meta.index) {
24
- meta.index = {}
25
- }
26
- if (!meta.index.id) {
27
- meta.index.id = new Map()
28
- }
29
20
  let references = new WeakMap()
30
21
 
31
22
  function stringifyValue(value, inarray=false) {
@@ -139,7 +130,7 @@ export default function serialize(value, meta, skipLength=false, index=false) {
139
130
  if (typeof s == 'string' || s instanceof String) {
140
131
  s = encoder.encode(s)
141
132
  }
142
- if (skipLength) {
133
+ if (s[0]==43 || options.skipLength) {
143
134
  return new Uint8Array(s)
144
135
  }
145
136
  let length = encoder.encode('('+s.length+')')
@@ -154,16 +145,31 @@ export default function serialize(value, meta, skipLength=false, index=false) {
154
145
  } else {
155
146
  resultArray.push(value)
156
147
  }
157
- let current = 0
158
- while(current<resultArray.length) {
159
- if (resultArray[current][isChanged] || !resultArray[current][isProxy]) {
160
- resultArray[current] = encoder.encode(innerStringify(current))
148
+ let currentSource = 0
149
+ let currentResult = 0
150
+ let skipCount = 0
151
+ let result = resultArray
152
+ if (options.changes) {
153
+ result = []
154
+ }
155
+ while(currentSource<resultArray.length) {
156
+ if (resultArray[currentSource][isChanged] || !resultArray[currentSource][isProxy]) {
157
+ if (skipCount) {
158
+ result[currentResult] = encoder.encode('+'+skipCount)
159
+ skipCount = 0
160
+ currentResult++
161
+ }
162
+ result[currentResult] = encoder.encode(innerStringify(currentSource))
163
+ currentResult++
164
+ } else if (!options.changes) {
165
+ resultArray[currentResult] = resultArray[currentSource][getBuffer](currentSource)
166
+ currentResult++
161
167
  } else {
162
- resultArray[current] = resultArray[current][getBuffer](current)
168
+ skipCount++
163
169
  }
164
- current++
170
+ currentSource++
165
171
  }
166
- let arr = resultArray.map(encode)
172
+ let arr = result.map(encode)
167
173
  let length = 0
168
174
  for (let line of arr) {
169
175
  length += line.length+1