-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
320 lines (271 loc) · 6.64 KB
/
Copy pathindex.js
File metadata and controls
320 lines (271 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
const fp = require("lodash/fp");
const isProduction = process.env.NODE_ENV === "production";
/**
* @type {Object}
* */
const EMPTY_OBJ = Object.freeze({});
/**
* @type {readonly any[]}
* */
const EMPTY_ARR = Object.freeze([]);
const INDEX_EXPR = /^(?:0|[1-9]\d*)$/;
/**
* @param {string|readonly string[]|null} path
* @returns {readonly string[]}
*/
function tokenize(path) {
return path == null || path === "" || path.length === 0
? EMPTY_ARR
: fp.toPath(path);
}
const deepFreeze = !isProduction
? function deepFreeze(obj, seen = new WeakSet()) {
if (obj === null || typeof obj !== "object") {
return obj;
}
if (seen.has(obj)) {
return obj;
}
seen.add(obj);
for (const key of Object.keys(obj)) {
deepFreeze(obj[key], seen);
}
return Object.freeze(obj);
}
: function deepFreeze(obj) {
return obj;
};
/**
*
* @param {Object|null} data
* @param {string|readonly string[]|null} path
* @returns {*}
*/
function get(data, path) {
if (path == null || path === "" || path.length === 0) {
return data;
}
const tokens = tokenize(path);
for (let i = 0; i < tokens.length; i++) {
if (data == null || typeof data !== "object") {
return undefined;
}
data = data[tokens[i]];
}
return data;
}
/**
* @param {string} value
* @returns {any}
*/
function parse(value) {
if (value === "{}") {
return EMPTY_OBJ;
} else if (value === "[]") {
return EMPTY_ARR;
} else {
return JSON.parse(value);
}
}
/**
* @param {any} value
* @returns {string}
*/
function stringify(value) {
if (value === EMPTY_OBJ) {
return "{}";
} else if (value === EMPTY_ARR) {
return "[]";
} else {
return JSON.stringify(value);
}
}
/**
* @param {string} key
* @returns {boolean}
*/
function isIndex(key) {
return INDEX_EXPR.test(key);
}
/**
*
* @param {Object|null} data
* @param {string|null|readonly string[]} path
* @param {any} value
* @param {boolean} [isPlainJSON]
* @returns {*}
*/
function set(data, path, value, isPlainJSON = false) {
data = data ?? EMPTY_OBJ;
if (typeof data !== "object") {
throw new Error("data must be object or null");
}
if (path == null || path === "" || path.length === 0) {
return _patch(data, value, isPlainJSON);
}
const tokens = tokenize(path);
const oldValue = get(data, tokens);
const newValue = value == null ? value : _patch(oldValue, value, isPlainJSON);
if (newValue === oldValue) {
return deepFreeze(data);
}
if (tokens.length === 0) {
return deepFreeze(newValue);
}
let result;
const rootIsArray = data ? Array.isArray(data) : isIndex(tokens[0]);
let node = data ? shallowCopy(data) : rootIsArray ? [] : {};
for (let i = 0; i < tokens.length; i++) {
const token = tokens[i];
const value =
i === tokens.length - 1
? newValue
: node[token] != null && typeof node[token] === "object"
? shallowCopy(node[token])
: !isIndex(tokens[i + 1])
? {}
: [];
if (Array.isArray(node)) {
if (!isIndex(token)) {
break;
}
const idx = Number(token);
for (let j = 0; j < idx; j++) {
node[j] ??= null;
}
result ??= node;
node = node[idx] = value ?? null;
} else if (value === undefined) {
if (Object.hasOwn(node, token)) {
result ??= node;
delete node[token];
}
break;
} else {
result ??= node;
node = node[token] = value;
}
}
return deepFreeze(result ?? data);
}
function jsonClone(o, isPlainJSON = false) {
if (o == null || typeof o === "string") {
return o;
}
if (Array.isArray(o) && o.length === 0) {
return EMPTY_ARR;
} else if (isPlainObject(o, isPlainJSON) && Object.keys(o).length === 0) {
return EMPTY_OBJ;
}
return JSON.parse(JSON.stringify(o));
}
function _patch(oldValue, newValue, isPlainJSON) {
if (oldValue === newValue) {
return oldValue;
} else if (oldValue == null || newValue == null) {
return isPlainJSON ? newValue : jsonClone(newValue, isPlainJSON);
} else if (Array.isArray(oldValue) && Array.isArray(newValue)) {
if (newValue.length === 0) {
return oldValue.length === 0 ? oldValue : EMPTY_ARR;
}
let len = newValue.length;
while (len > 0 && newValue[len - 1] === undefined) {
len--;
}
let arr = oldValue.length === len ? null : [];
for (let i = 0; i < len; i++) {
const val = _patch(oldValue[i], newValue[i], isPlainJSON);
if (!arr) {
if (val !== undefined && val === oldValue[i]) {
continue;
}
arr = [];
for (let j = 0; j < i; ++j) {
arr[j] = oldValue[j] ?? null;
}
}
arr[i] = val ?? null;
}
return arr ?? oldValue;
} else if (
isPlainObject(oldValue, true) &&
isPlainObject(newValue, isPlainJSON)
) {
const newKeys = Object.keys(newValue);
const oldKeys = Object.keys(oldValue);
if (newKeys.length === 0) {
return oldKeys.length === 0 ? oldValue : EMPTY_OBJ;
}
let obj;
let idx = 0;
for (let i = 0; i < newKeys.length; ++i) {
const key = newKeys[i];
const val = _patch(oldValue[key], newValue[key], isPlainJSON);
if (val !== undefined) {
idx++;
}
if (!obj) {
if (
val !== undefined &&
val === oldValue[key] &&
key === oldKeys[idx - 1]
) {
continue;
}
if (val === undefined && !Object.hasOwn(oldValue, key)) {
continue;
}
obj = {};
for (let j = 0; j < i; j++) {
const key = newKeys[j];
const val = oldValue[key];
if (val !== undefined) {
obj[key] = val;
}
}
}
if (val !== undefined) {
obj[key] = val;
}
}
if (idx === 0) {
return EMPTY_OBJ;
}
if (obj) {
return obj;
}
if (idx === oldKeys.length) {
return oldValue;
}
obj = {};
for (let j = 0; j < newKeys.length; j++) {
const key = newKeys[j];
const val = oldValue[key];
if (val !== undefined) {
obj[key] = val;
}
}
return obj;
} else {
return isPlainJSON ? newValue : jsonClone(newValue, false);
}
}
function shallowCopy(obj) {
return Array.isArray(obj) ? [...obj] : { ...obj };
}
function isPlainObject(value, isPlainJSON = false) {
return isPlainJSON
? value != null && typeof value === "object" && !Array.isArray(value)
: fp.isPlainObject(value);
}
module.exports = {
EMPTY: EMPTY_OBJ, // Compat
EMPTY_OBJ,
EMPTY_ARR,
parse,
stringify,
get,
set,
jsonClone,
tokenize,
};