-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathaccessDeep.ts
More file actions
167 lines (140 loc) · 3.8 KB
/
accessDeep.ts
File metadata and controls
167 lines (140 loc) · 3.8 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
import { isMap, isArray, isPlainObject, isSet } from './is.js';
import { includes } from './util.js';
export type AccessDeepContext = WeakMap<object, any[]>;
const getNthKey = (
value: Map<any, any> | Set<any>,
n: number,
context: AccessDeepContext
): any => {
let indexed = context.get(value);
if (!indexed) {
indexed = Array.from(value.keys());
context.set(value, indexed);
}
if (!Number.isInteger(n) || n < 0 || n >= indexed.length) {
throw new Error('index out of bounds');
}
return indexed[n];
};
function validatePath(path: (string | number)[]) {
if (includes(path, '__proto__')) {
throw new Error('__proto__ is not allowed as a property');
}
if (includes(path, 'prototype')) {
throw new Error('prototype is not allowed as a property');
}
if (includes(path, 'constructor')) {
throw new Error('constructor is not allowed as a property');
}
}
export const getDeep = (
object: object,
path: (string | number)[],
context: AccessDeepContext
): object => {
validatePath(path);
for (let i = 0; i < path.length; i++) {
const key = path[i];
if (isSet(object)) {
object = getNthKey(object, +key, context);
} else if (isMap(object)) {
const row = +key;
const type = +path[++i] === 0 ? 'key' : 'value';
const keyOfRow = getNthKey(object, row, context);
switch (type) {
case 'key':
object = keyOfRow;
break;
case 'value':
object = object.get(keyOfRow);
break;
}
} else {
object = (object as any)[key];
}
}
return object;
};
export const setDeep = (
object: any,
path: (string | number)[],
mapper: (v: any) => any,
context: AccessDeepContext
): any => {
validatePath(path);
if (path.length === 0) {
return mapper(object);
}
let parent = object;
for (let i = 0; i < path.length - 1; i++) {
const key = path[i];
if (isArray(parent)) {
const index = +key;
parent = parent[index];
} else if (isPlainObject(parent)) {
parent = parent[key];
} else if (isSet(parent)) {
const row = +key;
parent = getNthKey(parent, row, context);
} else if (isMap(parent)) {
const isEnd = i === path.length - 2;
if (isEnd) {
break;
}
const row = +key;
const type = +path[++i] === 0 ? 'key' : 'value';
const keyOfRow = getNthKey(parent, row, context);
switch (type) {
case 'key':
parent = keyOfRow;
break;
case 'value':
parent = parent.get(keyOfRow);
break;
}
}
}
const lastKey = path[path.length - 1];
if (isArray(parent)) {
parent[+lastKey] = mapper(parent[+lastKey]);
} else if (isPlainObject(parent)) {
parent[lastKey] = mapper(parent[lastKey]);
}
if (isSet(parent)) {
const row = +lastKey;
const oldValue = getNthKey(parent, row, context);
const newValue = mapper(oldValue);
if (oldValue !== newValue) {
parent.delete(oldValue);
parent.add(newValue);
const currentContext = context.get(parent);
if (currentContext) {
currentContext[row] = newValue;
}
}
}
if (isMap(parent)) {
const row = +path[path.length - 2];
const keyToRow = getNthKey(parent, row, context);
const type = +lastKey === 0 ? 'key' : 'value';
switch (type) {
case 'key': {
const newKey = mapper(keyToRow);
parent.set(newKey, parent.get(keyToRow));
if (newKey !== keyToRow) {
parent.delete(keyToRow);
const currentContext = context.get(parent);
if (currentContext) {
currentContext[row] = newKey;
}
}
break;
}
case 'value': {
parent.set(keyToRow, mapper(parent.get(keyToRow)));
break;
}
}
}
return object;
};