Initial commit
[yaffs-website] / node_modules / lodash.assign / index.js
1 /**
2  * lodash (Custom Build) <https://lodash.com/>
3  * Build: `lodash modularize exports="npm" -o ./`
4  * Copyright jQuery Foundation and other contributors <https://jquery.org/>
5  * Released under MIT license <https://lodash.com/license>
6  * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
7  * Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
8  */
9
10 /** Used as references for various `Number` constants. */
11 var MAX_SAFE_INTEGER = 9007199254740991;
12
13 /** `Object#toString` result references. */
14 var argsTag = '[object Arguments]',
15     funcTag = '[object Function]',
16     genTag = '[object GeneratorFunction]';
17
18 /** Used to detect unsigned integer values. */
19 var reIsUint = /^(?:0|[1-9]\d*)$/;
20
21 /**
22  * A faster alternative to `Function#apply`, this function invokes `func`
23  * with the `this` binding of `thisArg` and the arguments of `args`.
24  *
25  * @private
26  * @param {Function} func The function to invoke.
27  * @param {*} thisArg The `this` binding of `func`.
28  * @param {Array} args The arguments to invoke `func` with.
29  * @returns {*} Returns the result of `func`.
30  */
31 function apply(func, thisArg, args) {
32   switch (args.length) {
33     case 0: return func.call(thisArg);
34     case 1: return func.call(thisArg, args[0]);
35     case 2: return func.call(thisArg, args[0], args[1]);
36     case 3: return func.call(thisArg, args[0], args[1], args[2]);
37   }
38   return func.apply(thisArg, args);
39 }
40
41 /**
42  * The base implementation of `_.times` without support for iteratee shorthands
43  * or max array length checks.
44  *
45  * @private
46  * @param {number} n The number of times to invoke `iteratee`.
47  * @param {Function} iteratee The function invoked per iteration.
48  * @returns {Array} Returns the array of results.
49  */
50 function baseTimes(n, iteratee) {
51   var index = -1,
52       result = Array(n);
53
54   while (++index < n) {
55     result[index] = iteratee(index);
56   }
57   return result;
58 }
59
60 /**
61  * Creates a unary function that invokes `func` with its argument transformed.
62  *
63  * @private
64  * @param {Function} func The function to wrap.
65  * @param {Function} transform The argument transform.
66  * @returns {Function} Returns the new function.
67  */
68 function overArg(func, transform) {
69   return function(arg) {
70     return func(transform(arg));
71   };
72 }
73
74 /** Used for built-in method references. */
75 var objectProto = Object.prototype;
76
77 /** Used to check objects for own properties. */
78 var hasOwnProperty = objectProto.hasOwnProperty;
79
80 /**
81  * Used to resolve the
82  * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
83  * of values.
84  */
85 var objectToString = objectProto.toString;
86
87 /** Built-in value references. */
88 var propertyIsEnumerable = objectProto.propertyIsEnumerable;
89
90 /* Built-in method references for those with the same name as other `lodash` methods. */
91 var nativeKeys = overArg(Object.keys, Object),
92     nativeMax = Math.max;
93
94 /** Detect if properties shadowing those on `Object.prototype` are non-enumerable. */
95 var nonEnumShadows = !propertyIsEnumerable.call({ 'valueOf': 1 }, 'valueOf');
96
97 /**
98  * Creates an array of the enumerable property names of the array-like `value`.
99  *
100  * @private
101  * @param {*} value The value to query.
102  * @param {boolean} inherited Specify returning inherited property names.
103  * @returns {Array} Returns the array of property names.
104  */
105 function arrayLikeKeys(value, inherited) {
106   // Safari 8.1 makes `arguments.callee` enumerable in strict mode.
107   // Safari 9 makes `arguments.length` enumerable in strict mode.
108   var result = (isArray(value) || isArguments(value))
109     ? baseTimes(value.length, String)
110     : [];
111
112   var length = result.length,
113       skipIndexes = !!length;
114
115   for (var key in value) {
116     if ((inherited || hasOwnProperty.call(value, key)) &&
117         !(skipIndexes && (key == 'length' || isIndex(key, length)))) {
118       result.push(key);
119     }
120   }
121   return result;
122 }
123
124 /**
125  * Assigns `value` to `key` of `object` if the existing value is not equivalent
126  * using [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)
127  * for equality comparisons.
128  *
129  * @private
130  * @param {Object} object The object to modify.
131  * @param {string} key The key of the property to assign.
132  * @param {*} value The value to assign.
133  */
134 function assignValue(object, key, value) {
135   var objValue = object[key];
136   if (!(hasOwnProperty.call(object, key) && eq(objValue, value)) ||
137       (value === undefined && !(key in object))) {
138     object[key] = value;
139   }
140 }
141
142 /**
143  * The base implementation of `_.keys` which doesn't treat sparse arrays as dense.
144  *
145  * @private
146  * @param {Object} object The object to query.
147  * @returns {Array} Returns the array of property names.
148  */
149 function baseKeys(object) {
150   if (!isPrototype(object)) {
151     return nativeKeys(object);
152   }
153   var result = [];
154   for (var key in Object(object)) {
155     if (hasOwnProperty.call(object, key) && key != 'constructor') {
156       result.push(key);
157     }
158   }
159   return result;
160 }
161
162 /**
163  * The base implementation of `_.rest` which doesn't validate or coerce arguments.
164  *
165  * @private
166  * @param {Function} func The function to apply a rest parameter to.
167  * @param {number} [start=func.length-1] The start position of the rest parameter.
168  * @returns {Function} Returns the new function.
169  */
170 function baseRest(func, start) {
171   start = nativeMax(start === undefined ? (func.length - 1) : start, 0);
172   return function() {
173     var args = arguments,
174         index = -1,
175         length = nativeMax(args.length - start, 0),
176         array = Array(length);
177
178     while (++index < length) {
179       array[index] = args[start + index];
180     }
181     index = -1;
182     var otherArgs = Array(start + 1);
183     while (++index < start) {
184       otherArgs[index] = args[index];
185     }
186     otherArgs[start] = array;
187     return apply(func, this, otherArgs);
188   };
189 }
190
191 /**
192  * Copies properties of `source` to `object`.
193  *
194  * @private
195  * @param {Object} source The object to copy properties from.
196  * @param {Array} props The property identifiers to copy.
197  * @param {Object} [object={}] The object to copy properties to.
198  * @param {Function} [customizer] The function to customize copied values.
199  * @returns {Object} Returns `object`.
200  */
201 function copyObject(source, props, object, customizer) {
202   object || (object = {});
203
204   var index = -1,
205       length = props.length;
206
207   while (++index < length) {
208     var key = props[index];
209
210     var newValue = customizer
211       ? customizer(object[key], source[key], key, object, source)
212       : undefined;
213
214     assignValue(object, key, newValue === undefined ? source[key] : newValue);
215   }
216   return object;
217 }
218
219 /**
220  * Creates a function like `_.assign`.
221  *
222  * @private
223  * @param {Function} assigner The function to assign values.
224  * @returns {Function} Returns the new assigner function.
225  */
226 function createAssigner(assigner) {
227   return baseRest(function(object, sources) {
228     var index = -1,
229         length = sources.length,
230         customizer = length > 1 ? sources[length - 1] : undefined,
231         guard = length > 2 ? sources[2] : undefined;
232
233     customizer = (assigner.length > 3 && typeof customizer == 'function')
234       ? (length--, customizer)
235       : undefined;
236
237     if (guard && isIterateeCall(sources[0], sources[1], guard)) {
238       customizer = length < 3 ? undefined : customizer;
239       length = 1;
240     }
241     object = Object(object);
242     while (++index < length) {
243       var source = sources[index];
244       if (source) {
245         assigner(object, source, index, customizer);
246       }
247     }
248     return object;
249   });
250 }
251
252 /**
253  * Checks if `value` is a valid array-like index.
254  *
255  * @private
256  * @param {*} value The value to check.
257  * @param {number} [length=MAX_SAFE_INTEGER] The upper bounds of a valid index.
258  * @returns {boolean} Returns `true` if `value` is a valid index, else `false`.
259  */
260 function isIndex(value, length) {
261   length = length == null ? MAX_SAFE_INTEGER : length;
262   return !!length &&
263     (typeof value == 'number' || reIsUint.test(value)) &&
264     (value > -1 && value % 1 == 0 && value < length);
265 }
266
267 /**
268  * Checks if the given arguments are from an iteratee call.
269  *
270  * @private
271  * @param {*} value The potential iteratee value argument.
272  * @param {*} index The potential iteratee index or key argument.
273  * @param {*} object The potential iteratee object argument.
274  * @returns {boolean} Returns `true` if the arguments are from an iteratee call,
275  *  else `false`.
276  */
277 function isIterateeCall(value, index, object) {
278   if (!isObject(object)) {
279     return false;
280   }
281   var type = typeof index;
282   if (type == 'number'
283         ? (isArrayLike(object) && isIndex(index, object.length))
284         : (type == 'string' && index in object)
285       ) {
286     return eq(object[index], value);
287   }
288   return false;
289 }
290
291 /**
292  * Checks if `value` is likely a prototype object.
293  *
294  * @private
295  * @param {*} value The value to check.
296  * @returns {boolean} Returns `true` if `value` is a prototype, else `false`.
297  */
298 function isPrototype(value) {
299   var Ctor = value && value.constructor,
300       proto = (typeof Ctor == 'function' && Ctor.prototype) || objectProto;
301
302   return value === proto;
303 }
304
305 /**
306  * Performs a
307  * [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)
308  * comparison between two values to determine if they are equivalent.
309  *
310  * @static
311  * @memberOf _
312  * @since 4.0.0
313  * @category Lang
314  * @param {*} value The value to compare.
315  * @param {*} other The other value to compare.
316  * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
317  * @example
318  *
319  * var object = { 'a': 1 };
320  * var other = { 'a': 1 };
321  *
322  * _.eq(object, object);
323  * // => true
324  *
325  * _.eq(object, other);
326  * // => false
327  *
328  * _.eq('a', 'a');
329  * // => true
330  *
331  * _.eq('a', Object('a'));
332  * // => false
333  *
334  * _.eq(NaN, NaN);
335  * // => true
336  */
337 function eq(value, other) {
338   return value === other || (value !== value && other !== other);
339 }
340
341 /**
342  * Checks if `value` is likely an `arguments` object.
343  *
344  * @static
345  * @memberOf _
346  * @since 0.1.0
347  * @category Lang
348  * @param {*} value The value to check.
349  * @returns {boolean} Returns `true` if `value` is an `arguments` object,
350  *  else `false`.
351  * @example
352  *
353  * _.isArguments(function() { return arguments; }());
354  * // => true
355  *
356  * _.isArguments([1, 2, 3]);
357  * // => false
358  */
359 function isArguments(value) {
360   // Safari 8.1 makes `arguments.callee` enumerable in strict mode.
361   return isArrayLikeObject(value) && hasOwnProperty.call(value, 'callee') &&
362     (!propertyIsEnumerable.call(value, 'callee') || objectToString.call(value) == argsTag);
363 }
364
365 /**
366  * Checks if `value` is classified as an `Array` object.
367  *
368  * @static
369  * @memberOf _
370  * @since 0.1.0
371  * @category Lang
372  * @param {*} value The value to check.
373  * @returns {boolean} Returns `true` if `value` is an array, else `false`.
374  * @example
375  *
376  * _.isArray([1, 2, 3]);
377  * // => true
378  *
379  * _.isArray(document.body.children);
380  * // => false
381  *
382  * _.isArray('abc');
383  * // => false
384  *
385  * _.isArray(_.noop);
386  * // => false
387  */
388 var isArray = Array.isArray;
389
390 /**
391  * Checks if `value` is array-like. A value is considered array-like if it's
392  * not a function and has a `value.length` that's an integer greater than or
393  * equal to `0` and less than or equal to `Number.MAX_SAFE_INTEGER`.
394  *
395  * @static
396  * @memberOf _
397  * @since 4.0.0
398  * @category Lang
399  * @param {*} value The value to check.
400  * @returns {boolean} Returns `true` if `value` is array-like, else `false`.
401  * @example
402  *
403  * _.isArrayLike([1, 2, 3]);
404  * // => true
405  *
406  * _.isArrayLike(document.body.children);
407  * // => true
408  *
409  * _.isArrayLike('abc');
410  * // => true
411  *
412  * _.isArrayLike(_.noop);
413  * // => false
414  */
415 function isArrayLike(value) {
416   return value != null && isLength(value.length) && !isFunction(value);
417 }
418
419 /**
420  * This method is like `_.isArrayLike` except that it also checks if `value`
421  * is an object.
422  *
423  * @static
424  * @memberOf _
425  * @since 4.0.0
426  * @category Lang
427  * @param {*} value The value to check.
428  * @returns {boolean} Returns `true` if `value` is an array-like object,
429  *  else `false`.
430  * @example
431  *
432  * _.isArrayLikeObject([1, 2, 3]);
433  * // => true
434  *
435  * _.isArrayLikeObject(document.body.children);
436  * // => true
437  *
438  * _.isArrayLikeObject('abc');
439  * // => false
440  *
441  * _.isArrayLikeObject(_.noop);
442  * // => false
443  */
444 function isArrayLikeObject(value) {
445   return isObjectLike(value) && isArrayLike(value);
446 }
447
448 /**
449  * Checks if `value` is classified as a `Function` object.
450  *
451  * @static
452  * @memberOf _
453  * @since 0.1.0
454  * @category Lang
455  * @param {*} value The value to check.
456  * @returns {boolean} Returns `true` if `value` is a function, else `false`.
457  * @example
458  *
459  * _.isFunction(_);
460  * // => true
461  *
462  * _.isFunction(/abc/);
463  * // => false
464  */
465 function isFunction(value) {
466   // The use of `Object#toString` avoids issues with the `typeof` operator
467   // in Safari 8-9 which returns 'object' for typed array and other constructors.
468   var tag = isObject(value) ? objectToString.call(value) : '';
469   return tag == funcTag || tag == genTag;
470 }
471
472 /**
473  * Checks if `value` is a valid array-like length.
474  *
475  * **Note:** This method is loosely based on
476  * [`ToLength`](http://ecma-international.org/ecma-262/7.0/#sec-tolength).
477  *
478  * @static
479  * @memberOf _
480  * @since 4.0.0
481  * @category Lang
482  * @param {*} value The value to check.
483  * @returns {boolean} Returns `true` if `value` is a valid length, else `false`.
484  * @example
485  *
486  * _.isLength(3);
487  * // => true
488  *
489  * _.isLength(Number.MIN_VALUE);
490  * // => false
491  *
492  * _.isLength(Infinity);
493  * // => false
494  *
495  * _.isLength('3');
496  * // => false
497  */
498 function isLength(value) {
499   return typeof value == 'number' &&
500     value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
501 }
502
503 /**
504  * Checks if `value` is the
505  * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
506  * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
507  *
508  * @static
509  * @memberOf _
510  * @since 0.1.0
511  * @category Lang
512  * @param {*} value The value to check.
513  * @returns {boolean} Returns `true` if `value` is an object, else `false`.
514  * @example
515  *
516  * _.isObject({});
517  * // => true
518  *
519  * _.isObject([1, 2, 3]);
520  * // => true
521  *
522  * _.isObject(_.noop);
523  * // => true
524  *
525  * _.isObject(null);
526  * // => false
527  */
528 function isObject(value) {
529   var type = typeof value;
530   return !!value && (type == 'object' || type == 'function');
531 }
532
533 /**
534  * Checks if `value` is object-like. A value is object-like if it's not `null`
535  * and has a `typeof` result of "object".
536  *
537  * @static
538  * @memberOf _
539  * @since 4.0.0
540  * @category Lang
541  * @param {*} value The value to check.
542  * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
543  * @example
544  *
545  * _.isObjectLike({});
546  * // => true
547  *
548  * _.isObjectLike([1, 2, 3]);
549  * // => true
550  *
551  * _.isObjectLike(_.noop);
552  * // => false
553  *
554  * _.isObjectLike(null);
555  * // => false
556  */
557 function isObjectLike(value) {
558   return !!value && typeof value == 'object';
559 }
560
561 /**
562  * Assigns own enumerable string keyed properties of source objects to the
563  * destination object. Source objects are applied from left to right.
564  * Subsequent sources overwrite property assignments of previous sources.
565  *
566  * **Note:** This method mutates `object` and is loosely based on
567  * [`Object.assign`](https://mdn.io/Object/assign).
568  *
569  * @static
570  * @memberOf _
571  * @since 0.10.0
572  * @category Object
573  * @param {Object} object The destination object.
574  * @param {...Object} [sources] The source objects.
575  * @returns {Object} Returns `object`.
576  * @see _.assignIn
577  * @example
578  *
579  * function Foo() {
580  *   this.a = 1;
581  * }
582  *
583  * function Bar() {
584  *   this.c = 3;
585  * }
586  *
587  * Foo.prototype.b = 2;
588  * Bar.prototype.d = 4;
589  *
590  * _.assign({ 'a': 0 }, new Foo, new Bar);
591  * // => { 'a': 1, 'c': 3 }
592  */
593 var assign = createAssigner(function(object, source) {
594   if (nonEnumShadows || isPrototype(source) || isArrayLike(source)) {
595     copyObject(source, keys(source), object);
596     return;
597   }
598   for (var key in source) {
599     if (hasOwnProperty.call(source, key)) {
600       assignValue(object, key, source[key]);
601     }
602   }
603 });
604
605 /**
606  * Creates an array of the own enumerable property names of `object`.
607  *
608  * **Note:** Non-object values are coerced to objects. See the
609  * [ES spec](http://ecma-international.org/ecma-262/7.0/#sec-object.keys)
610  * for more details.
611  *
612  * @static
613  * @since 0.1.0
614  * @memberOf _
615  * @category Object
616  * @param {Object} object The object to query.
617  * @returns {Array} Returns the array of property names.
618  * @example
619  *
620  * function Foo() {
621  *   this.a = 1;
622  *   this.b = 2;
623  * }
624  *
625  * Foo.prototype.c = 3;
626  *
627  * _.keys(new Foo);
628  * // => ['a', 'b'] (iteration order is not guaranteed)
629  *
630  * _.keys('hi');
631  * // => ['0', '1']
632  */
633 function keys(object) {
634   return isArrayLike(object) ? arrayLikeKeys(object) : baseKeys(object);
635 }
636
637 module.exports = assign;