-
Notifications
You must be signed in to change notification settings - Fork 6
/
object.js
206 lines (160 loc) · 3.84 KB
/
object.js
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
import { forEach } from './collection.js';
import {
isObject,
isUndefined,
isDefined,
isNil
} from './lang.js';
/**
* Convenience wrapper for `Object.assign`.
*
* @param {Object} target
* @param {...Object} others
*
* @return {Object} the target
*/
export function assign(target, ...others) {
return Object.assign(target, ...others);
}
/**
* Sets a nested property of a given object to the specified value.
*
* This mutates the object and returns it.
*
* @template T
*
* @param {T} target The target of the set operation.
* @param {(string|number)[]} path The path to the nested value.
* @param {any} value The value to set.
*
* @return {T}
*/
export function set(target, path, value) {
let currentTarget = target;
forEach(path, function(key, idx) {
if (typeof key !== 'number' && typeof key !== 'string') {
throw new Error('illegal key type: ' + typeof key + '. Key should be of type number or string.');
}
if (key === 'constructor') {
throw new Error('illegal key: constructor');
}
if (key === '__proto__') {
throw new Error('illegal key: __proto__');
}
let nextKey = path[idx + 1];
let nextTarget = currentTarget[key];
if (isDefined(nextKey) && isNil(nextTarget)) {
nextTarget = currentTarget[key] = isNaN(+nextKey) ? {} : [];
}
if (isUndefined(nextKey)) {
if (isUndefined(value)) {
delete currentTarget[key];
} else {
currentTarget[key] = value;
}
} else {
currentTarget = nextTarget;
}
});
return target;
}
/**
* Gets a nested property of a given object.
*
* @param {Object} target The target of the get operation.
* @param {(string|number)[]} path The path to the nested value.
* @param {any} [defaultValue] The value to return if no value exists.
*
* @return {any}
*/
export function get(target, path, defaultValue) {
let currentTarget = target;
forEach(path, function(key) {
// accessing nil property yields <undefined>
if (isNil(currentTarget)) {
currentTarget = undefined;
return false;
}
currentTarget = currentTarget[key];
});
return isUndefined(currentTarget) ? defaultValue : currentTarget;
}
/**
* Pick properties from the given target.
*
* @template T
* @template {any[]} V
*
* @param {T} target
* @param {V} properties
*
* @return Pick<T, V>
*/
export function pick(target, properties) {
let result = {};
let obj = Object(target);
forEach(properties, function(prop) {
if (prop in obj) {
result[prop] = target[prop];
}
});
return result;
}
/**
* Pick all target properties, excluding the given ones.
*
* @template T
* @template {any[]} V
*
* @param {T} target
* @param {V} properties
*
* @return {Omit<T, V>} target
*/
export function omit(target, properties) {
let result = {};
let obj = Object(target);
forEach(obj, function(prop, key) {
if (properties.indexOf(key) === -1) {
result[key] = prop;
}
});
return result;
}
/**
* Recursively merge `...sources` into given target.
*
* Does support merging objects; does not support merging arrays.
*
* @param {Object} target
* @param {...Object} sources
*
* @return {Object} the target
*/
export function merge(target, ...sources) {
if (!sources.length) {
return target;
}
forEach(sources, function(source) {
// skip non-obj sources, i.e. null
if (!source || !isObject(source)) {
return;
}
forEach(source, function(sourceVal, key) {
if (key === '__proto__') {
return;
}
let targetVal = target[key];
if (isObject(sourceVal)) {
if (!isObject(targetVal)) {
// override target[key] with object
targetVal = {};
}
target[key] = merge(targetVal, sourceVal);
} else {
target[key] = sourceVal;
}
});
});
return target;
}