flattify 1.0.2 → 2.0.0
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 +1 -1
- package/README.md +332 -120
- package/dist/flatten-CYe7gyYE.js +228 -0
- package/dist/flatten-DWk2mUxV.cjs +287 -0
- package/dist/index.cjs +89 -0
- package/dist/index.d.cts +152 -0
- package/dist/index.d.ts +152 -0
- package/dist/index.js +87 -0
- package/dist/path-types-D9d1uvNh.d.cts +148 -0
- package/dist/path-types-D9d1uvNh.d.ts +148 -0
- package/dist/path.cjs +132 -0
- package/dist/path.d.cts +168 -0
- package/dist/path.d.ts +168 -0
- package/dist/path.js +124 -0
- package/package.json +106 -17
- package/path/package.json +6 -0
- package/.editorconfig +0 -17
- package/.npmignore +0 -3
- package/CHANGELOG.md +0 -6
- package/index.js +0 -7
- package/lib/flattify.js +0 -60
- package/test/flattify.js +0 -180
- package/test/mocks/array.json +0 -44
- package/test/mocks/object.json +0 -21
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
//#region src/shared/guards.ts
|
|
2
|
+
/**
|
|
3
|
+
* Plain objects are the ones created by `{}`, `Object.create(null)` or `JSON.parse`, including those
|
|
4
|
+
* from another realm (iframe, `vm`): their prototype is `null` or a root prototype. Dates, maps, class
|
|
5
|
+
* instances and every other object are treated as values.
|
|
6
|
+
*/
|
|
7
|
+
function isPlainObject(input) {
|
|
8
|
+
if (typeof input !== "object" || input === null) return false;
|
|
9
|
+
const proto = Object.getPrototypeOf(input);
|
|
10
|
+
return proto === Object.prototype || proto === null || Object.getPrototypeOf(proto) === null;
|
|
11
|
+
}
|
|
12
|
+
/** Plain objects, and arrays unless `safe` keeps them as values. */
|
|
13
|
+
function isContainer(input, safe) {
|
|
14
|
+
return Array.isArray(input) ? !safe : isPlainObject(input);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Sets an own enumerable property. A plain assignment of `__proto__` would change the prototype of
|
|
18
|
+
* `target` instead of creating a key.
|
|
19
|
+
*/
|
|
20
|
+
function assign(target, key, value) {
|
|
21
|
+
if (key === "__proto__") Object.defineProperty(target, key, {
|
|
22
|
+
value,
|
|
23
|
+
writable: true,
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true
|
|
26
|
+
});
|
|
27
|
+
else target[key] = value;
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/shared/path.ts
|
|
31
|
+
/**
|
|
32
|
+
* Builds the function that escapes a single key so the path can be split back. Keys that are already
|
|
33
|
+
* unambiguous are returned as they are; the others get `\` before every `\`, every first character of the
|
|
34
|
+
* delimiter and every `extra` character (`[` in bracket notation).
|
|
35
|
+
*
|
|
36
|
+
* A key is ambiguous when it contains `\`, the delimiter or an `extra` character, or ends like the start
|
|
37
|
+
* of the delimiter so that the two overlap (`'a:' + '::'` → `'a:::'`). The overlapping tails depend on the
|
|
38
|
+
* delimiter only, so they are computed once here rather than for every key.
|
|
39
|
+
*/
|
|
40
|
+
function keyEscaper(delimiter, extra) {
|
|
41
|
+
const tails = [];
|
|
42
|
+
for (let length = 1; length < delimiter.length; length++) {
|
|
43
|
+
const tail = delimiter.slice(0, length);
|
|
44
|
+
if ((tail + delimiter).startsWith(delimiter)) tails.push(tail);
|
|
45
|
+
}
|
|
46
|
+
const special = `\\${delimiter[0]}${extra}`;
|
|
47
|
+
return (key) => {
|
|
48
|
+
if (key.indexOf("\\") === -1 && key.indexOf(delimiter) === -1 && (extra === "" || key.indexOf(extra) === -1) && !tails.some((tail) => key.endsWith(tail))) return key;
|
|
49
|
+
let escaped = "";
|
|
50
|
+
for (const char of key) escaped += special.includes(char) ? `\\${char}` : char;
|
|
51
|
+
return escaped;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/** Escapes a single key; see `keyEscaper`. */
|
|
55
|
+
function escapeKey(key, delimiter, extra = "") {
|
|
56
|
+
return keyEscaper(delimiter, extra)(key);
|
|
57
|
+
}
|
|
58
|
+
/** Splits a path on the delimiter. With `escape`, `\` makes the next character part of the key, reversing `escapeKey`. */
|
|
59
|
+
function splitPath(path, delimiter, escape) {
|
|
60
|
+
if (!escape || !path.includes("\\")) return path.split(delimiter);
|
|
61
|
+
const segments = [];
|
|
62
|
+
let current = "";
|
|
63
|
+
let i = 0;
|
|
64
|
+
while (i < path.length) if (path[i] === "\\" && i + 1 < path.length) {
|
|
65
|
+
current += path[i + 1];
|
|
66
|
+
i += 2;
|
|
67
|
+
} else if (path.startsWith(delimiter, i)) {
|
|
68
|
+
segments.push(current);
|
|
69
|
+
current = "";
|
|
70
|
+
i += delimiter.length;
|
|
71
|
+
} else current += path[i++];
|
|
72
|
+
segments.push(current);
|
|
73
|
+
return segments;
|
|
74
|
+
}
|
|
75
|
+
/** Canonical array indices (`0`, `1`, `42`, not `01` or `-1`) up to `limit`. */
|
|
76
|
+
function isIndex(segment, limit) {
|
|
77
|
+
return /^(?:0|[1-9]\d*)$/.test(segment) && Number(segment) <= limit;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Splits a bracket-notation path: `a.b[0].c` and `a[b][0][c]` (the form-field style) both give
|
|
81
|
+
* `['a', 'b', '0', 'c']`. A `[` without a closing `]` is part of the key.
|
|
82
|
+
*/
|
|
83
|
+
function splitBracketPath(path, delimiter, escape) {
|
|
84
|
+
const segments = [];
|
|
85
|
+
let current = "";
|
|
86
|
+
let started = true;
|
|
87
|
+
const lastClose = lastUnescapedClose(path, escape);
|
|
88
|
+
let i = 0;
|
|
89
|
+
while (i < path.length) {
|
|
90
|
+
const char = path[i];
|
|
91
|
+
if (escape && char === "\\" && i + 1 < path.length) {
|
|
92
|
+
current += path[i + 1];
|
|
93
|
+
started = true;
|
|
94
|
+
i += 2;
|
|
95
|
+
} else if (char === "[" && i < lastClose) {
|
|
96
|
+
const close = findClose(path, i + 1, escape);
|
|
97
|
+
if (current !== "" || started && i > 0) segments.push(current);
|
|
98
|
+
segments.push(unescapeBracket(path.slice(i + 1, close), escape));
|
|
99
|
+
current = "";
|
|
100
|
+
started = false;
|
|
101
|
+
i = close + 1;
|
|
102
|
+
if (path.startsWith(delimiter, i)) {
|
|
103
|
+
started = true;
|
|
104
|
+
i += delimiter.length;
|
|
105
|
+
}
|
|
106
|
+
} else if (path.startsWith(delimiter, i)) {
|
|
107
|
+
segments.push(current);
|
|
108
|
+
current = "";
|
|
109
|
+
started = true;
|
|
110
|
+
i += delimiter.length;
|
|
111
|
+
} else {
|
|
112
|
+
current += char;
|
|
113
|
+
started = true;
|
|
114
|
+
i++;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (current !== "" || started) segments.push(current);
|
|
118
|
+
return segments;
|
|
119
|
+
}
|
|
120
|
+
/** Called only before `lastUnescapedClose`, so a closing `]` is always found. */
|
|
121
|
+
function findClose(path, from, escape) {
|
|
122
|
+
let i = from;
|
|
123
|
+
while (path[i] !== "]") i += escape && path[i] === "\\" ? 2 : 1;
|
|
124
|
+
return i;
|
|
125
|
+
}
|
|
126
|
+
function lastUnescapedClose(path, escape) {
|
|
127
|
+
if (!escape) return path.lastIndexOf("]");
|
|
128
|
+
let last = -1;
|
|
129
|
+
for (let i = 0; i < path.length; i++) if (path[i] === "\\") i++;
|
|
130
|
+
else if (path[i] === "]") last = i;
|
|
131
|
+
return last;
|
|
132
|
+
}
|
|
133
|
+
function unescapeBracket(key, escape) {
|
|
134
|
+
return escape && key.includes("\\") ? key.replace(/\\(.)/g, "$1") : key;
|
|
135
|
+
}
|
|
136
|
+
/** RFC 6901: `~` is written `~0` and `/` is written `~1`. */
|
|
137
|
+
function escapePointer(key) {
|
|
138
|
+
return key.replace(/~/g, "~0").replace(/\//g, "~1");
|
|
139
|
+
}
|
|
140
|
+
/** Splits a JSON Pointer (`/a/0/b~1c` → `['a', '0', 'b/c']`). `''` is the whole document: no keys. */
|
|
141
|
+
function splitPointer(pointer) {
|
|
142
|
+
if (pointer === "") return [];
|
|
143
|
+
if (!pointer.startsWith("/")) throw new TypeError(`A JSON Pointer must start with "/", got "${pointer}"`);
|
|
144
|
+
return pointer.slice(1).split("/").map((segment) => segment.includes("~") ? segment.replace(/~1/g, "/").replace(/~0/g, "~") : segment);
|
|
145
|
+
}
|
|
146
|
+
//#endregion
|
|
147
|
+
//#region src/shared/notation.ts
|
|
148
|
+
function validateNotation(notation) {
|
|
149
|
+
if (notation !== "dot" && notation !== "bracket" && notation !== "pointer") throw new TypeError("`notation` must be 'dot', 'bracket' or 'pointer'");
|
|
150
|
+
}
|
|
151
|
+
function pathFormat(notation, delimiter, escape) {
|
|
152
|
+
if (notation === "pointer") return {
|
|
153
|
+
join: (path, key) => `${path !== null && path !== void 0 ? path : ""}/${escapePointer(key)}`,
|
|
154
|
+
split: splitPointer
|
|
155
|
+
};
|
|
156
|
+
const bracket = notation === "bracket";
|
|
157
|
+
const escapeKey = escape ? keyEscaper(delimiter, bracket ? "[" : "") : void 0;
|
|
158
|
+
return {
|
|
159
|
+
join: (path, key, isIndex) => {
|
|
160
|
+
if (bracket && isIndex) return `${path !== null && path !== void 0 ? path : ""}[${key}]`;
|
|
161
|
+
const segment = escapeKey ? escapeKey(key) : key;
|
|
162
|
+
return path === void 0 ? segment : path + delimiter + segment;
|
|
163
|
+
},
|
|
164
|
+
split: notation === "bracket" ? (path) => splitBracketPath(path, delimiter, escape) : (path) => splitPath(path, delimiter, escape)
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
//#endregion
|
|
168
|
+
//#region src/shared/validate.ts
|
|
169
|
+
function validate(delimiter, escape) {
|
|
170
|
+
if (typeof delimiter !== "string" || delimiter === "") throw new TypeError("`delimiter` must be a non-empty string");
|
|
171
|
+
if (escape && delimiter.includes("\\")) throw new RangeError("`delimiter` cannot contain `\\` while `escape` is enabled: it is the escape character");
|
|
172
|
+
}
|
|
173
|
+
//#endregion
|
|
174
|
+
//#region src/flatten/flatten.ts
|
|
175
|
+
function frame(value, path, depth) {
|
|
176
|
+
return {
|
|
177
|
+
value,
|
|
178
|
+
keys: Object.keys(value),
|
|
179
|
+
index: 0,
|
|
180
|
+
isArray: Array.isArray(value),
|
|
181
|
+
path,
|
|
182
|
+
depth
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
function validateWalk(maxDepth, circular) {
|
|
186
|
+
if (maxDepth !== Infinity && (!Number.isInteger(maxDepth) || maxDepth < 1)) throw new RangeError("`maxDepth` must be a positive integer or Infinity");
|
|
187
|
+
if (circular !== "throw" && circular !== "skip") throw new TypeError("`circular` must be 'throw' or 'skip'");
|
|
188
|
+
}
|
|
189
|
+
function flatten(input, options = {}) {
|
|
190
|
+
const { delimiter = ".", notation = "dot", maxDepth = Infinity, safe = false, keepEmpty = true, escape = true, circular = "throw", transformKey, preserve } = options;
|
|
191
|
+
validateNotation(notation);
|
|
192
|
+
if (notation !== "pointer") validate(delimiter, escape);
|
|
193
|
+
validateWalk(maxDepth, circular);
|
|
194
|
+
if (!isContainer(input, safe)) throw new TypeError("flatten() expects a plain object or an array");
|
|
195
|
+
const { join } = pathFormat(notation, delimiter, escape);
|
|
196
|
+
const result = {};
|
|
197
|
+
const ancestors = /* @__PURE__ */ new Set([input]);
|
|
198
|
+
const stack = [frame(input, void 0, 1)];
|
|
199
|
+
while (stack.length > 0) {
|
|
200
|
+
const current = stack[stack.length - 1];
|
|
201
|
+
if (current.index === current.keys.length) {
|
|
202
|
+
stack.pop();
|
|
203
|
+
ancestors.delete(current.value);
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
const key = current.keys[current.index++];
|
|
207
|
+
const value = current.value[key];
|
|
208
|
+
const path = join(current.path, transformKey && !current.isArray ? transformKey(key) : key, current.isArray);
|
|
209
|
+
if (isContainer(value, safe) && current.depth < maxDepth && !(preserve && preserve(path, value))) {
|
|
210
|
+
if (ancestors.has(value)) {
|
|
211
|
+
if (circular === "throw") throw new TypeError(`Circular reference at "${path}"`);
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
const child = frame(value, path, current.depth + 1);
|
|
215
|
+
if (child.keys.length === 0) {
|
|
216
|
+
if (keepEmpty) assign(result, path, value);
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
ancestors.add(value);
|
|
220
|
+
stack.push(child);
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
assign(result, path, value);
|
|
224
|
+
}
|
|
225
|
+
return result;
|
|
226
|
+
}
|
|
227
|
+
//#endregion
|
|
228
|
+
export { escapeKey as a, assign as c, validateNotation as i, isContainer as l, validate as n, escapePointer as o, pathFormat as r, isIndex as s, flatten as t, isPlainObject as u };
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
//#region src/shared/guards.ts
|
|
2
|
+
/**
|
|
3
|
+
* Plain objects are the ones created by `{}`, `Object.create(null)` or `JSON.parse`, including those
|
|
4
|
+
* from another realm (iframe, `vm`): their prototype is `null` or a root prototype. Dates, maps, class
|
|
5
|
+
* instances and every other object are treated as values.
|
|
6
|
+
*/
|
|
7
|
+
function isPlainObject(input) {
|
|
8
|
+
if (typeof input !== "object" || input === null) return false;
|
|
9
|
+
const proto = Object.getPrototypeOf(input);
|
|
10
|
+
return proto === Object.prototype || proto === null || Object.getPrototypeOf(proto) === null;
|
|
11
|
+
}
|
|
12
|
+
/** Plain objects, and arrays unless `safe` keeps them as values. */
|
|
13
|
+
function isContainer(input, safe) {
|
|
14
|
+
return Array.isArray(input) ? !safe : isPlainObject(input);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Sets an own enumerable property. A plain assignment of `__proto__` would change the prototype of
|
|
18
|
+
* `target` instead of creating a key.
|
|
19
|
+
*/
|
|
20
|
+
function assign(target, key, value) {
|
|
21
|
+
if (key === "__proto__") Object.defineProperty(target, key, {
|
|
22
|
+
value,
|
|
23
|
+
writable: true,
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true
|
|
26
|
+
});
|
|
27
|
+
else target[key] = value;
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/shared/path.ts
|
|
31
|
+
/**
|
|
32
|
+
* Builds the function that escapes a single key so the path can be split back. Keys that are already
|
|
33
|
+
* unambiguous are returned as they are; the others get `\` before every `\`, every first character of the
|
|
34
|
+
* delimiter and every `extra` character (`[` in bracket notation).
|
|
35
|
+
*
|
|
36
|
+
* A key is ambiguous when it contains `\`, the delimiter or an `extra` character, or ends like the start
|
|
37
|
+
* of the delimiter so that the two overlap (`'a:' + '::'` → `'a:::'`). The overlapping tails depend on the
|
|
38
|
+
* delimiter only, so they are computed once here rather than for every key.
|
|
39
|
+
*/
|
|
40
|
+
function keyEscaper(delimiter, extra) {
|
|
41
|
+
const tails = [];
|
|
42
|
+
for (let length = 1; length < delimiter.length; length++) {
|
|
43
|
+
const tail = delimiter.slice(0, length);
|
|
44
|
+
if ((tail + delimiter).startsWith(delimiter)) tails.push(tail);
|
|
45
|
+
}
|
|
46
|
+
const special = `\\${delimiter[0]}${extra}`;
|
|
47
|
+
return (key) => {
|
|
48
|
+
if (key.indexOf("\\") === -1 && key.indexOf(delimiter) === -1 && (extra === "" || key.indexOf(extra) === -1) && !tails.some((tail) => key.endsWith(tail))) return key;
|
|
49
|
+
let escaped = "";
|
|
50
|
+
for (const char of key) escaped += special.includes(char) ? `\\${char}` : char;
|
|
51
|
+
return escaped;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/** Escapes a single key; see `keyEscaper`. */
|
|
55
|
+
function escapeKey(key, delimiter, extra = "") {
|
|
56
|
+
return keyEscaper(delimiter, extra)(key);
|
|
57
|
+
}
|
|
58
|
+
/** Splits a path on the delimiter. With `escape`, `\` makes the next character part of the key, reversing `escapeKey`. */
|
|
59
|
+
function splitPath(path, delimiter, escape) {
|
|
60
|
+
if (!escape || !path.includes("\\")) return path.split(delimiter);
|
|
61
|
+
const segments = [];
|
|
62
|
+
let current = "";
|
|
63
|
+
let i = 0;
|
|
64
|
+
while (i < path.length) if (path[i] === "\\" && i + 1 < path.length) {
|
|
65
|
+
current += path[i + 1];
|
|
66
|
+
i += 2;
|
|
67
|
+
} else if (path.startsWith(delimiter, i)) {
|
|
68
|
+
segments.push(current);
|
|
69
|
+
current = "";
|
|
70
|
+
i += delimiter.length;
|
|
71
|
+
} else current += path[i++];
|
|
72
|
+
segments.push(current);
|
|
73
|
+
return segments;
|
|
74
|
+
}
|
|
75
|
+
/** Canonical array indices (`0`, `1`, `42`, not `01` or `-1`) up to `limit`. */
|
|
76
|
+
function isIndex(segment, limit) {
|
|
77
|
+
return /^(?:0|[1-9]\d*)$/.test(segment) && Number(segment) <= limit;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Splits a bracket-notation path: `a.b[0].c` and `a[b][0][c]` (the form-field style) both give
|
|
81
|
+
* `['a', 'b', '0', 'c']`. A `[` without a closing `]` is part of the key.
|
|
82
|
+
*/
|
|
83
|
+
function splitBracketPath(path, delimiter, escape) {
|
|
84
|
+
const segments = [];
|
|
85
|
+
let current = "";
|
|
86
|
+
let started = true;
|
|
87
|
+
const lastClose = lastUnescapedClose(path, escape);
|
|
88
|
+
let i = 0;
|
|
89
|
+
while (i < path.length) {
|
|
90
|
+
const char = path[i];
|
|
91
|
+
if (escape && char === "\\" && i + 1 < path.length) {
|
|
92
|
+
current += path[i + 1];
|
|
93
|
+
started = true;
|
|
94
|
+
i += 2;
|
|
95
|
+
} else if (char === "[" && i < lastClose) {
|
|
96
|
+
const close = findClose(path, i + 1, escape);
|
|
97
|
+
if (current !== "" || started && i > 0) segments.push(current);
|
|
98
|
+
segments.push(unescapeBracket(path.slice(i + 1, close), escape));
|
|
99
|
+
current = "";
|
|
100
|
+
started = false;
|
|
101
|
+
i = close + 1;
|
|
102
|
+
if (path.startsWith(delimiter, i)) {
|
|
103
|
+
started = true;
|
|
104
|
+
i += delimiter.length;
|
|
105
|
+
}
|
|
106
|
+
} else if (path.startsWith(delimiter, i)) {
|
|
107
|
+
segments.push(current);
|
|
108
|
+
current = "";
|
|
109
|
+
started = true;
|
|
110
|
+
i += delimiter.length;
|
|
111
|
+
} else {
|
|
112
|
+
current += char;
|
|
113
|
+
started = true;
|
|
114
|
+
i++;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (current !== "" || started) segments.push(current);
|
|
118
|
+
return segments;
|
|
119
|
+
}
|
|
120
|
+
/** Called only before `lastUnescapedClose`, so a closing `]` is always found. */
|
|
121
|
+
function findClose(path, from, escape) {
|
|
122
|
+
let i = from;
|
|
123
|
+
while (path[i] !== "]") i += escape && path[i] === "\\" ? 2 : 1;
|
|
124
|
+
return i;
|
|
125
|
+
}
|
|
126
|
+
function lastUnescapedClose(path, escape) {
|
|
127
|
+
if (!escape) return path.lastIndexOf("]");
|
|
128
|
+
let last = -1;
|
|
129
|
+
for (let i = 0; i < path.length; i++) if (path[i] === "\\") i++;
|
|
130
|
+
else if (path[i] === "]") last = i;
|
|
131
|
+
return last;
|
|
132
|
+
}
|
|
133
|
+
function unescapeBracket(key, escape) {
|
|
134
|
+
return escape && key.includes("\\") ? key.replace(/\\(.)/g, "$1") : key;
|
|
135
|
+
}
|
|
136
|
+
/** RFC 6901: `~` is written `~0` and `/` is written `~1`. */
|
|
137
|
+
function escapePointer(key) {
|
|
138
|
+
return key.replace(/~/g, "~0").replace(/\//g, "~1");
|
|
139
|
+
}
|
|
140
|
+
/** Splits a JSON Pointer (`/a/0/b~1c` → `['a', '0', 'b/c']`). `''` is the whole document: no keys. */
|
|
141
|
+
function splitPointer(pointer) {
|
|
142
|
+
if (pointer === "") return [];
|
|
143
|
+
if (!pointer.startsWith("/")) throw new TypeError(`A JSON Pointer must start with "/", got "${pointer}"`);
|
|
144
|
+
return pointer.slice(1).split("/").map((segment) => segment.includes("~") ? segment.replace(/~1/g, "/").replace(/~0/g, "~") : segment);
|
|
145
|
+
}
|
|
146
|
+
//#endregion
|
|
147
|
+
//#region src/shared/notation.ts
|
|
148
|
+
function validateNotation(notation) {
|
|
149
|
+
if (notation !== "dot" && notation !== "bracket" && notation !== "pointer") throw new TypeError("`notation` must be 'dot', 'bracket' or 'pointer'");
|
|
150
|
+
}
|
|
151
|
+
function pathFormat(notation, delimiter, escape) {
|
|
152
|
+
if (notation === "pointer") return {
|
|
153
|
+
join: (path, key) => `${path !== null && path !== void 0 ? path : ""}/${escapePointer(key)}`,
|
|
154
|
+
split: splitPointer
|
|
155
|
+
};
|
|
156
|
+
const bracket = notation === "bracket";
|
|
157
|
+
const escapeKey = escape ? keyEscaper(delimiter, bracket ? "[" : "") : void 0;
|
|
158
|
+
return {
|
|
159
|
+
join: (path, key, isIndex) => {
|
|
160
|
+
if (bracket && isIndex) return `${path !== null && path !== void 0 ? path : ""}[${key}]`;
|
|
161
|
+
const segment = escapeKey ? escapeKey(key) : key;
|
|
162
|
+
return path === void 0 ? segment : path + delimiter + segment;
|
|
163
|
+
},
|
|
164
|
+
split: notation === "bracket" ? (path) => splitBracketPath(path, delimiter, escape) : (path) => splitPath(path, delimiter, escape)
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
//#endregion
|
|
168
|
+
//#region src/shared/validate.ts
|
|
169
|
+
function validate(delimiter, escape) {
|
|
170
|
+
if (typeof delimiter !== "string" || delimiter === "") throw new TypeError("`delimiter` must be a non-empty string");
|
|
171
|
+
if (escape && delimiter.includes("\\")) throw new RangeError("`delimiter` cannot contain `\\` while `escape` is enabled: it is the escape character");
|
|
172
|
+
}
|
|
173
|
+
//#endregion
|
|
174
|
+
//#region src/flatten/flatten.ts
|
|
175
|
+
function frame(value, path, depth) {
|
|
176
|
+
return {
|
|
177
|
+
value,
|
|
178
|
+
keys: Object.keys(value),
|
|
179
|
+
index: 0,
|
|
180
|
+
isArray: Array.isArray(value),
|
|
181
|
+
path,
|
|
182
|
+
depth
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
function validateWalk(maxDepth, circular) {
|
|
186
|
+
if (maxDepth !== Infinity && (!Number.isInteger(maxDepth) || maxDepth < 1)) throw new RangeError("`maxDepth` must be a positive integer or Infinity");
|
|
187
|
+
if (circular !== "throw" && circular !== "skip") throw new TypeError("`circular` must be 'throw' or 'skip'");
|
|
188
|
+
}
|
|
189
|
+
function flatten(input, options = {}) {
|
|
190
|
+
const { delimiter = ".", notation = "dot", maxDepth = Infinity, safe = false, keepEmpty = true, escape = true, circular = "throw", transformKey, preserve } = options;
|
|
191
|
+
validateNotation(notation);
|
|
192
|
+
if (notation !== "pointer") validate(delimiter, escape);
|
|
193
|
+
validateWalk(maxDepth, circular);
|
|
194
|
+
if (!isContainer(input, safe)) throw new TypeError("flatten() expects a plain object or an array");
|
|
195
|
+
const { join } = pathFormat(notation, delimiter, escape);
|
|
196
|
+
const result = {};
|
|
197
|
+
const ancestors = /* @__PURE__ */ new Set([input]);
|
|
198
|
+
const stack = [frame(input, void 0, 1)];
|
|
199
|
+
while (stack.length > 0) {
|
|
200
|
+
const current = stack[stack.length - 1];
|
|
201
|
+
if (current.index === current.keys.length) {
|
|
202
|
+
stack.pop();
|
|
203
|
+
ancestors.delete(current.value);
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
const key = current.keys[current.index++];
|
|
207
|
+
const value = current.value[key];
|
|
208
|
+
const path = join(current.path, transformKey && !current.isArray ? transformKey(key) : key, current.isArray);
|
|
209
|
+
if (isContainer(value, safe) && current.depth < maxDepth && !(preserve && preserve(path, value))) {
|
|
210
|
+
if (ancestors.has(value)) {
|
|
211
|
+
if (circular === "throw") throw new TypeError(`Circular reference at "${path}"`);
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
const child = frame(value, path, current.depth + 1);
|
|
215
|
+
if (child.keys.length === 0) {
|
|
216
|
+
if (keepEmpty) assign(result, path, value);
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
ancestors.add(value);
|
|
220
|
+
stack.push(child);
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
assign(result, path, value);
|
|
224
|
+
}
|
|
225
|
+
return result;
|
|
226
|
+
}
|
|
227
|
+
//#endregion
|
|
228
|
+
Object.defineProperty(exports, "assign", {
|
|
229
|
+
enumerable: true,
|
|
230
|
+
get: function() {
|
|
231
|
+
return assign;
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
Object.defineProperty(exports, "escapeKey", {
|
|
235
|
+
enumerable: true,
|
|
236
|
+
get: function() {
|
|
237
|
+
return escapeKey;
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
Object.defineProperty(exports, "escapePointer", {
|
|
241
|
+
enumerable: true,
|
|
242
|
+
get: function() {
|
|
243
|
+
return escapePointer;
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
Object.defineProperty(exports, "flatten", {
|
|
247
|
+
enumerable: true,
|
|
248
|
+
get: function() {
|
|
249
|
+
return flatten;
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
Object.defineProperty(exports, "isContainer", {
|
|
253
|
+
enumerable: true,
|
|
254
|
+
get: function() {
|
|
255
|
+
return isContainer;
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
Object.defineProperty(exports, "isIndex", {
|
|
259
|
+
enumerable: true,
|
|
260
|
+
get: function() {
|
|
261
|
+
return isIndex;
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
Object.defineProperty(exports, "isPlainObject", {
|
|
265
|
+
enumerable: true,
|
|
266
|
+
get: function() {
|
|
267
|
+
return isPlainObject;
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
Object.defineProperty(exports, "pathFormat", {
|
|
271
|
+
enumerable: true,
|
|
272
|
+
get: function() {
|
|
273
|
+
return pathFormat;
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
Object.defineProperty(exports, "validate", {
|
|
277
|
+
enumerable: true,
|
|
278
|
+
get: function() {
|
|
279
|
+
return validate;
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
Object.defineProperty(exports, "validateNotation", {
|
|
283
|
+
enumerable: true,
|
|
284
|
+
get: function() {
|
|
285
|
+
return validateNotation;
|
|
286
|
+
}
|
|
287
|
+
});
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_flatten = require("./flatten-DWk2mUxV.cjs");
|
|
3
|
+
//#region src/unflatten/unflatten.ts
|
|
4
|
+
const hasOwn = (target, key) => Object.prototype.hasOwnProperty.call(target, key);
|
|
5
|
+
function validateArrayLimit(arrayLimit) {
|
|
6
|
+
if (arrayLimit !== Infinity && (!Number.isInteger(arrayLimit) || arrayLimit < 0)) throw new RangeError("`arrayLimit` must be a non-negative integer or Infinity");
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Walks from `parent` into `key`, creating the object when it is missing. Returns `undefined` when a
|
|
10
|
+
* value that is not an object or array is in the way and `overwrite` is off.
|
|
11
|
+
*
|
|
12
|
+
* `owned` holds the objects built by `unflatten`. Objects and arrays that came in as values of `input`
|
|
13
|
+
* are not in it: they are copied before new keys go in, never modified.
|
|
14
|
+
*/
|
|
15
|
+
function descend(parent, key, overwrite, owned, candidates) {
|
|
16
|
+
const current = hasOwn(parent, key) ? parent[key] : void 0;
|
|
17
|
+
if (require_flatten.isContainer(current, false) && owned.has(current)) return current;
|
|
18
|
+
if (current !== void 0 && !require_flatten.isContainer(current, false) && !overwrite) return void 0;
|
|
19
|
+
const next = {};
|
|
20
|
+
if (require_flatten.isContainer(current, false)) for (const k of Object.keys(current)) require_flatten.assign(next, k, current[k]);
|
|
21
|
+
owned.add(next);
|
|
22
|
+
require_flatten.assign(parent, key, next);
|
|
23
|
+
candidates === null || candidates === void 0 || candidates.push([
|
|
24
|
+
parent,
|
|
25
|
+
key,
|
|
26
|
+
next
|
|
27
|
+
]);
|
|
28
|
+
return next;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Turns the candidates whose keys are all array indices into arrays. They are visited from the last one
|
|
32
|
+
* built, so nested arrays are in place before the object that holds them is converted.
|
|
33
|
+
*/
|
|
34
|
+
function restoreArrays(candidates, arrayLimit) {
|
|
35
|
+
for (let c = candidates.length - 1; c >= 0; c--) {
|
|
36
|
+
const [parent, key, object] = candidates[c];
|
|
37
|
+
if (parent[key] !== object) continue;
|
|
38
|
+
const keys = Object.keys(object);
|
|
39
|
+
if (!keys.every((k) => require_flatten.isIndex(k, arrayLimit))) continue;
|
|
40
|
+
const array = [];
|
|
41
|
+
for (const k of keys) array[Number(k)] = object[k];
|
|
42
|
+
parent[key] = array;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function unflatten(input, options = {}) {
|
|
46
|
+
const { delimiter = ".", notation = "dot", object = false, overwrite = false, escape = true, arrayLimit = 1e3, asArray = false, transformKey } = options;
|
|
47
|
+
require_flatten.validateNotation(notation);
|
|
48
|
+
if (notation !== "pointer") require_flatten.validate(delimiter, escape);
|
|
49
|
+
validateArrayLimit(arrayLimit);
|
|
50
|
+
const { split } = require_flatten.pathFormat(notation, delimiter, escape);
|
|
51
|
+
if (!require_flatten.isPlainObject(input)) throw new TypeError("unflatten() expects a plain object");
|
|
52
|
+
const result = {};
|
|
53
|
+
const owned = /* @__PURE__ */ new Set([result]);
|
|
54
|
+
const candidates = object ? void 0 : [];
|
|
55
|
+
entries: for (const key of Object.keys(input)) {
|
|
56
|
+
const segments = split(key);
|
|
57
|
+
if (segments.length === 0) throw new TypeError("The JSON Pointer \"\" (the whole document) cannot be a key of a flat object");
|
|
58
|
+
if (segments.includes("__proto__")) continue;
|
|
59
|
+
if (transformKey) {
|
|
60
|
+
for (let i = 0; i < segments.length; i++) if (!require_flatten.isIndex(segments[i], Infinity)) segments[i] = transformKey(segments[i]);
|
|
61
|
+
}
|
|
62
|
+
let target = result;
|
|
63
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
64
|
+
const next = descend(target, segments[i], overwrite, owned, candidates && require_flatten.isIndex(segments[i + 1], arrayLimit) ? candidates : void 0);
|
|
65
|
+
if (next === void 0) continue entries;
|
|
66
|
+
target = next;
|
|
67
|
+
}
|
|
68
|
+
const last = segments[segments.length - 1];
|
|
69
|
+
const value = input[key];
|
|
70
|
+
const current = hasOwn(target, last) ? target[last] : void 0;
|
|
71
|
+
const isEmpty = require_flatten.isContainer(value, false) && Object.keys(value).length === 0;
|
|
72
|
+
if (require_flatten.isContainer(current, false) && (!overwrite || isEmpty)) continue;
|
|
73
|
+
require_flatten.assign(target, last, value);
|
|
74
|
+
}
|
|
75
|
+
if (candidates) restoreArrays(candidates, arrayLimit);
|
|
76
|
+
return asArray ? toRootArray(result, arrayLimit) : result;
|
|
77
|
+
}
|
|
78
|
+
/** `asArray`: the top-level keys are the indices of the array, as written by `flatten(array)`. */
|
|
79
|
+
function toRootArray(result, arrayLimit) {
|
|
80
|
+
const array = [];
|
|
81
|
+
for (const key of Object.keys(result)) {
|
|
82
|
+
if (!require_flatten.isIndex(key, arrayLimit)) throw new TypeError(`\`asArray\` expects every top-level key to be an array index up to \`arrayLimit\`, got "${key}"`);
|
|
83
|
+
array[Number(key)] = result[key];
|
|
84
|
+
}
|
|
85
|
+
return array;
|
|
86
|
+
}
|
|
87
|
+
//#endregion
|
|
88
|
+
exports.flatten = require_flatten.flatten;
|
|
89
|
+
exports.unflatten = unflatten;
|