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 +21 -0
- package/README.md +17 -0
- package/dist/Flatpack.d.mts +92 -0
- package/dist/Flatpack.mjs +560 -0
- package/dist/RefElements.d.mts +139 -0
- package/dist/RefElements.mjs +385 -0
- package/dist/Trie.d.mts +18 -0
- package/dist/Trie.mjs +35 -0
- package/dist/flatpackUtil.d.mts +12 -0
- package/dist/flatpackUtil.mjs +95 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +5 -0
- package/dist/storage.d.mts +60 -0
- package/dist/storage.mjs +364 -0
- package/dist/stringify.d.mts +3 -0
- package/dist/stringify.mjs +13 -0
- package/dist/types.d.mts +99 -0
- package/dist/types.mjs +15 -0
- package/dist/unpack.d.mts +4 -0
- package/dist/unpack.mjs +163 -0
- package/package.json +59 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { Flatpacked, FlatpackOptions, Serializable, Unpacked } from './types.mjs';
|
|
2
|
+
export declare class CompactStorage {
|
|
3
|
+
readonly options?: FlatpackOptions | undefined;
|
|
4
|
+
private data;
|
|
5
|
+
private dedupe;
|
|
6
|
+
private sortKeys;
|
|
7
|
+
private emptyObjIdx;
|
|
8
|
+
/**
|
|
9
|
+
* Cache of primitives and objects that have been added to the data.
|
|
10
|
+
*/
|
|
11
|
+
private cache;
|
|
12
|
+
/**
|
|
13
|
+
* Set of indexes that have been referenced by other indexes.
|
|
14
|
+
*/
|
|
15
|
+
private referenced;
|
|
16
|
+
/**
|
|
17
|
+
* Cache of arrays that have been deduped.
|
|
18
|
+
* The key is a hash of the array elements as a function of the index of the element.
|
|
19
|
+
*/
|
|
20
|
+
private cachedArrays;
|
|
21
|
+
/**
|
|
22
|
+
* Cache of strings that have been deduped and stored in the data array.
|
|
23
|
+
*/
|
|
24
|
+
private knownStrings;
|
|
25
|
+
private cachedElements;
|
|
26
|
+
constructor(options?: FlatpackOptions | undefined);
|
|
27
|
+
private primitiveToIdx;
|
|
28
|
+
private addSubStringRef;
|
|
29
|
+
private addKnownString;
|
|
30
|
+
private addStringPrimitive;
|
|
31
|
+
private duplicateIndex;
|
|
32
|
+
private stringToIdx;
|
|
33
|
+
private objSetToIdx;
|
|
34
|
+
private createUniqueKeys;
|
|
35
|
+
private objMapToIdx;
|
|
36
|
+
private objRegExpToIdx;
|
|
37
|
+
private objDateToIdx;
|
|
38
|
+
private bigintToIdx;
|
|
39
|
+
private objToIdx;
|
|
40
|
+
private storeElement;
|
|
41
|
+
private cacheElement;
|
|
42
|
+
private stashArray;
|
|
43
|
+
private createArrayElementFromIndexValues;
|
|
44
|
+
/**
|
|
45
|
+
* Convert an array to an index.
|
|
46
|
+
* @param value - The array to convert to an index.
|
|
47
|
+
* @param cacheValue - Whether to cache the value.
|
|
48
|
+
* @returns the index of the array.
|
|
49
|
+
*/
|
|
50
|
+
private arrToIdx;
|
|
51
|
+
private valueToIdx;
|
|
52
|
+
/**
|
|
53
|
+
* Reset things in a way that allows for reuse.
|
|
54
|
+
*/
|
|
55
|
+
private softReset;
|
|
56
|
+
toJSON<V extends Serializable>(json: V): Flatpacked;
|
|
57
|
+
}
|
|
58
|
+
export declare function toJSON<V extends Serializable>(json: V, options?: FlatpackOptions): Flatpacked;
|
|
59
|
+
export declare function stringify(data: Unpacked, pretty?: boolean): string;
|
|
60
|
+
//# sourceMappingURL=storage.d.mts.map
|
package/dist/storage.mjs
ADDED
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
import assert from 'node:assert';
|
|
2
|
+
import { stringifyFlatpacked } from './stringify.mjs';
|
|
3
|
+
import { Trie } from './Trie.mjs';
|
|
4
|
+
import { blockSplitRegex, dataHeader, ElementType } from './types.mjs';
|
|
5
|
+
const collator = new Intl.Collator('en', {
|
|
6
|
+
usage: 'sort',
|
|
7
|
+
numeric: true,
|
|
8
|
+
sensitivity: 'variant',
|
|
9
|
+
caseFirst: 'upper',
|
|
10
|
+
ignorePunctuation: false,
|
|
11
|
+
});
|
|
12
|
+
const compare = collator.compare;
|
|
13
|
+
const forceStringPrimitives = false;
|
|
14
|
+
const minSubStringLen = 4;
|
|
15
|
+
export class CompactStorage {
|
|
16
|
+
options;
|
|
17
|
+
data = [dataHeader];
|
|
18
|
+
dedupe = true;
|
|
19
|
+
sortKeys = true;
|
|
20
|
+
emptyObjIdx = 0;
|
|
21
|
+
/**
|
|
22
|
+
* Cache of primitives and objects that have been added to the data.
|
|
23
|
+
*/
|
|
24
|
+
cache = new Map([[undefined, 0]]);
|
|
25
|
+
/**
|
|
26
|
+
* Set of indexes that have been referenced by other indexes.
|
|
27
|
+
*/
|
|
28
|
+
referenced = new Set();
|
|
29
|
+
/**
|
|
30
|
+
* Cache of arrays that have been deduped.
|
|
31
|
+
* The key is a hash of the array elements as a function of the index of the element.
|
|
32
|
+
*/
|
|
33
|
+
cachedArrays = new Map();
|
|
34
|
+
/**
|
|
35
|
+
* Cache of strings that have been deduped and stored in the data array.
|
|
36
|
+
*/
|
|
37
|
+
knownStrings = new Trie();
|
|
38
|
+
cachedElements = new Map();
|
|
39
|
+
constructor(options) {
|
|
40
|
+
this.options = options;
|
|
41
|
+
this.dedupe = options?.dedupe ?? true;
|
|
42
|
+
this.sortKeys = options?.sortKeys || this.dedupe;
|
|
43
|
+
}
|
|
44
|
+
primitiveToIdx(value) {
|
|
45
|
+
if (typeof value === 'string')
|
|
46
|
+
return this.stringToIdx(value);
|
|
47
|
+
if (typeof value === 'bigint')
|
|
48
|
+
return this.bigintToIdx(value);
|
|
49
|
+
const found = this.cache.get(value);
|
|
50
|
+
if (found !== undefined) {
|
|
51
|
+
return found;
|
|
52
|
+
}
|
|
53
|
+
const idx = this.data.push(value) - 1;
|
|
54
|
+
this.cache.set(value, idx);
|
|
55
|
+
return idx;
|
|
56
|
+
}
|
|
57
|
+
addSubStringRef(idxString, value, offset) {
|
|
58
|
+
const found = this.cache.get(value);
|
|
59
|
+
if (found !== undefined) {
|
|
60
|
+
return found;
|
|
61
|
+
}
|
|
62
|
+
const sub = offset
|
|
63
|
+
? [ElementType.SubString, idxString, value.length, offset]
|
|
64
|
+
: [ElementType.SubString, idxString, value.length];
|
|
65
|
+
const idx = this.data.push(sub) - 1;
|
|
66
|
+
this.cache.set(value, idx);
|
|
67
|
+
return idx;
|
|
68
|
+
}
|
|
69
|
+
addKnownString(idx, value) {
|
|
70
|
+
if (value.length >= minSubStringLen) {
|
|
71
|
+
this.knownStrings.add(value.length > 256 ? value.slice(0, 256) : value, { idx });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
addStringPrimitive(value) {
|
|
75
|
+
const idx = this.data.push(value) - 1;
|
|
76
|
+
this.addKnownString(idx, value);
|
|
77
|
+
this.cache.set(value, idx);
|
|
78
|
+
return idx;
|
|
79
|
+
}
|
|
80
|
+
duplicateIndex(idx) {
|
|
81
|
+
const element = this.data[idx];
|
|
82
|
+
const duplicate = this.data.push(element) - 1;
|
|
83
|
+
return duplicate;
|
|
84
|
+
}
|
|
85
|
+
stringToIdx(value) {
|
|
86
|
+
const found = this.cache.get(value);
|
|
87
|
+
if (found !== undefined) {
|
|
88
|
+
return found;
|
|
89
|
+
}
|
|
90
|
+
if (forceStringPrimitives || value.length < minSubStringLen || blockSplitRegex.test(value)) {
|
|
91
|
+
return this.addStringPrimitive(value);
|
|
92
|
+
}
|
|
93
|
+
const trieFound = this.knownStrings.find(value);
|
|
94
|
+
if (!trieFound || !trieFound.data || trieFound.found.length < minSubStringLen) {
|
|
95
|
+
return this.addStringPrimitive(value);
|
|
96
|
+
}
|
|
97
|
+
const { data: tData, found: subStr } = trieFound;
|
|
98
|
+
const sIdx = this.addSubStringRef(tData.idx, subStr, tData.offset);
|
|
99
|
+
if (subStr === value)
|
|
100
|
+
return sIdx;
|
|
101
|
+
const v = [sIdx, this.stringToIdx(value.slice(subStr.length))];
|
|
102
|
+
const idx = this.data.push([ElementType.String, ...v]) - 1;
|
|
103
|
+
this.cache.set(value, idx);
|
|
104
|
+
this.addKnownString(idx, value);
|
|
105
|
+
return idx;
|
|
106
|
+
}
|
|
107
|
+
objSetToIdx(value) {
|
|
108
|
+
const found = this.cache.get(value);
|
|
109
|
+
if (found !== undefined) {
|
|
110
|
+
this.referenced.add(found);
|
|
111
|
+
return found;
|
|
112
|
+
}
|
|
113
|
+
const idx = this.data.push(0) - 1;
|
|
114
|
+
this.cache.set(value, idx);
|
|
115
|
+
const keys = [...value];
|
|
116
|
+
const k = this.createUniqueKeys(keys, false);
|
|
117
|
+
const element = [ElementType.Set, k];
|
|
118
|
+
return this.storeElement(value, idx, element);
|
|
119
|
+
}
|
|
120
|
+
createUniqueKeys(keys, cacheValue = true) {
|
|
121
|
+
let k = this.arrToIdx(keys, cacheValue);
|
|
122
|
+
const elementKeys = this.data[k];
|
|
123
|
+
const uniqueKeys = new Set(elementKeys.slice(1));
|
|
124
|
+
if (uniqueKeys.size !== keys.length) {
|
|
125
|
+
// one or more of the keys got deduped. We need to duplicate it.
|
|
126
|
+
uniqueKeys.clear();
|
|
127
|
+
const indexes = elementKeys.slice(1).map((idx) => {
|
|
128
|
+
if (uniqueKeys.has(idx)) {
|
|
129
|
+
return this.duplicateIndex(idx);
|
|
130
|
+
}
|
|
131
|
+
uniqueKeys.add(idx);
|
|
132
|
+
return idx;
|
|
133
|
+
});
|
|
134
|
+
k = this.createArrayElementFromIndexValues(this.data.length, indexes);
|
|
135
|
+
}
|
|
136
|
+
return k;
|
|
137
|
+
}
|
|
138
|
+
objMapToIdx(value) {
|
|
139
|
+
const found = this.cache.get(value);
|
|
140
|
+
if (found !== undefined) {
|
|
141
|
+
this.referenced.add(found);
|
|
142
|
+
return found;
|
|
143
|
+
}
|
|
144
|
+
const idx = this.data.push(0) - 1;
|
|
145
|
+
this.cache.set(value, idx);
|
|
146
|
+
const entries = [...value.entries()];
|
|
147
|
+
const k = this.createUniqueKeys(entries.map(([key]) => key), false);
|
|
148
|
+
const v = this.arrToIdx(entries.map(([, value]) => value), false);
|
|
149
|
+
const element = [ElementType.Map, k, v];
|
|
150
|
+
return this.storeElement(value, idx, element);
|
|
151
|
+
}
|
|
152
|
+
objRegExpToIdx(value) {
|
|
153
|
+
const found = this.cache.get(value);
|
|
154
|
+
if (found !== undefined) {
|
|
155
|
+
return found;
|
|
156
|
+
}
|
|
157
|
+
const idx = this.data.push(0) - 1;
|
|
158
|
+
this.cache.set(value, idx);
|
|
159
|
+
const element = [
|
|
160
|
+
ElementType.RegExp,
|
|
161
|
+
this.stringToIdx(value.source),
|
|
162
|
+
this.stringToIdx(value.flags),
|
|
163
|
+
];
|
|
164
|
+
return this.storeElement(value, idx, element);
|
|
165
|
+
}
|
|
166
|
+
objDateToIdx(value) {
|
|
167
|
+
const found = this.cache.get(value);
|
|
168
|
+
if (found !== undefined) {
|
|
169
|
+
return found;
|
|
170
|
+
}
|
|
171
|
+
const idx = this.data.push(0) - 1;
|
|
172
|
+
this.cache.set(value, idx);
|
|
173
|
+
const element = [ElementType.Date, value.getTime()];
|
|
174
|
+
return this.storeElement(value, idx, element);
|
|
175
|
+
}
|
|
176
|
+
bigintToIdx(value) {
|
|
177
|
+
const found = this.cache.get(value);
|
|
178
|
+
if (found !== undefined) {
|
|
179
|
+
return found;
|
|
180
|
+
}
|
|
181
|
+
const idx = this.data.push(0) - 1;
|
|
182
|
+
this.cache.set(value, idx);
|
|
183
|
+
const element = [
|
|
184
|
+
ElementType.BigInt,
|
|
185
|
+
this.primitiveToIdx(value <= Number.MAX_SAFE_INTEGER && value >= -Number.MAX_SAFE_INTEGER
|
|
186
|
+
? Number(value)
|
|
187
|
+
: value.toString()),
|
|
188
|
+
];
|
|
189
|
+
return this.storeElement(value, idx, element);
|
|
190
|
+
}
|
|
191
|
+
objToIdx(value) {
|
|
192
|
+
const found = this.cache.get(value);
|
|
193
|
+
if (found !== undefined) {
|
|
194
|
+
this.referenced.add(found);
|
|
195
|
+
return found;
|
|
196
|
+
}
|
|
197
|
+
if (isObjectWrapper(value)) {
|
|
198
|
+
const idx = this.data.push({}) - 1;
|
|
199
|
+
this.cache.set(value, idx);
|
|
200
|
+
const element = [ElementType.Object, 0, this.valueToIdx(value.valueOf())];
|
|
201
|
+
return this.storeElement(value, idx, element);
|
|
202
|
+
}
|
|
203
|
+
const entries = Object.entries(value);
|
|
204
|
+
if (!entries.length) {
|
|
205
|
+
if (this.emptyObjIdx) {
|
|
206
|
+
return this.emptyObjIdx;
|
|
207
|
+
}
|
|
208
|
+
const idx = this.data.push({}) - 1;
|
|
209
|
+
this.emptyObjIdx = idx;
|
|
210
|
+
return idx;
|
|
211
|
+
}
|
|
212
|
+
const idx = this.data.push(0) - 1;
|
|
213
|
+
this.cache.set(value, idx);
|
|
214
|
+
if (this.sortKeys) {
|
|
215
|
+
entries.sort(([a], [b]) => compare(a, b));
|
|
216
|
+
}
|
|
217
|
+
const k = this.arrToIdx(entries.map(([key]) => key));
|
|
218
|
+
const v = this.arrToIdx(entries.map(([, value]) => value));
|
|
219
|
+
const element = [ElementType.Object, k, v];
|
|
220
|
+
return this.storeElement(value, idx, element);
|
|
221
|
+
}
|
|
222
|
+
storeElement(value, idx, element) {
|
|
223
|
+
const useIdx = this.dedupe ? this.cacheElement(idx, element) : idx;
|
|
224
|
+
if (useIdx !== idx && idx === this.data.length - 1) {
|
|
225
|
+
this.data.length = idx;
|
|
226
|
+
this.cache.set(value, useIdx);
|
|
227
|
+
return useIdx;
|
|
228
|
+
}
|
|
229
|
+
this.data[idx] = element;
|
|
230
|
+
return idx;
|
|
231
|
+
}
|
|
232
|
+
cacheElement(elemIdx, element) {
|
|
233
|
+
let map = this.cachedElements;
|
|
234
|
+
for (let i = 0; i < element.length - 1; i++) {
|
|
235
|
+
const idx = element[i];
|
|
236
|
+
let found = map.get(idx);
|
|
237
|
+
if (!found) {
|
|
238
|
+
found = new Map();
|
|
239
|
+
map.set(idx, found);
|
|
240
|
+
}
|
|
241
|
+
assert(found instanceof Map);
|
|
242
|
+
map = found;
|
|
243
|
+
}
|
|
244
|
+
const idx = element[element.length - 1];
|
|
245
|
+
const foundIdx = map.get(idx);
|
|
246
|
+
if (typeof foundIdx === 'number') {
|
|
247
|
+
return this.referenced.has(elemIdx) ? elemIdx : foundIdx;
|
|
248
|
+
}
|
|
249
|
+
map.set(idx, elemIdx);
|
|
250
|
+
return elemIdx;
|
|
251
|
+
}
|
|
252
|
+
stashArray(idx, element) {
|
|
253
|
+
const indexHash = simpleHash(element);
|
|
254
|
+
let found = this.cachedArrays.get(indexHash);
|
|
255
|
+
if (!found) {
|
|
256
|
+
found = [];
|
|
257
|
+
this.cachedArrays.set(indexHash, found);
|
|
258
|
+
}
|
|
259
|
+
const foundIdx = found.find((entry) => isEqual(entry.v, element));
|
|
260
|
+
if (foundIdx) {
|
|
261
|
+
return this.referenced.has(idx) ? idx : foundIdx.idx;
|
|
262
|
+
}
|
|
263
|
+
found.push({ idx, v: element });
|
|
264
|
+
return idx;
|
|
265
|
+
}
|
|
266
|
+
createArrayElementFromIndexValues(idx, indexValues) {
|
|
267
|
+
const element = [ElementType.Array, ...indexValues];
|
|
268
|
+
const useIdx = this.dedupe ? this.stashArray(idx, element) : idx;
|
|
269
|
+
if (useIdx !== idx) {
|
|
270
|
+
assert(this.data.length == idx + 1, `Expected ${idx + 1} but got ${this.data.length}`);
|
|
271
|
+
this.data.length = idx;
|
|
272
|
+
return useIdx;
|
|
273
|
+
}
|
|
274
|
+
this.data[idx] = element;
|
|
275
|
+
return idx;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Convert an array to an index.
|
|
279
|
+
* @param value - The array to convert to an index.
|
|
280
|
+
* @param cacheValue - Whether to cache the value.
|
|
281
|
+
* @returns the index of the array.
|
|
282
|
+
*/
|
|
283
|
+
arrToIdx(value, cacheValue = true) {
|
|
284
|
+
const found = this.cache.get(value);
|
|
285
|
+
if (found !== undefined) {
|
|
286
|
+
this.referenced.add(found);
|
|
287
|
+
return found;
|
|
288
|
+
}
|
|
289
|
+
const idx = this.data.push(0) - 1;
|
|
290
|
+
this.cache.set(value, idx);
|
|
291
|
+
const useIdx = this.createArrayElementFromIndexValues(idx, value.map((idx) => this.valueToIdx(idx)));
|
|
292
|
+
if (cacheValue) {
|
|
293
|
+
this.cache.set(value, useIdx);
|
|
294
|
+
}
|
|
295
|
+
return useIdx;
|
|
296
|
+
}
|
|
297
|
+
valueToIdx(value) {
|
|
298
|
+
if (value === null) {
|
|
299
|
+
return this.primitiveToIdx(value);
|
|
300
|
+
}
|
|
301
|
+
if (typeof value === 'object') {
|
|
302
|
+
if (value instanceof Set) {
|
|
303
|
+
return this.objSetToIdx(value);
|
|
304
|
+
}
|
|
305
|
+
if (value instanceof Map) {
|
|
306
|
+
return this.objMapToIdx(value);
|
|
307
|
+
}
|
|
308
|
+
if (value instanceof RegExp) {
|
|
309
|
+
return this.objRegExpToIdx(value);
|
|
310
|
+
}
|
|
311
|
+
if (Array.isArray(value)) {
|
|
312
|
+
return this.arrToIdx(value);
|
|
313
|
+
}
|
|
314
|
+
if (value instanceof Date) {
|
|
315
|
+
return this.objDateToIdx(value);
|
|
316
|
+
}
|
|
317
|
+
return this.objToIdx(value);
|
|
318
|
+
}
|
|
319
|
+
return this.primitiveToIdx(value);
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Reset things in a way that allows for reuse.
|
|
323
|
+
*/
|
|
324
|
+
softReset() {
|
|
325
|
+
this.cache.clear();
|
|
326
|
+
this.cache.set(undefined, 0);
|
|
327
|
+
}
|
|
328
|
+
toJSON(json) {
|
|
329
|
+
this.softReset();
|
|
330
|
+
this.valueToIdx(json);
|
|
331
|
+
return this.data;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
function isEqual(a, b) {
|
|
335
|
+
if (a === b)
|
|
336
|
+
return true;
|
|
337
|
+
if (a.length !== b.length)
|
|
338
|
+
return false;
|
|
339
|
+
for (let i = 0; i < a.length; i++) {
|
|
340
|
+
if (a[i] !== b[i])
|
|
341
|
+
return false;
|
|
342
|
+
}
|
|
343
|
+
return true;
|
|
344
|
+
}
|
|
345
|
+
function simpleHash(values) {
|
|
346
|
+
let hash = Math.sqrt(values.length);
|
|
347
|
+
for (const value of values) {
|
|
348
|
+
hash += value * value;
|
|
349
|
+
}
|
|
350
|
+
return hash;
|
|
351
|
+
}
|
|
352
|
+
function isObjectWrapper(value) {
|
|
353
|
+
return (typeof value === 'object' &&
|
|
354
|
+
value !== null &&
|
|
355
|
+
typeof value.valueOf === 'function' &&
|
|
356
|
+
value.valueOf() !== value);
|
|
357
|
+
}
|
|
358
|
+
export function toJSON(json, options) {
|
|
359
|
+
return new CompactStorage(options).toJSON(json);
|
|
360
|
+
}
|
|
361
|
+
export function stringify(data, pretty = true) {
|
|
362
|
+
return pretty ? stringifyFlatpacked(toJSON(data)) : JSON.stringify(toJSON(data));
|
|
363
|
+
}
|
|
364
|
+
//# sourceMappingURL=storage.mjs.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function stringifyFlatpacked(input) {
|
|
2
|
+
let result = '[\n' + JSON.stringify(input[0]);
|
|
3
|
+
let prev = '';
|
|
4
|
+
for (let i = 1; i < input.length; i++) {
|
|
5
|
+
const next = JSON.stringify(input[i]);
|
|
6
|
+
result += prev == next ? ',' : ',\n';
|
|
7
|
+
result += next;
|
|
8
|
+
prev = next;
|
|
9
|
+
}
|
|
10
|
+
result += '\n]\n';
|
|
11
|
+
return result;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=stringify.mjs.map
|
package/dist/types.d.mts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
export type SimplePrimitive = string | number | boolean | null | undefined;
|
|
2
|
+
export type Primitive = SimplePrimitive | RegExp | Date | bigint;
|
|
3
|
+
export type PrimitiveSet = Set<Primitive | PrimitiveObject | PrimitiveArray | PrimitiveSet | PrimitiveMap>;
|
|
4
|
+
export type PrimitiveMap = Map<Primitive | PrimitiveObject | PrimitiveArray | PrimitiveSet | PrimitiveMap, Primitive | PrimitiveObject | PrimitiveArray | PrimitiveSet | PrimitiveMap>;
|
|
5
|
+
export interface PrimitiveObject {
|
|
6
|
+
readonly [key: string]: Primitive | PrimitiveObject | PrimitiveArray | PrimitiveSet | PrimitiveMap;
|
|
7
|
+
}
|
|
8
|
+
export interface ObjectWrapper {
|
|
9
|
+
valueOf(): PrimitiveObject;
|
|
10
|
+
}
|
|
11
|
+
export type PrimitiveArray = readonly (Primitive | PrimitiveObject | PrimitiveArray | PrimitiveSet | PrimitiveMap)[];
|
|
12
|
+
type PrimitiveElement = Primitive;
|
|
13
|
+
export type Serializable = Primitive | PrimitiveObject | PrimitiveArray | PrimitiveSet | PrimitiveMap;
|
|
14
|
+
export declare enum ElementType {
|
|
15
|
+
Array = 0,
|
|
16
|
+
Object = 1,
|
|
17
|
+
String = 2,
|
|
18
|
+
SubString = 3,
|
|
19
|
+
Set = 4,
|
|
20
|
+
Map = 5,
|
|
21
|
+
RegExp = 6,
|
|
22
|
+
Date = 7,
|
|
23
|
+
BigInt = 8
|
|
24
|
+
}
|
|
25
|
+
interface EmptyObject {
|
|
26
|
+
readonly t?: ElementType.Object;
|
|
27
|
+
}
|
|
28
|
+
type ObjectBasedElements = EmptyObject;
|
|
29
|
+
/**
|
|
30
|
+
* Non-primitive elements.
|
|
31
|
+
* An array is used to represent the element.
|
|
32
|
+
* The first element is the type of the element.
|
|
33
|
+
*/
|
|
34
|
+
export type ArrayBasedElements = ArrayElement | BigIntElement | DateElement | MapElement | ObjectElement | ObjectWrapperElement | RegExpElement | SetElement | StringElement | SubStringElement;
|
|
35
|
+
export type Index = number;
|
|
36
|
+
/**
|
|
37
|
+
* A Compound string element. Each index is a reference to a string element that is concatenated
|
|
38
|
+
* to form the final string.
|
|
39
|
+
*/
|
|
40
|
+
export type StringElement = readonly [type: ElementType.String, ...Index[]];
|
|
41
|
+
/**
|
|
42
|
+
* A substring element. The first index is a reference to a string element.
|
|
43
|
+
* The second index is the length of the substring.
|
|
44
|
+
* The third index is the offset of the substring, defaults to 0.
|
|
45
|
+
*/
|
|
46
|
+
export type SubStringElement = readonly [type: ElementType.SubString, idx: Index, len: number, offset?: number];
|
|
47
|
+
/**
|
|
48
|
+
* An object element. The first index is a reference to an array of keys.
|
|
49
|
+
* The second index is a reference to an array of values.
|
|
50
|
+
*/
|
|
51
|
+
export type ObjectElement = readonly [type: ElementType.Object, keys: Index, values: Index];
|
|
52
|
+
/**
|
|
53
|
+
* A Object wrapper element.
|
|
54
|
+
*/
|
|
55
|
+
export type ObjectWrapperElement = readonly [type: ElementType.Object, keys: 0, values: Index];
|
|
56
|
+
/**
|
|
57
|
+
* A set element. The first index is a reference to an array of keys.
|
|
58
|
+
*/
|
|
59
|
+
export type SetElement = readonly [type: ElementType.Set, keys: Index];
|
|
60
|
+
/**
|
|
61
|
+
* A map element. The first index is a reference to an array of keys.
|
|
62
|
+
* The second index is a reference to an array of values.
|
|
63
|
+
*/
|
|
64
|
+
export type MapElement = readonly [type: ElementType.Map, keys: Index, values: Index];
|
|
65
|
+
/**
|
|
66
|
+
* A regular expression element. The first index is a reference to a string element that represents the pattern.
|
|
67
|
+
* The second index is a reference to a string element that represents the flags.
|
|
68
|
+
*/
|
|
69
|
+
export type RegExpElement = readonly [type: ElementType.RegExp, pattern: Index, flags: Index];
|
|
70
|
+
/**
|
|
71
|
+
* A date element. The first index is the number of milliseconds since the epoch.
|
|
72
|
+
*/
|
|
73
|
+
export type DateElement = readonly [type: ElementType.Date, value: number];
|
|
74
|
+
export type BigIntElement = readonly [type: ElementType.BigInt, value: Index];
|
|
75
|
+
/**
|
|
76
|
+
* An array element. Each index is a reference to an element.
|
|
77
|
+
*/
|
|
78
|
+
export type ArrayElement = readonly [type: ElementType.Array, ...Index[]];
|
|
79
|
+
export type FlattenedElement = Readonly<PrimitiveElement | ObjectBasedElements | ArrayBasedElements>;
|
|
80
|
+
type Header = string;
|
|
81
|
+
export type Flatpacked = [Header, ...FlattenedElement[]];
|
|
82
|
+
export type Unpacked = Readonly<Serializable>;
|
|
83
|
+
export declare const blockSplitRegex: RegExp;
|
|
84
|
+
export interface FlatpackOptions {
|
|
85
|
+
/**
|
|
86
|
+
* Sort keys in objects.
|
|
87
|
+
* Does not affect arrays, sets, or maps.
|
|
88
|
+
*/
|
|
89
|
+
sortKeys?: boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Dedupe objects and arrays.
|
|
92
|
+
* Implies {@linkcode sortKeys}.
|
|
93
|
+
* @default true
|
|
94
|
+
*/
|
|
95
|
+
dedupe?: boolean;
|
|
96
|
+
}
|
|
97
|
+
export declare const dataHeader: "Dehydrated JSON v1";
|
|
98
|
+
export {};
|
|
99
|
+
//# sourceMappingURL=types.d.mts.map
|
package/dist/types.mjs
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export var ElementType;
|
|
2
|
+
(function (ElementType) {
|
|
3
|
+
ElementType[ElementType["Array"] = 0] = "Array";
|
|
4
|
+
ElementType[ElementType["Object"] = 1] = "Object";
|
|
5
|
+
ElementType[ElementType["String"] = 2] = "String";
|
|
6
|
+
ElementType[ElementType["SubString"] = 3] = "SubString";
|
|
7
|
+
ElementType[ElementType["Set"] = 4] = "Set";
|
|
8
|
+
ElementType[ElementType["Map"] = 5] = "Map";
|
|
9
|
+
ElementType[ElementType["RegExp"] = 6] = "RegExp";
|
|
10
|
+
ElementType[ElementType["Date"] = 7] = "Date";
|
|
11
|
+
ElementType[ElementType["BigInt"] = 8] = "BigInt";
|
|
12
|
+
})(ElementType || (ElementType = {}));
|
|
13
|
+
export const blockSplitRegex = /^sha\d/;
|
|
14
|
+
export const dataHeader = 'Dehydrated JSON v1';
|
|
15
|
+
//# sourceMappingURL=types.mjs.map
|