index.js 16 KB

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