flatpack-json 8.14.3

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Jason Dent
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # Flatpack JSON
2
+
3
+ A library to reduce the size of JSON data by flattening and normalizing the values.
4
+
5
+ It is similar to [flatted](https://www.npmjs.com/package/flatted).
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ npm install -S flatpack-json
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ts
16
+ import { parse, stringify } from 'flatpack-json';
17
+ ```
@@ -0,0 +1,92 @@
1
+ import { FlatpackedWrapper } from './flatpackUtil.mjs';
2
+ import type { Flatpacked, FlatpackOptions, Serializable, Unpacked } from './types.mjs';
3
+ export declare class FlatpackStore {
4
+ #private;
5
+ readonly options?: FlatpackOptions | undefined;
6
+ private knownElements;
7
+ private assignedElements;
8
+ private elements;
9
+ private root;
10
+ private dedupe;
11
+ private sortKeys;
12
+ private emptyObjIdx;
13
+ private ids;
14
+ /**
15
+ * Cache of primitives and objects that have been added to the data.
16
+ */
17
+ private cache;
18
+ /**
19
+ * Set of elements that have been referenced by other indexes.
20
+ */
21
+ private referenced;
22
+ /**
23
+ * Cache of arrays that have been deduped.
24
+ * The key is a hash of the array elements as a function of the index of the element.
25
+ */
26
+ private cachedArrays;
27
+ private cachedObjects;
28
+ private cachedSets;
29
+ private cachedMaps;
30
+ /**
31
+ * Cache of strings that have been deduped and stored in the data array.
32
+ */
33
+ private knownStrings;
34
+ /**
35
+ * Cache of reversed strings that have been deduped and stored in the data array.
36
+ * This is used to find matching suffixes.
37
+ */
38
+ private knownStringsRev;
39
+ private refUndefined;
40
+ constructor(value: Serializable | FlatpackedWrapper, options?: FlatpackOptions | undefined);
41
+ setValue(value: Serializable): void;
42
+ private nextId;
43
+ private addElement;
44
+ private addValueAndElement;
45
+ private primitiveToRef;
46
+ private createSubStringRef;
47
+ private addKnownString;
48
+ private addStringPrimitive;
49
+ private addStringElement;
50
+ private stringPrefix;
51
+ private stringSuffix;
52
+ private stringToRef;
53
+ private cvtSetToRef;
54
+ private dedupeSetRefs;
55
+ private createUniqueKeys;
56
+ private cvtMapToRef;
57
+ private dedupeMapRefs;
58
+ private cvtRegExpToRef;
59
+ private cvtDateToRef;
60
+ private cvtBigintToRef;
61
+ private cvtObjToRef;
62
+ private dedupeObject;
63
+ /**
64
+ *
65
+ * @param value - The array converted to an ArrayRefElement.
66
+ * @param element - the element to dedupe.
67
+ * @param cacheValue - Whether to cache the value. It is false when it is a dynamic array, like object keys,
68
+ * in that case, we want to dedupe the keys and values.
69
+ * @returns the element to use.
70
+ */
71
+ private dedupeArray;
72
+ /**
73
+ * Convert an array to an index.
74
+ * @param value - The array to convert to an index.
75
+ * @param cacheValue - Whether to cache the value.
76
+ * @returns the index of the array.
77
+ */
78
+ private arrToRef;
79
+ private valueToRef;
80
+ /**
81
+ * Reset things in a way that allows for reuse.
82
+ */
83
+ private softReset;
84
+ toJSON(): Flatpacked;
85
+ static fromJSON(data: Flatpacked): FlatpackStore;
86
+ static parse(content: string): FlatpackStore;
87
+ stringify(): string;
88
+ toValue(): Unpacked;
89
+ }
90
+ export declare function toJSON<V extends Serializable>(json: V, options?: FlatpackOptions): Flatpacked;
91
+ export declare function stringify(data: Unpacked, pretty?: boolean): string;
92
+ //# sourceMappingURL=Flatpack.d.mts.map
@@ -0,0 +1,560 @@
1
+ import { FlatpackedWrapper } from './flatpackUtil.mjs';
2
+ import { ArrayRefElement, BigIntRefElement, DateRefElement, isStringRefElements, MapRefElement, ObjectRefElement, ObjectWrapperRefElement, PrimitiveRefElement, RegExpRefElement, SetRefElement, StringConcatRefElement, StringPrimitiveRefElement, SubStringRefElement, } from './RefElements.mjs';
3
+ import { stringifyFlatpacked } from './stringify.mjs';
4
+ import { Trie } from './Trie.mjs';
5
+ import { blockSplitRegex, dataHeader } from './types.mjs';
6
+ import { fromJSON } from './unpack.mjs';
7
+ const collator = new Intl.Collator('en', {
8
+ usage: 'sort',
9
+ numeric: true,
10
+ sensitivity: 'variant',
11
+ caseFirst: 'upper',
12
+ ignorePunctuation: false,
13
+ });
14
+ const compare = collator.compare;
15
+ const forceStringPrimitives = false;
16
+ const minSubStringLen = 8;
17
+ const minSubStringSuffixLen = 16;
18
+ const useSuffix = true;
19
+ const maxCachedStringLen = 256;
20
+ export class FlatpackStore {
21
+ options;
22
+ knownElements = new Set();
23
+ assignedElements = new Map();
24
+ elements = [undefined];
25
+ root = undefined;
26
+ dedupe = true;
27
+ sortKeys = true;
28
+ emptyObjIdx = undefined;
29
+ ids = 0;
30
+ /**
31
+ * Cache of primitives and objects that have been added to the data.
32
+ */
33
+ cache = new Map();
34
+ /**
35
+ * Set of elements that have been referenced by other indexes.
36
+ */
37
+ referenced = new Set();
38
+ /**
39
+ * Cache of arrays that have been deduped.
40
+ * The key is a hash of the array elements as a function of the index of the element.
41
+ */
42
+ cachedArrays = new Map();
43
+ cachedObjects = new Map();
44
+ cachedSets = new Map();
45
+ cachedMaps = new Map();
46
+ /**
47
+ * Cache of strings that have been deduped and stored in the data array.
48
+ */
49
+ knownStrings = new Trie();
50
+ /**
51
+ * Cache of reversed strings that have been deduped and stored in the data array.
52
+ * This is used to find matching suffixes.
53
+ */
54
+ knownStringsRev = new Trie();
55
+ refUndefined;
56
+ constructor(value, options) {
57
+ this.options = options;
58
+ this.dedupe = options?.dedupe ?? true;
59
+ this.sortKeys = options?.sortKeys || this.dedupe;
60
+ this.refUndefined = this.addValueAndElement(undefined, new PrimitiveRefElement(undefined));
61
+ if (value instanceof FlatpackedWrapper) {
62
+ this.#fromWrapper(value);
63
+ }
64
+ else {
65
+ this.#setValue(value);
66
+ }
67
+ }
68
+ #fromWrapper(wrapper) {
69
+ this.elements = wrapper.toRefElements();
70
+ this.root = this.elements[1];
71
+ for (let i = 1; i < this.elements.length; i++) {
72
+ const element = this.elements[i];
73
+ if (!element)
74
+ continue;
75
+ this.knownElements.add(element);
76
+ if (element instanceof PrimitiveRefElement) {
77
+ this.addValueAndElement(element.value, element);
78
+ }
79
+ if (isStringRefElements(element)) {
80
+ this.addStringElement(element.value, element);
81
+ }
82
+ }
83
+ this.ids = this.elements.length;
84
+ this.#resolveRefs();
85
+ }
86
+ setValue(value) {
87
+ this.#setValue(value);
88
+ }
89
+ #setValue(value) {
90
+ this.softReset();
91
+ this.root = this.valueToRef(value);
92
+ this.#resolveRefs();
93
+ return this.root;
94
+ }
95
+ nextId() {
96
+ return this.ids++;
97
+ }
98
+ addElement(element) {
99
+ if (this.knownElements.has(element))
100
+ return element;
101
+ element.setId(this.nextId());
102
+ this.knownElements.add(element);
103
+ return element;
104
+ }
105
+ addValueAndElement(value, element, cache = true) {
106
+ this.addElement(element);
107
+ if (cache) {
108
+ this.cache.set(value, element);
109
+ }
110
+ return element;
111
+ }
112
+ primitiveToRef(value) {
113
+ if (typeof value === 'string')
114
+ return this.stringToRef(value);
115
+ if (typeof value === 'bigint')
116
+ return this.cvtBigintToRef(value);
117
+ const found = this.cache.get(value);
118
+ if (found !== undefined) {
119
+ return found;
120
+ }
121
+ return this.addValueAndElement(value, new PrimitiveRefElement(value));
122
+ }
123
+ createSubStringRef(baseString, value, offset) {
124
+ const found = this.cache.get(value);
125
+ if (found !== undefined) {
126
+ return found;
127
+ }
128
+ return this.addStringElement(value, new SubStringRefElement(baseString, value.length, offset));
129
+ }
130
+ addKnownString(ref, value) {
131
+ if (value.length >= minSubStringLen) {
132
+ this.knownStrings.add(prefix(value, maxCachedStringLen), ref);
133
+ const rev = reverse(suffix(value, maxCachedStringLen));
134
+ this.knownStringsRev.add(rev, ref);
135
+ }
136
+ }
137
+ addStringPrimitive(value) {
138
+ return this.addStringElement(value, new StringPrimitiveRefElement(value));
139
+ }
140
+ addStringElement(value, element) {
141
+ this.addKnownString(element, value);
142
+ return this.addValueAndElement(value, element);
143
+ }
144
+ stringPrefix(value) {
145
+ // if (value.length < maxCachedStringLen * 2) return undefined;
146
+ const trieFound = this.knownStrings.find(value);
147
+ if (!trieFound || !trieFound.data || trieFound.found.length < minSubStringLen) {
148
+ return undefined;
149
+ }
150
+ const { data: tData, found: subStr } = trieFound;
151
+ // assert(subStr === value.slice(0, subStr.length));
152
+ return this.createSubStringRef(tData, subStr);
153
+ }
154
+ stringSuffix(value) {
155
+ if (!useSuffix)
156
+ return undefined;
157
+ const rev = reverse(value);
158
+ const trieFound = this.knownStringsRev.find(rev);
159
+ if (!trieFound || !trieFound.data || trieFound.found.length < minSubStringSuffixLen) {
160
+ return undefined;
161
+ }
162
+ const { data: tData, found: subStr } = trieFound;
163
+ // assert(subStr === value.slice(0, subStr.length));
164
+ return this.createSubStringRef(tData, value.slice(-subStr.length), tData.length - subStr.length);
165
+ }
166
+ stringToRef(value) {
167
+ const found = this.cache.get(value);
168
+ if (found !== undefined) {
169
+ return found;
170
+ }
171
+ if (forceStringPrimitives || value.length < minSubStringLen || blockSplitRegex.test(value)) {
172
+ return this.addStringPrimitive(value);
173
+ }
174
+ const partialsPfx = [];
175
+ const partialsSfx = [];
176
+ let subStr = value;
177
+ while (subStr.length) {
178
+ const prefix = this.stringPrefix(subStr);
179
+ const suffix = this.stringSuffix(subStr);
180
+ if (!prefix && !suffix)
181
+ break;
182
+ if (prefix && prefix.length >= (suffix?.length || 0)) {
183
+ partialsPfx.push(prefix);
184
+ subStr = subStr.slice(prefix.length);
185
+ }
186
+ else {
187
+ const sfx = suffix;
188
+ partialsSfx.push(sfx);
189
+ subStr = subStr.slice(0, -sfx.length);
190
+ }
191
+ }
192
+ partialsSfx.reverse();
193
+ if (!partialsPfx.length && !partialsSfx.length) {
194
+ return this.addStringPrimitive(value);
195
+ }
196
+ if (subStr.length) {
197
+ partialsPfx.push(this.stringToRef(subStr));
198
+ }
199
+ const partials = [...partialsPfx, ...partialsSfx];
200
+ return this.addStringElement(value, partials.length === 1 ? partials[0] : new StringConcatRefElement(partials));
201
+ }
202
+ cvtSetToRef(value) {
203
+ const found = this.cache.get(value);
204
+ if (found !== undefined) {
205
+ this.referenced.add(found);
206
+ return found;
207
+ }
208
+ const element = this.addValueAndElement(value, new SetRefElement());
209
+ element.setValues(this.createUniqueKeys([...value]));
210
+ return this.dedupeSetRefs(value, element);
211
+ }
212
+ dedupeSetRefs(value, element) {
213
+ if (!this.dedupe)
214
+ return element;
215
+ const values = element.values();
216
+ const found = this.cachedSets.get(values);
217
+ if (!found) {
218
+ this.cachedSets.set(values, element);
219
+ return element;
220
+ }
221
+ if (this.referenced.has(element))
222
+ return element;
223
+ this.knownElements.delete(element);
224
+ this.cache.set(value, found);
225
+ return found;
226
+ }
227
+ createUniqueKeys(keys) {
228
+ const cacheValue = false;
229
+ let k = this.arrToRef(keys, cacheValue);
230
+ const uniqueKeys = new Set(k.valueRefs());
231
+ if (uniqueKeys.size !== keys.length) {
232
+ // one or more of the keys got deduped. We need to duplicate it.
233
+ uniqueKeys.clear();
234
+ const values = k.valueRefs().map((ref) => {
235
+ if (uniqueKeys.has(ref)) {
236
+ return this.addElement(ref.clone());
237
+ }
238
+ uniqueKeys.add(ref);
239
+ return ref;
240
+ });
241
+ k = this.addValueAndElement(keys, new ArrayRefElement(values), cacheValue);
242
+ }
243
+ return k;
244
+ }
245
+ cvtMapToRef(value) {
246
+ const found = this.cache.get(value);
247
+ if (found !== undefined) {
248
+ this.referenced.add(found);
249
+ return found;
250
+ }
251
+ const element = this.addValueAndElement(value, new MapRefElement());
252
+ element.setKeysAndValues(this.createUniqueKeys([...value.keys()]), this.arrToRef([...value.values()], false));
253
+ return this.dedupeMapRefs(value, element);
254
+ }
255
+ dedupeMapRefs(value, element) {
256
+ if (!this.dedupe)
257
+ return element;
258
+ const keys = element.keys();
259
+ const values = element.values();
260
+ let found = this.cachedMaps.get(keys);
261
+ if (!found) {
262
+ found = new Map();
263
+ found.set(values, element);
264
+ this.cachedMaps.set(keys, found);
265
+ return element;
266
+ }
267
+ const foundValue = found.get(values);
268
+ if (foundValue) {
269
+ if (this.referenced.has(element))
270
+ return element;
271
+ this.knownElements.delete(element);
272
+ this.cache.set(value, foundValue);
273
+ return foundValue;
274
+ }
275
+ found.set(values, element);
276
+ return element;
277
+ }
278
+ cvtRegExpToRef(value) {
279
+ const found = this.cache.get(value);
280
+ if (found !== undefined) {
281
+ return found;
282
+ }
283
+ return this.addValueAndElement(value, new RegExpRefElement(this.stringToRef(value.source), this.stringToRef(value.flags)));
284
+ }
285
+ cvtDateToRef(value) {
286
+ const found = this.cache.get(value);
287
+ if (found !== undefined) {
288
+ return found;
289
+ }
290
+ return this.addValueAndElement(value, new DateRefElement(value.getTime()));
291
+ }
292
+ cvtBigintToRef(value) {
293
+ const found = this.cache.get(value);
294
+ if (found !== undefined) {
295
+ return found;
296
+ }
297
+ const valElement = this.primitiveToRef(value <= Number.MAX_SAFE_INTEGER && value >= -Number.MAX_SAFE_INTEGER ? Number(value) : value.toString());
298
+ return this.addValueAndElement(value, new BigIntRefElement(valElement));
299
+ }
300
+ cvtObjToRef(value) {
301
+ const found = this.cache.get(value);
302
+ if (found !== undefined) {
303
+ this.referenced.add(found);
304
+ return found;
305
+ }
306
+ if (isObjectWrapper(value)) {
307
+ const element = this.addValueAndElement(value, new ObjectWrapperRefElement());
308
+ element.setValue(this.valueToRef(value.valueOf()));
309
+ return element;
310
+ }
311
+ const entries = Object.entries(value);
312
+ if (!entries.length) {
313
+ if (this.emptyObjIdx) {
314
+ return this.emptyObjIdx;
315
+ }
316
+ this.emptyObjIdx = this.addValueAndElement(value, new ObjectRefElement());
317
+ return this.emptyObjIdx;
318
+ }
319
+ if (this.sortKeys) {
320
+ entries.sort((a, b) => compare(a[0], b[0]));
321
+ }
322
+ const element = this.addValueAndElement(value, new ObjectRefElement());
323
+ const k = this.arrToRef(entries.map(([key]) => key), false);
324
+ const v = this.arrToRef(entries.map(([, value]) => value), false);
325
+ element.setKeysAndValues(k, v);
326
+ return this.dedupeObject(value, element);
327
+ }
328
+ dedupeObject(value, element) {
329
+ if (!this.dedupe)
330
+ return element;
331
+ const keys = element.keys();
332
+ const values = element.values();
333
+ let found = this.cachedObjects.get(keys);
334
+ if (!found) {
335
+ found = new Map();
336
+ found.set(values, element);
337
+ this.cachedObjects.set(keys, found);
338
+ return element;
339
+ }
340
+ const foundValue = found.get(values);
341
+ if (foundValue) {
342
+ if (this.referenced.has(element))
343
+ return element;
344
+ this.knownElements.delete(element);
345
+ this.cache.set(value, foundValue);
346
+ return foundValue;
347
+ }
348
+ found.set(values, element);
349
+ return element;
350
+ }
351
+ /**
352
+ *
353
+ * @param value - The array converted to an ArrayRefElement.
354
+ * @param element - the element to dedupe.
355
+ * @param cacheValue - Whether to cache the value. It is false when it is a dynamic array, like object keys,
356
+ * in that case, we want to dedupe the keys and values.
357
+ * @returns the element to use.
358
+ */
359
+ dedupeArray(value, element, cacheValue) {
360
+ if (cacheValue && !this.dedupe)
361
+ return element;
362
+ const indexHash = element.hash;
363
+ let cached = this.cachedArrays.get(indexHash);
364
+ if (!cached) {
365
+ cached = [];
366
+ this.cachedArrays.set(indexHash, cached);
367
+ }
368
+ const found = cached.find((entry) => element.isEqual(entry));
369
+ if (found) {
370
+ if (this.referenced.has(element))
371
+ return element;
372
+ this.knownElements.delete(element);
373
+ if (cacheValue || this.cache.has(value)) {
374
+ this.cache.set(value, found);
375
+ }
376
+ return found;
377
+ }
378
+ cached.push(element);
379
+ return element;
380
+ }
381
+ /**
382
+ * Convert an array to an index.
383
+ * @param value - The array to convert to an index.
384
+ * @param cacheValue - Whether to cache the value.
385
+ * @returns the index of the array.
386
+ */
387
+ arrToRef(value, cacheValue = true) {
388
+ const found = this.cache.get(value);
389
+ if (found !== undefined) {
390
+ this.referenced.add(found);
391
+ return found;
392
+ }
393
+ const element = this.addValueAndElement(value, new ArrayRefElement(), cacheValue);
394
+ element.setValues(value.map((v) => this.valueToRef(v)));
395
+ return this.dedupeArray(value, element, cacheValue);
396
+ }
397
+ valueToRef(value) {
398
+ if (value === null) {
399
+ return this.primitiveToRef(value);
400
+ }
401
+ if (typeof value === 'object') {
402
+ if (value instanceof Set) {
403
+ return this.cvtSetToRef(value);
404
+ }
405
+ if (value instanceof Map) {
406
+ return this.cvtMapToRef(value);
407
+ }
408
+ if (value instanceof RegExp) {
409
+ return this.cvtRegExpToRef(value);
410
+ }
411
+ if (Array.isArray(value)) {
412
+ return this.arrToRef(value);
413
+ }
414
+ if (value instanceof Date) {
415
+ return this.cvtDateToRef(value);
416
+ }
417
+ return this.cvtObjToRef(value);
418
+ }
419
+ return this.primitiveToRef(value);
420
+ }
421
+ /**
422
+ * Reset things in a way that allows for reuse.
423
+ */
424
+ softReset() {
425
+ this.referenced.clear();
426
+ if (this.root) {
427
+ const idx = this.assignedElements.get(this.root);
428
+ if (idx) {
429
+ this.elements[idx] = undefined;
430
+ this.assignedElements.delete(this.root);
431
+ }
432
+ }
433
+ this.root = undefined;
434
+ }
435
+ #resolveRefs() {
436
+ if (!this.root)
437
+ return;
438
+ const elements = this.elements;
439
+ const assigned = this.assignedElements;
440
+ const referenced = this.referenced;
441
+ const availableIndexes = [];
442
+ function addElement(ref) {
443
+ if (assigned.has(ref))
444
+ return false;
445
+ const emptyCell = availableIndexes.pop();
446
+ if (emptyCell) {
447
+ assigned.set(ref, emptyCell);
448
+ elements[emptyCell] = ref;
449
+ return true;
450
+ }
451
+ const i = elements.push(ref) - 1;
452
+ assigned.set(ref, i);
453
+ return true;
454
+ }
455
+ function walk(ref, action) {
456
+ function _walk(ref) {
457
+ if (!action(ref))
458
+ return;
459
+ const deps = ref.getDependencies();
460
+ if (!deps)
461
+ return;
462
+ for (const dep of deps) {
463
+ _walk(dep);
464
+ }
465
+ }
466
+ _walk(ref);
467
+ }
468
+ function calcReferences(root) {
469
+ referenced.clear();
470
+ walk(root, (ref) => {
471
+ if (referenced.has(ref))
472
+ return false;
473
+ referenced.add(ref);
474
+ return true;
475
+ });
476
+ }
477
+ function calcAvailableIndexes() {
478
+ availableIndexes.length = 0;
479
+ for (let i = 1; i < elements.length; i++) {
480
+ const ref = elements[i];
481
+ if (!ref || !referenced.has(ref)) {
482
+ availableIndexes.push(i);
483
+ if (ref) {
484
+ assigned.delete(ref);
485
+ }
486
+ elements[i] = undefined;
487
+ }
488
+ else {
489
+ assigned.set(ref, i);
490
+ }
491
+ }
492
+ availableIndexes.reverse();
493
+ }
494
+ function addElements(root) {
495
+ walk(root, addElement);
496
+ let i = elements.length - 1;
497
+ for (; i > 0 && elements[i] === undefined; i--) {
498
+ // empty
499
+ }
500
+ elements.length = i + 1;
501
+ }
502
+ calcReferences(this.root);
503
+ elements[0] = this.refUndefined;
504
+ assigned.set(this.refUndefined, 0);
505
+ calcAvailableIndexes();
506
+ addElements(this.root);
507
+ }
508
+ toJSON() {
509
+ const data = [dataHeader];
510
+ const idxLookup = this.assignedElements;
511
+ const lookup = (ref) => (ref && idxLookup.get(ref)) || 0;
512
+ const elements = this.elements;
513
+ for (let i = 1; i < elements.length; i++) {
514
+ const element = elements[i];
515
+ if (!element) {
516
+ data.push(0);
517
+ continue;
518
+ }
519
+ const value = element.toElement(lookup);
520
+ if (value === undefined)
521
+ continue;
522
+ data.push(value);
523
+ }
524
+ return data;
525
+ }
526
+ static fromJSON(data) {
527
+ return new FlatpackStore(new FlatpackedWrapper(data));
528
+ }
529
+ static parse(content) {
530
+ return new FlatpackStore(FlatpackedWrapper.parse(content));
531
+ }
532
+ stringify() {
533
+ return stringifyFlatpacked(this.toJSON());
534
+ }
535
+ toValue() {
536
+ return fromJSON(this.toJSON());
537
+ }
538
+ }
539
+ function isObjectWrapper(value) {
540
+ return (typeof value === 'object' &&
541
+ value !== null &&
542
+ typeof value.valueOf === 'function' &&
543
+ value.valueOf() !== value);
544
+ }
545
+ function prefix(value, len) {
546
+ return value.length > len ? [...value].slice(0, len) : value;
547
+ }
548
+ function suffix(value, len) {
549
+ return value.length > len ? [...value].slice(-len) : value;
550
+ }
551
+ function reverse(value) {
552
+ return [...value].reverse();
553
+ }
554
+ export function toJSON(json, options) {
555
+ return new FlatpackStore(json, options).toJSON();
556
+ }
557
+ export function stringify(data, pretty = true) {
558
+ return pretty ? stringifyFlatpacked(toJSON(data)) : JSON.stringify(toJSON(data));
559
+ }
560
+ //# sourceMappingURL=Flatpack.mjs.map