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
10 /** Used as references for various `Number` constants. */
11 var MAX_SAFE_INTEGER = 9007199254740991;
13 /** `Object#toString` result references. */
14 var argsTag = '[object Arguments]',
15 funcTag = '[object Function]',
16 genTag = '[object GeneratorFunction]';
18 /** Used to detect unsigned integer values. */
19 var reIsUint = /^(?:0|[1-9]\d*)$/;
22 * A faster alternative to `Function#apply`, this function invokes `func`
23 * with the `this` binding of `thisArg` and the arguments of `args`.
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`.
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]);
38 return func.apply(thisArg, args);
42 * The base implementation of `_.times` without support for iteratee shorthands
43 * or max array length checks.
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.
50 function baseTimes(n, iteratee) {
55 result[index] = iteratee(index);
61 * Creates a unary function that invokes `func` with its argument transformed.
64 * @param {Function} func The function to wrap.
65 * @param {Function} transform The argument transform.
66 * @returns {Function} Returns the new function.
68 function overArg(func, transform) {
69 return function(arg) {
70 return func(transform(arg));
74 /** Used for built-in method references. */
75 var objectProto = Object.prototype;
77 /** Used to check objects for own properties. */
78 var hasOwnProperty = objectProto.hasOwnProperty;
82 * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
85 var objectToString = objectProto.toString;
87 /** Built-in value references. */
88 var propertyIsEnumerable = objectProto.propertyIsEnumerable;
90 /* Built-in method references for those with the same name as other `lodash` methods. */
91 var nativeKeys = overArg(Object.keys, Object),
94 /** Detect if properties shadowing those on `Object.prototype` are non-enumerable. */
95 var nonEnumShadows = !propertyIsEnumerable.call({ 'valueOf': 1 }, 'valueOf');
98 * Creates an array of the enumerable property names of the array-like `value`.
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.
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)
112 var length = result.length,
113 skipIndexes = !!length;
115 for (var key in value) {
116 if ((inherited || hasOwnProperty.call(value, key)) &&
117 !(skipIndexes && (key == 'length' || isIndex(key, length)))) {
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.
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.
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))) {
143 * The base implementation of `_.keys` which doesn't treat sparse arrays as dense.
146 * @param {Object} object The object to query.
147 * @returns {Array} Returns the array of property names.
149 function baseKeys(object) {
150 if (!isPrototype(object)) {
151 return nativeKeys(object);
154 for (var key in Object(object)) {
155 if (hasOwnProperty.call(object, key) && key != 'constructor') {
163 * The base implementation of `_.rest` which doesn't validate or coerce arguments.
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.
170 function baseRest(func, start) {
171 start = nativeMax(start === undefined ? (func.length - 1) : start, 0);
173 var args = arguments,
175 length = nativeMax(args.length - start, 0),
176 array = Array(length);
178 while (++index < length) {
179 array[index] = args[start + index];
182 var otherArgs = Array(start + 1);
183 while (++index < start) {
184 otherArgs[index] = args[index];
186 otherArgs[start] = array;
187 return apply(func, this, otherArgs);
192 * Copies properties of `source` to `object`.
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`.
201 function copyObject(source, props, object, customizer) {
202 object || (object = {});
205 length = props.length;
207 while (++index < length) {
208 var key = props[index];
210 var newValue = customizer
211 ? customizer(object[key], source[key], key, object, source)
214 assignValue(object, key, newValue === undefined ? source[key] : newValue);
220 * Creates a function like `_.assign`.
223 * @param {Function} assigner The function to assign values.
224 * @returns {Function} Returns the new assigner function.
226 function createAssigner(assigner) {
227 return baseRest(function(object, sources) {
229 length = sources.length,
230 customizer = length > 1 ? sources[length - 1] : undefined,
231 guard = length > 2 ? sources[2] : undefined;
233 customizer = (assigner.length > 3 && typeof customizer == 'function')
234 ? (length--, customizer)
237 if (guard && isIterateeCall(sources[0], sources[1], guard)) {
238 customizer = length < 3 ? undefined : customizer;
241 object = Object(object);
242 while (++index < length) {
243 var source = sources[index];
245 assigner(object, source, index, customizer);
253 * Checks if `value` is a valid array-like index.
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`.
260 function isIndex(value, length) {
261 length = length == null ? MAX_SAFE_INTEGER : length;
263 (typeof value == 'number' || reIsUint.test(value)) &&
264 (value > -1 && value % 1 == 0 && value < length);
268 * Checks if the given arguments are from an iteratee call.
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,
277 function isIterateeCall(value, index, object) {
278 if (!isObject(object)) {
281 var type = typeof index;
283 ? (isArrayLike(object) && isIndex(index, object.length))
284 : (type == 'string' && index in object)
286 return eq(object[index], value);
292 * Checks if `value` is likely a prototype object.
295 * @param {*} value The value to check.
296 * @returns {boolean} Returns `true` if `value` is a prototype, else `false`.
298 function isPrototype(value) {
299 var Ctor = value && value.constructor,
300 proto = (typeof Ctor == 'function' && Ctor.prototype) || objectProto;
302 return value === proto;
307 * [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)
308 * comparison between two values to determine if they are equivalent.
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`.
319 * var object = { 'a': 1 };
320 * var other = { 'a': 1 };
322 * _.eq(object, object);
325 * _.eq(object, other);
331 * _.eq('a', Object('a'));
337 function eq(value, other) {
338 return value === other || (value !== value && other !== other);
342 * Checks if `value` is likely an `arguments` object.
348 * @param {*} value The value to check.
349 * @returns {boolean} Returns `true` if `value` is an `arguments` object,
353 * _.isArguments(function() { return arguments; }());
356 * _.isArguments([1, 2, 3]);
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);
366 * Checks if `value` is classified as an `Array` object.
372 * @param {*} value The value to check.
373 * @returns {boolean} Returns `true` if `value` is an array, else `false`.
376 * _.isArray([1, 2, 3]);
379 * _.isArray(document.body.children);
388 var isArray = Array.isArray;
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`.
399 * @param {*} value The value to check.
400 * @returns {boolean} Returns `true` if `value` is array-like, else `false`.
403 * _.isArrayLike([1, 2, 3]);
406 * _.isArrayLike(document.body.children);
409 * _.isArrayLike('abc');
412 * _.isArrayLike(_.noop);
415 function isArrayLike(value) {
416 return value != null && isLength(value.length) && !isFunction(value);
420 * This method is like `_.isArrayLike` except that it also checks if `value`
427 * @param {*} value The value to check.
428 * @returns {boolean} Returns `true` if `value` is an array-like object,
432 * _.isArrayLikeObject([1, 2, 3]);
435 * _.isArrayLikeObject(document.body.children);
438 * _.isArrayLikeObject('abc');
441 * _.isArrayLikeObject(_.noop);
444 function isArrayLikeObject(value) {
445 return isObjectLike(value) && isArrayLike(value);
449 * Checks if `value` is classified as a `Function` object.
455 * @param {*} value The value to check.
456 * @returns {boolean} Returns `true` if `value` is a function, else `false`.
462 * _.isFunction(/abc/);
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;
473 * Checks if `value` is a valid array-like length.
475 * **Note:** This method is loosely based on
476 * [`ToLength`](http://ecma-international.org/ecma-262/7.0/#sec-tolength).
482 * @param {*} value The value to check.
483 * @returns {boolean} Returns `true` if `value` is a valid length, else `false`.
489 * _.isLength(Number.MIN_VALUE);
492 * _.isLength(Infinity);
498 function isLength(value) {
499 return typeof value == 'number' &&
500 value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
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('')`)
512 * @param {*} value The value to check.
513 * @returns {boolean} Returns `true` if `value` is an object, else `false`.
519 * _.isObject([1, 2, 3]);
522 * _.isObject(_.noop);
528 function isObject(value) {
529 var type = typeof value;
530 return !!value && (type == 'object' || type == 'function');
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".
541 * @param {*} value The value to check.
542 * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
545 * _.isObjectLike({});
548 * _.isObjectLike([1, 2, 3]);
551 * _.isObjectLike(_.noop);
554 * _.isObjectLike(null);
557 function isObjectLike(value) {
558 return !!value && typeof value == 'object';
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.
566 * **Note:** This method mutates `object` and is loosely based on
567 * [`Object.assign`](https://mdn.io/Object/assign).
573 * @param {Object} object The destination object.
574 * @param {...Object} [sources] The source objects.
575 * @returns {Object} Returns `object`.
587 * Foo.prototype.b = 2;
588 * Bar.prototype.d = 4;
590 * _.assign({ 'a': 0 }, new Foo, new Bar);
591 * // => { 'a': 1, 'c': 3 }
593 var assign = createAssigner(function(object, source) {
594 if (nonEnumShadows || isPrototype(source) || isArrayLike(source)) {
595 copyObject(source, keys(source), object);
598 for (var key in source) {
599 if (hasOwnProperty.call(source, key)) {
600 assignValue(object, key, source[key]);
606 * Creates an array of the own enumerable property names of `object`.
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)
616 * @param {Object} object The object to query.
617 * @returns {Array} Returns the array of property names.
625 * Foo.prototype.c = 3;
628 * // => ['a', 'b'] (iteration order is not guaranteed)
633 function keys(object) {
634 return isArrayLike(object) ? arrayLikeKeys(object) : baseKeys(object);
637 module.exports = assign;