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.
@@ -0,0 +1,163 @@
1
+ import assert from 'node:assert';
2
+ import { dataHeader, ElementType, } from './types.mjs';
3
+ export function fromJSON(data) {
4
+ const [header] = data;
5
+ if (header !== dataHeader) {
6
+ throw new Error('Invalid header');
7
+ }
8
+ const cache = new Map([[0, undefined]]);
9
+ /**
10
+ * indexes that have been referenced by other objects.
11
+ */
12
+ const referenced = new Set();
13
+ function mergeKeysValues(keys, values) {
14
+ return keys.map((key, i) => [key, values[i]]);
15
+ }
16
+ function toSet(idx, elem) {
17
+ const [_, k] = elem;
18
+ const s = k ? new Set(idxToArr(k)) : new Set();
19
+ cache.set(idx, s);
20
+ return s;
21
+ }
22
+ function toMap(idx, elem) {
23
+ const [_, k, v] = elem;
24
+ const m = !k || !v ? new Map() : new Map(mergeKeysValues(idxToArr(k), idxToArr(v)));
25
+ cache.set(idx, m);
26
+ return m;
27
+ }
28
+ function toRegExp(idx, elem) {
29
+ const [_, pattern, flags] = elem;
30
+ const p = idxToValue(pattern);
31
+ const f = idxToValue(flags);
32
+ const r = new RegExp(p, f);
33
+ cache.set(idx, r);
34
+ return r;
35
+ }
36
+ function toBigInt(idx, elem) {
37
+ const [_, vIdx] = elem;
38
+ const r = BigInt(idxToValue(vIdx));
39
+ cache.set(idx, r);
40
+ return r;
41
+ }
42
+ function toDate(idx, elem) {
43
+ const [_, value] = elem;
44
+ const r = new Date(value);
45
+ cache.set(idx, r);
46
+ return r;
47
+ }
48
+ function toString(idx, elem) {
49
+ const s = typeof elem === 'string' ? elem : idxToValue(elem.slice(1));
50
+ cache.set(idx, s);
51
+ return s;
52
+ }
53
+ function toObj(idx, elem) {
54
+ const [_, k, v] = elem;
55
+ // Object Wrapper
56
+ if (!k && v) {
57
+ const obj = Object(idxToValue(v));
58
+ cache.set(idx, obj);
59
+ return obj;
60
+ }
61
+ const obj = {};
62
+ cache.set(idx, obj);
63
+ if (!k || !v)
64
+ return obj;
65
+ const keys = idxToArr(k);
66
+ const values = idxToArr(v);
67
+ Object.assign(obj, Object.fromEntries(mergeKeysValues(keys, values)));
68
+ return obj;
69
+ }
70
+ function idxToArr(idx) {
71
+ const element = data[idx];
72
+ assert(isArrayElement(element));
73
+ return toArr(idx, element);
74
+ }
75
+ function toArr(idx, element) {
76
+ const placeHolder = [];
77
+ const refs = element.slice(1);
78
+ cache.set(idx, placeHolder);
79
+ const arr = refs.map(idxToValue);
80
+ // check if the array has been referenced by another object.
81
+ if (!referenced.has(idx)) {
82
+ // It has not, just replace the placeholder with the array.
83
+ cache.set(idx, arr);
84
+ return arr;
85
+ }
86
+ placeHolder.push(...arr);
87
+ return placeHolder;
88
+ }
89
+ function handleSubStringElement(idx, refs) {
90
+ const [_t, sIdx, len, offset = 0] = refs;
91
+ const s = `${idxToValue(sIdx)}`.slice(offset, offset + len);
92
+ cache.set(idx, s);
93
+ return s;
94
+ }
95
+ function handleArrayElement(idx, element) {
96
+ switch (element[0]) {
97
+ case ElementType.Array: {
98
+ break;
99
+ }
100
+ case ElementType.Object: {
101
+ return toObj(idx, element);
102
+ }
103
+ case ElementType.String: {
104
+ return toString(idx, element);
105
+ }
106
+ case ElementType.SubString: {
107
+ return handleSubStringElement(idx, element);
108
+ }
109
+ case ElementType.Set: {
110
+ return toSet(idx, element);
111
+ }
112
+ case ElementType.Map: {
113
+ return toMap(idx, element);
114
+ }
115
+ case ElementType.RegExp: {
116
+ return toRegExp(idx, element);
117
+ }
118
+ case ElementType.Date: {
119
+ return toDate(idx, element);
120
+ }
121
+ case ElementType.BigInt: {
122
+ return toBigInt(idx, element);
123
+ }
124
+ }
125
+ return toArr(idx, element);
126
+ }
127
+ function idxToValue(idx) {
128
+ if (!idx)
129
+ return undefined;
130
+ const found = cache.get(idx);
131
+ if (found !== undefined) {
132
+ if (typeof idx === 'number')
133
+ referenced.add(idx);
134
+ return found;
135
+ }
136
+ if (Array.isArray(idx)) {
137
+ // it is a nested string;
138
+ const parts = idx.map((i) => idxToValue(i));
139
+ return joinToString(parts);
140
+ }
141
+ const element = data[idx];
142
+ if (typeof element === 'object') {
143
+ // eslint-disable-next-line unicorn/no-null
144
+ if (element === null)
145
+ return null;
146
+ if (Array.isArray(element))
147
+ return handleArrayElement(idx, element);
148
+ return {};
149
+ }
150
+ return element;
151
+ }
152
+ return idxToValue(1);
153
+ }
154
+ function joinToString(parts) {
155
+ return parts.map((a) => (Array.isArray(a) ? joinToString(a) : a)).join('');
156
+ }
157
+ function isArrayElement(value) {
158
+ return Array.isArray(value) && value[0] === ElementType.Array;
159
+ }
160
+ export function parse(data) {
161
+ return fromJSON(JSON.parse(data));
162
+ }
163
+ //# sourceMappingURL=unpack.mjs.map
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "flatpack-json",
3
+ "publishConfig": {
4
+ "access": "public"
5
+ },
6
+ "version": "8.14.3",
7
+ "description": "A library to normalize JSON objects to reduce the size.",
8
+ "keywords": [
9
+ "cspell",
10
+ "json",
11
+ "normalize",
12
+ "flatten"
13
+ ],
14
+ "author": "Jason Dent <jason@streetsidesoftware.nl>",
15
+ "homepage": "https://github.com/streetsidesoftware/cspell/tree/main/packages/flatpack-json#readme",
16
+ "license": "MIT",
17
+ "type": "module",
18
+ "sideEffects": false,
19
+ "exports": {
20
+ ".": "./dist/index.js"
21
+ },
22
+ "directories": {
23
+ "dist": "dist"
24
+ },
25
+ "typings": "dist/index.d.ts",
26
+ "files": [
27
+ "dist",
28
+ "!**/*.tsbuildInfo",
29
+ "!**/__mocks__",
30
+ "!**/*.test.*",
31
+ "!**/*.spec.*",
32
+ "!**/*.map"
33
+ ],
34
+ "scripts": {
35
+ "build": "tsc -p .",
36
+ "watch": "tsc -p . -w",
37
+ "clean": "shx rm -rf dist temp coverage \"*.tsbuildInfo\"",
38
+ "clean-build": "pnpm run clean && pnpm run build",
39
+ "coverage": "vitest run --coverage",
40
+ "test-watch": "vitest",
41
+ "test": "vitest run"
42
+ },
43
+ "repository": {
44
+ "type": "git",
45
+ "url": "https://github.com/streetsidesoftware/cspell.git",
46
+ "directory": "packages/flatpack-json"
47
+ },
48
+ "bugs": {
49
+ "url": "https://github.com/streetsidesoftware/cspell/labels/filetype"
50
+ },
51
+ "engines": {
52
+ "node": ">=18"
53
+ },
54
+ "devDependencies": {
55
+ "@cspell/filetypes": "8.14.3",
56
+ "diff": "^7.0.0"
57
+ },
58
+ "gitHead": "ce996377857f5d7c3e70f27fc512afbe605562f8"
59
+ }