You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 16KB

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