index.js 17 KB

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