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