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.

637 lines
16 KiB

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