1 | function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
2 |
|
3 | import { isBigNumber } from './is';
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | export function clone(x) {
|
17 | var type = _typeof(x);
|
18 |
|
19 |
|
20 | if (type === 'number' || type === 'string' || type === 'boolean' || x === null || x === undefined) {
|
21 | return x;
|
22 | }
|
23 |
|
24 |
|
25 | if (typeof x.clone === 'function') {
|
26 | return x.clone();
|
27 | }
|
28 |
|
29 |
|
30 | if (Array.isArray(x)) {
|
31 | return x.map(function (value) {
|
32 | return clone(value);
|
33 | });
|
34 | }
|
35 |
|
36 | if (x instanceof Date) return new Date(x.valueOf());
|
37 | if (isBigNumber(x)) return x;
|
38 |
|
39 | if (x instanceof RegExp) throw new TypeError('Cannot clone ' + x);
|
40 |
|
41 |
|
42 | return mapObject(x, clone);
|
43 | }
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | export function mapObject(object, callback) {
|
52 | var clone = {};
|
53 |
|
54 | for (var key in object) {
|
55 | if (hasOwnProperty(object, key)) {
|
56 | clone[key] = callback(object[key]);
|
57 | }
|
58 | }
|
59 |
|
60 | return clone;
|
61 | }
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | export function extend(a, b) {
|
70 | for (var prop in b) {
|
71 | if (hasOwnProperty(b, prop)) {
|
72 | a[prop] = b[prop];
|
73 | }
|
74 | }
|
75 |
|
76 | return a;
|
77 | }
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | export function deepExtend(a, b) {
|
86 |
|
87 | if (Array.isArray(b)) {
|
88 | throw new TypeError('Arrays are not supported by deepExtend');
|
89 | }
|
90 |
|
91 | for (var prop in b) {
|
92 | if (hasOwnProperty(b, prop)) {
|
93 | if (b[prop] && b[prop].constructor === Object) {
|
94 | if (a[prop] === undefined) {
|
95 | a[prop] = {};
|
96 | }
|
97 |
|
98 | if (a[prop] && a[prop].constructor === Object) {
|
99 | deepExtend(a[prop], b[prop]);
|
100 | } else {
|
101 | a[prop] = b[prop];
|
102 | }
|
103 | } else if (Array.isArray(b[prop])) {
|
104 | throw new TypeError('Arrays are not supported by deepExtend');
|
105 | } else {
|
106 | a[prop] = b[prop];
|
107 | }
|
108 | }
|
109 | }
|
110 |
|
111 | return a;
|
112 | }
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | export function deepStrictEqual(a, b) {
|
122 | var prop, i, len;
|
123 |
|
124 | if (Array.isArray(a)) {
|
125 | if (!Array.isArray(b)) {
|
126 | return false;
|
127 | }
|
128 |
|
129 | if (a.length !== b.length) {
|
130 | return false;
|
131 | }
|
132 |
|
133 | for (i = 0, len = a.length; i < len; i++) {
|
134 | if (!deepStrictEqual(a[i], b[i])) {
|
135 | return false;
|
136 | }
|
137 | }
|
138 |
|
139 | return true;
|
140 | } else if (typeof a === 'function') {
|
141 | return a === b;
|
142 | } else if (a instanceof Object) {
|
143 | if (Array.isArray(b) || !(b instanceof Object)) {
|
144 | return false;
|
145 | }
|
146 |
|
147 | for (prop in a) {
|
148 |
|
149 | if (!(prop in b) || !deepStrictEqual(a[prop], b[prop])) {
|
150 | return false;
|
151 | }
|
152 | }
|
153 |
|
154 | for (prop in b) {
|
155 |
|
156 | if (!(prop in a) || !deepStrictEqual(a[prop], b[prop])) {
|
157 | return false;
|
158 | }
|
159 | }
|
160 |
|
161 | return true;
|
162 | } else {
|
163 | return a === b;
|
164 | }
|
165 | }
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | export function deepFlatten(nestedObject) {
|
173 | var flattenedObject = {};
|
174 |
|
175 | _deepFlatten(nestedObject, flattenedObject);
|
176 |
|
177 | return flattenedObject;
|
178 | }
|
179 |
|
180 | function _deepFlatten(nestedObject, flattenedObject) {
|
181 | for (var prop in nestedObject) {
|
182 | if (hasOwnProperty(nestedObject, prop)) {
|
183 | var value = nestedObject[prop];
|
184 |
|
185 | if (_typeof(value) === 'object' && value !== null) {
|
186 | _deepFlatten(value, flattenedObject);
|
187 | } else {
|
188 | flattenedObject[prop] = value;
|
189 | }
|
190 | }
|
191 | }
|
192 | }
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 | export function canDefineProperty() {
|
200 |
|
201 | try {
|
202 | if (Object.defineProperty) {
|
203 | Object.defineProperty({}, 'x', {
|
204 | get: function get() {}
|
205 | });
|
206 | return true;
|
207 | }
|
208 | } catch (e) {}
|
209 |
|
210 | return false;
|
211 | }
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 | export function lazy(object, prop, valueResolver) {
|
223 | var _uninitialized = true;
|
224 |
|
225 | var _value;
|
226 |
|
227 | Object.defineProperty(object, prop, {
|
228 | get: function get() {
|
229 | if (_uninitialized) {
|
230 | _value = valueResolver();
|
231 | _uninitialized = false;
|
232 | }
|
233 |
|
234 | return _value;
|
235 | },
|
236 | set: function set(value) {
|
237 | _value = value;
|
238 | _uninitialized = false;
|
239 | },
|
240 | configurable: true,
|
241 | enumerable: true
|
242 | });
|
243 | }
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 | export function traverse(object, path) {
|
253 | if (path && typeof path === 'string') {
|
254 | return traverse(object, path.split('.'));
|
255 | }
|
256 |
|
257 | var obj = object;
|
258 |
|
259 | if (path) {
|
260 | for (var i = 0; i < path.length; i++) {
|
261 | var key = path[i];
|
262 |
|
263 | if (!(key in obj)) {
|
264 | obj[key] = {};
|
265 | }
|
266 |
|
267 | obj = obj[key];
|
268 | }
|
269 | }
|
270 |
|
271 | return obj;
|
272 | }
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 | export function hasOwnProperty(object, property) {
|
280 | return object && Object.hasOwnProperty.call(object, property);
|
281 | }
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 | export function isLegacyFactory(object) {
|
296 | return object && typeof object.factory === 'function';
|
297 | }
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 | export function get(object, path) {
|
306 | if (typeof path === 'string') {
|
307 | if (isPath(path)) {
|
308 | return get(object, path.split('.'));
|
309 | } else {
|
310 | return object[path];
|
311 | }
|
312 | }
|
313 |
|
314 | var child = object;
|
315 |
|
316 | for (var i = 0; i < path.length; i++) {
|
317 | var key = path[i];
|
318 | child = child ? child[key] : undefined;
|
319 | }
|
320 |
|
321 | return child;
|
322 | }
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 | export function set(object, path, value) {
|
334 | if (typeof path === 'string') {
|
335 | if (isPath(path)) {
|
336 | return set(object, path.split('.'), value);
|
337 | } else {
|
338 | object[path] = value;
|
339 | return object;
|
340 | }
|
341 | }
|
342 |
|
343 | var child = object;
|
344 |
|
345 | for (var i = 0; i < path.length - 1; i++) {
|
346 | var key = path[i];
|
347 |
|
348 | if (child[key] === undefined) {
|
349 | child[key] = {};
|
350 | }
|
351 |
|
352 | child = child[key];
|
353 | }
|
354 |
|
355 | if (path.length > 0) {
|
356 | var lastKey = path[path.length - 1];
|
357 | child[lastKey] = value;
|
358 | }
|
359 |
|
360 | return object;
|
361 | }
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 | export function pick(object, properties, transform) {
|
371 | var copy = {};
|
372 |
|
373 | for (var i = 0; i < properties.length; i++) {
|
374 | var key = properties[i];
|
375 | var value = get(object, key);
|
376 |
|
377 | if (value !== undefined) {
|
378 | set(copy, key, transform ? transform(value, key) : value);
|
379 | }
|
380 | }
|
381 |
|
382 | return copy;
|
383 | }
|
384 | /**
|
385 | * Shallow version of pick, creating an object composed of the picked object properties
|
386 | * but not for nested properties
|
387 | * @param {Object} object
|
388 | * @param {string[]} properties
|
389 | * @return {Object}
|
390 | */
|
391 |
|
392 | export function pickShallow(object, properties) {
|
393 | var copy = {};
|
394 |
|
395 | for (var i = 0; i < properties.length; i++) {
|
396 | var key = properties[i];
|
397 | var value = object[key];
|
398 |
|
399 | if (value !== undefined) {
|
400 | copy[key] = value;
|
401 | }
|
402 | }
|
403 |
|
404 | return copy;
|
405 | }
|
406 | export function values(object) {
|
407 | return Object.keys(object).map(function (key) {
|
408 | return object[key];
|
409 | });
|
410 | }
|
411 |
|
412 | function isPath(str) {
|
413 | return str.indexOf('.') !== -1;
|
414 | } |
\ | No newline at end of file |