index.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /**
  2. * lodash 4.3.2 (Custom Build) <https://lodash.com/>
  3. * Build: `lodash modularize exports="npm" -o ./`
  4. * Copyright 2012-2016 The Dojo Foundation <http://dojofoundation.org/>
  5. * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
  6. * Copyright 2009-2016 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
  7. * Available under MIT license <https://lodash.com/license>
  8. */
  9. var Stack = require('lodash._stack'),
  10. baseClone = require('lodash._baseclone'),
  11. isPlainObject = require('lodash.isplainobject'),
  12. keysIn = require('lodash.keysin'),
  13. mergeWith = require('lodash.mergewith'),
  14. rest = require('lodash.rest');
  15. /** Used as references for various `Number` constants. */
  16. var MAX_SAFE_INTEGER = 9007199254740991;
  17. /** `Object#toString` result references. */
  18. var argsTag = '[object Arguments]',
  19. arrayTag = '[object Array]',
  20. boolTag = '[object Boolean]',
  21. dateTag = '[object Date]',
  22. errorTag = '[object Error]',
  23. funcTag = '[object Function]',
  24. genTag = '[object GeneratorFunction]',
  25. mapTag = '[object Map]',
  26. numberTag = '[object Number]',
  27. objectTag = '[object Object]',
  28. regexpTag = '[object RegExp]',
  29. setTag = '[object Set]',
  30. stringTag = '[object String]',
  31. weakMapTag = '[object WeakMap]';
  32. var arrayBufferTag = '[object ArrayBuffer]',
  33. float32Tag = '[object Float32Array]',
  34. float64Tag = '[object Float64Array]',
  35. int8Tag = '[object Int8Array]',
  36. int16Tag = '[object Int16Array]',
  37. int32Tag = '[object Int32Array]',
  38. uint8Tag = '[object Uint8Array]',
  39. uint8ClampedTag = '[object Uint8ClampedArray]',
  40. uint16Tag = '[object Uint16Array]',
  41. uint32Tag = '[object Uint32Array]';
  42. /** Used to identify `toStringTag` values of typed arrays. */
  43. var typedArrayTags = {};
  44. typedArrayTags[float32Tag] = typedArrayTags[float64Tag] =
  45. typedArrayTags[int8Tag] = typedArrayTags[int16Tag] =
  46. typedArrayTags[int32Tag] = typedArrayTags[uint8Tag] =
  47. typedArrayTags[uint8ClampedTag] = typedArrayTags[uint16Tag] =
  48. typedArrayTags[uint32Tag] = true;
  49. typedArrayTags[argsTag] = typedArrayTags[arrayTag] =
  50. typedArrayTags[arrayBufferTag] = typedArrayTags[boolTag] =
  51. typedArrayTags[dateTag] = typedArrayTags[errorTag] =
  52. typedArrayTags[funcTag] = typedArrayTags[mapTag] =
  53. typedArrayTags[numberTag] = typedArrayTags[objectTag] =
  54. typedArrayTags[regexpTag] = typedArrayTags[setTag] =
  55. typedArrayTags[stringTag] = typedArrayTags[weakMapTag] = false;
  56. /**
  57. * A faster alternative to `Function#apply`, this function invokes `func`
  58. * with the `this` binding of `thisArg` and the arguments of `args`.
  59. *
  60. * @private
  61. * @param {Function} func The function to invoke.
  62. * @param {*} thisArg The `this` binding of `func`.
  63. * @param {...*} args The arguments to invoke `func` with.
  64. * @returns {*} Returns the result of `func`.
  65. */
  66. function apply(func, thisArg, args) {
  67. var length = args.length;
  68. switch (length) {
  69. case 0: return func.call(thisArg);
  70. case 1: return func.call(thisArg, args[0]);
  71. case 2: return func.call(thisArg, args[0], args[1]);
  72. case 3: return func.call(thisArg, args[0], args[1], args[2]);
  73. }
  74. return func.apply(thisArg, args);
  75. }
  76. /**
  77. * A specialized version of `_.forEach` for arrays without support for
  78. * iteratee shorthands.
  79. *
  80. * @private
  81. * @param {Array} array The array to iterate over.
  82. * @param {Function} iteratee The function invoked per iteration.
  83. * @returns {Array} Returns `array`.
  84. */
  85. function arrayEach(array, iteratee) {
  86. var index = -1,
  87. length = array.length;
  88. while (++index < length) {
  89. if (iteratee(array[index], index, array) === false) {
  90. break;
  91. }
  92. }
  93. return array;
  94. }
  95. /** Used for built-in method references. */
  96. var objectProto = Object.prototype;
  97. /** Used to check objects for own properties. */
  98. var hasOwnProperty = objectProto.hasOwnProperty;
  99. /**
  100. * Used to resolve the [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
  101. * of values.
  102. */
  103. var objectToString = objectProto.toString;
  104. /** Built-in value references. */
  105. var propertyIsEnumerable = objectProto.propertyIsEnumerable;
  106. /**
  107. * This function is like `assignValue` except that it doesn't assign
  108. * `undefined` values.
  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 assignMergeValue(object, key, value) {
  116. if ((value !== undefined && !eq(object[key], value)) ||
  117. (typeof key == 'number' && value === undefined && !(key in object))) {
  118. object[key] = value;
  119. }
  120. }
  121. /**
  122. * Assigns `value` to `key` of `object` if the existing value is not equivalent
  123. * using [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  124. * for equality comparisons.
  125. *
  126. * @private
  127. * @param {Object} object The object to modify.
  128. * @param {string} key The key of the property to assign.
  129. * @param {*} value The value to assign.
  130. */
  131. function assignValue(object, key, value) {
  132. var objValue = object[key];
  133. if (!(hasOwnProperty.call(object, key) && eq(objValue, value)) ||
  134. (value === undefined && !(key in object))) {
  135. object[key] = value;
  136. }
  137. }
  138. /**
  139. * The base implementation of `_.merge` without support for multiple sources.
  140. *
  141. * @private
  142. * @param {Object} object The destination object.
  143. * @param {Object} source The source object.
  144. * @param {number} srcIndex The index of `source`.
  145. * @param {Function} [customizer] The function to customize merged values.
  146. * @param {Object} [stack] Tracks traversed source values and their merged counterparts.
  147. */
  148. function baseMerge(object, source, srcIndex, customizer, stack) {
  149. if (object === source) {
  150. return;
  151. }
  152. var props = (isArray(source) || isTypedArray(source))
  153. ? undefined
  154. : keysIn(source);
  155. arrayEach(props || source, function(srcValue, key) {
  156. if (props) {
  157. key = srcValue;
  158. srcValue = source[key];
  159. }
  160. if (isObject(srcValue)) {
  161. stack || (stack = new Stack);
  162. baseMergeDeep(object, source, key, srcIndex, baseMerge, customizer, stack);
  163. }
  164. else {
  165. var newValue = customizer
  166. ? customizer(object[key], srcValue, (key + ''), object, source, stack)
  167. : undefined;
  168. if (newValue === undefined) {
  169. newValue = srcValue;
  170. }
  171. assignMergeValue(object, key, newValue);
  172. }
  173. });
  174. }
  175. /**
  176. * A specialized version of `baseMerge` for arrays and objects which performs
  177. * deep merges and tracks traversed objects enabling objects with circular
  178. * references to be merged.
  179. *
  180. * @private
  181. * @param {Object} object The destination object.
  182. * @param {Object} source The source object.
  183. * @param {string} key The key of the value to merge.
  184. * @param {number} srcIndex The index of `source`.
  185. * @param {Function} mergeFunc The function to merge values.
  186. * @param {Function} [customizer] The function to customize assigned values.
  187. * @param {Object} [stack] Tracks traversed source values and their merged counterparts.
  188. */
  189. function baseMergeDeep(object, source, key, srcIndex, mergeFunc, customizer, stack) {
  190. var objValue = object[key],
  191. srcValue = source[key],
  192. stacked = stack.get(srcValue);
  193. if (stacked) {
  194. assignMergeValue(object, key, stacked);
  195. return;
  196. }
  197. var newValue = customizer
  198. ? customizer(objValue, srcValue, (key + ''), object, source, stack)
  199. : undefined;
  200. var isCommon = newValue === undefined;
  201. if (isCommon) {
  202. newValue = srcValue;
  203. if (isArray(srcValue) || isTypedArray(srcValue)) {
  204. if (isArray(objValue)) {
  205. newValue = objValue;
  206. }
  207. else if (isArrayLikeObject(objValue)) {
  208. newValue = copyArray(objValue);
  209. }
  210. else {
  211. isCommon = false;
  212. newValue = baseClone(srcValue, !customizer);
  213. }
  214. }
  215. else if (isPlainObject(srcValue) || isArguments(srcValue)) {
  216. if (isArguments(objValue)) {
  217. newValue = toPlainObject(objValue);
  218. }
  219. else if (!isObject(objValue) || (srcIndex && isFunction(objValue))) {
  220. isCommon = false;
  221. newValue = baseClone(srcValue, !customizer);
  222. }
  223. else {
  224. newValue = objValue;
  225. }
  226. }
  227. else {
  228. isCommon = false;
  229. }
  230. }
  231. stack.set(srcValue, newValue);
  232. if (isCommon) {
  233. // Recursively merge objects and arrays (susceptible to call stack limits).
  234. mergeFunc(newValue, srcValue, srcIndex, customizer, stack);
  235. }
  236. stack['delete'](srcValue);
  237. assignMergeValue(object, key, newValue);
  238. }
  239. /**
  240. * The base implementation of `_.property` without support for deep paths.
  241. *
  242. * @private
  243. * @param {string} key The key of the property to get.
  244. * @returns {Function} Returns the new function.
  245. */
  246. function baseProperty(key) {
  247. return function(object) {
  248. return object == null ? undefined : object[key];
  249. };
  250. }
  251. /**
  252. * Copies the values of `source` to `array`.
  253. *
  254. * @private
  255. * @param {Array} source The array to copy values from.
  256. * @param {Array} [array=[]] The array to copy values to.
  257. * @returns {Array} Returns `array`.
  258. */
  259. function copyArray(source, array) {
  260. var index = -1,
  261. length = source.length;
  262. array || (array = Array(length));
  263. while (++index < length) {
  264. array[index] = source[index];
  265. }
  266. return array;
  267. }
  268. /**
  269. * Copies properties of `source` to `object`.
  270. *
  271. * @private
  272. * @param {Object} source The object to copy properties from.
  273. * @param {Array} props The property names to copy.
  274. * @param {Object} [object={}] The object to copy properties to.
  275. * @returns {Object} Returns `object`.
  276. */
  277. function copyObject(source, props, object) {
  278. return copyObjectWith(source, props, object);
  279. }
  280. /**
  281. * This function is like `copyObject` except that it accepts a function to
  282. * customize copied values.
  283. *
  284. * @private
  285. * @param {Object} source The object to copy properties from.
  286. * @param {Array} props The property names to copy.
  287. * @param {Object} [object={}] The object to copy properties to.
  288. * @param {Function} [customizer] The function to customize copied values.
  289. * @returns {Object} Returns `object`.
  290. */
  291. function copyObjectWith(source, props, object, customizer) {
  292. object || (object = {});
  293. var index = -1,
  294. length = props.length;
  295. while (++index < length) {
  296. var key = props[index];
  297. var newValue = customizer
  298. ? customizer(object[key], source[key], key, object, source)
  299. : source[key];
  300. assignValue(object, key, newValue);
  301. }
  302. return object;
  303. }
  304. /**
  305. * Gets the "length" property value of `object`.
  306. *
  307. * **Note:** This function is used to avoid a [JIT bug](https://bugs.webkit.org/show_bug.cgi?id=142792)
  308. * that affects Safari on at least iOS 8.1-8.3 ARM64.
  309. *
  310. * @private
  311. * @param {Object} object The object to query.
  312. * @returns {*} Returns the "length" value.
  313. */
  314. var getLength = baseProperty('length');
  315. /**
  316. * Used by `_.defaultsDeep` to customize its `_.merge` use.
  317. *
  318. * @private
  319. * @param {*} objValue The destination value.
  320. * @param {*} srcValue The source value.
  321. * @param {string} key The key of the property to merge.
  322. * @param {Object} object The parent object of `objValue`.
  323. * @param {Object} source The parent object of `srcValue`.
  324. * @param {Object} [stack] Tracks traversed source values and their merged counterparts.
  325. * @returns {*} Returns the value to assign.
  326. */
  327. function mergeDefaults(objValue, srcValue, key, object, source, stack) {
  328. if (isObject(objValue) && isObject(srcValue)) {
  329. baseMerge(objValue, srcValue, undefined, mergeDefaults, stack.set(srcValue, objValue));
  330. }
  331. return objValue;
  332. }
  333. /**
  334. * Performs a [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero)
  335. * comparison between two values to determine if they are equivalent.
  336. *
  337. * @static
  338. * @memberOf _
  339. * @category Lang
  340. * @param {*} value The value to compare.
  341. * @param {*} other The other value to compare.
  342. * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
  343. * @example
  344. *
  345. * var object = { 'user': 'fred' };
  346. * var other = { 'user': 'fred' };
  347. *
  348. * _.eq(object, object);
  349. * // => true
  350. *
  351. * _.eq(object, other);
  352. * // => false
  353. *
  354. * _.eq('a', 'a');
  355. * // => true
  356. *
  357. * _.eq('a', Object('a'));
  358. * // => false
  359. *
  360. * _.eq(NaN, NaN);
  361. * // => true
  362. */
  363. function eq(value, other) {
  364. return value === other || (value !== value && other !== other);
  365. }
  366. /**
  367. * Checks if `value` is likely an `arguments` object.
  368. *
  369. * @static
  370. * @memberOf _
  371. * @category Lang
  372. * @param {*} value The value to check.
  373. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  374. * @example
  375. *
  376. * _.isArguments(function() { return arguments; }());
  377. * // => true
  378. *
  379. * _.isArguments([1, 2, 3]);
  380. * // => false
  381. */
  382. function isArguments(value) {
  383. // Safari 8.1 incorrectly makes `arguments.callee` enumerable in strict mode.
  384. return isArrayLikeObject(value) && hasOwnProperty.call(value, 'callee') &&
  385. (!propertyIsEnumerable.call(value, 'callee') || objectToString.call(value) == argsTag);
  386. }
  387. /**
  388. * Checks if `value` is classified as an `Array` object.
  389. *
  390. * @static
  391. * @memberOf _
  392. * @type {Function}
  393. * @category Lang
  394. * @param {*} value The value to check.
  395. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  396. * @example
  397. *
  398. * _.isArray([1, 2, 3]);
  399. * // => true
  400. *
  401. * _.isArray(document.body.children);
  402. * // => false
  403. *
  404. * _.isArray('abc');
  405. * // => false
  406. *
  407. * _.isArray(_.noop);
  408. * // => false
  409. */
  410. var isArray = Array.isArray;
  411. /**
  412. * Checks if `value` is array-like. A value is considered array-like if it's
  413. * not a function and has a `value.length` that's an integer greater than or
  414. * equal to `0` and less than or equal to `Number.MAX_SAFE_INTEGER`.
  415. *
  416. * @static
  417. * @memberOf _
  418. * @category Lang
  419. * @param {*} value The value to check.
  420. * @returns {boolean} Returns `true` if `value` is array-like, else `false`.
  421. * @example
  422. *
  423. * _.isArrayLike([1, 2, 3]);
  424. * // => true
  425. *
  426. * _.isArrayLike(document.body.children);
  427. * // => true
  428. *
  429. * _.isArrayLike('abc');
  430. * // => true
  431. *
  432. * _.isArrayLike(_.noop);
  433. * // => false
  434. */
  435. function isArrayLike(value) {
  436. return value != null && isLength(getLength(value)) && !isFunction(value);
  437. }
  438. /**
  439. * This method is like `_.isArrayLike` except that it also checks if `value`
  440. * is an object.
  441. *
  442. * @static
  443. * @memberOf _
  444. * @category Lang
  445. * @param {*} value The value to check.
  446. * @returns {boolean} Returns `true` if `value` is an array-like object, else `false`.
  447. * @example
  448. *
  449. * _.isArrayLikeObject([1, 2, 3]);
  450. * // => true
  451. *
  452. * _.isArrayLikeObject(document.body.children);
  453. * // => true
  454. *
  455. * _.isArrayLikeObject('abc');
  456. * // => false
  457. *
  458. * _.isArrayLikeObject(_.noop);
  459. * // => false
  460. */
  461. function isArrayLikeObject(value) {
  462. return isObjectLike(value) && isArrayLike(value);
  463. }
  464. /**
  465. * Checks if `value` is classified as a `Function` object.
  466. *
  467. * @static
  468. * @memberOf _
  469. * @category Lang
  470. * @param {*} value The value to check.
  471. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  472. * @example
  473. *
  474. * _.isFunction(_);
  475. * // => true
  476. *
  477. * _.isFunction(/abc/);
  478. * // => false
  479. */
  480. function isFunction(value) {
  481. // The use of `Object#toString` avoids issues with the `typeof` operator
  482. // in Safari 8 which returns 'object' for typed array and weak map constructors,
  483. // and PhantomJS 1.9 which returns 'function' for `NodeList` instances.
  484. var tag = isObject(value) ? objectToString.call(value) : '';
  485. return tag == funcTag || tag == genTag;
  486. }
  487. /**
  488. * Checks if `value` is a valid array-like length.
  489. *
  490. * **Note:** This function is loosely based on [`ToLength`](http://ecma-international.org/ecma-262/6.0/#sec-tolength).
  491. *
  492. * @static
  493. * @memberOf _
  494. * @category Lang
  495. * @param {*} value The value to check.
  496. * @returns {boolean} Returns `true` if `value` is a valid length, else `false`.
  497. * @example
  498. *
  499. * _.isLength(3);
  500. * // => true
  501. *
  502. * _.isLength(Number.MIN_VALUE);
  503. * // => false
  504. *
  505. * _.isLength(Infinity);
  506. * // => false
  507. *
  508. * _.isLength('3');
  509. * // => false
  510. */
  511. function isLength(value) {
  512. return typeof value == 'number' &&
  513. value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
  514. }
  515. /**
  516. * Checks if `value` is the [language type](https://es5.github.io/#x8) of `Object`.
  517. * (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  518. *
  519. * @static
  520. * @memberOf _
  521. * @category Lang
  522. * @param {*} value The value to check.
  523. * @returns {boolean} Returns `true` if `value` is an object, else `false`.
  524. * @example
  525. *
  526. * _.isObject({});
  527. * // => true
  528. *
  529. * _.isObject([1, 2, 3]);
  530. * // => true
  531. *
  532. * _.isObject(_.noop);
  533. * // => true
  534. *
  535. * _.isObject(null);
  536. * // => false
  537. */
  538. function isObject(value) {
  539. var type = typeof value;
  540. return !!value && (type == 'object' || type == 'function');
  541. }
  542. /**
  543. * Checks if `value` is object-like. A value is object-like if it's not `null`
  544. * and has a `typeof` result of "object".
  545. *
  546. * @static
  547. * @memberOf _
  548. * @category Lang
  549. * @param {*} value The value to check.
  550. * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
  551. * @example
  552. *
  553. * _.isObjectLike({});
  554. * // => true
  555. *
  556. * _.isObjectLike([1, 2, 3]);
  557. * // => true
  558. *
  559. * _.isObjectLike(_.noop);
  560. * // => false
  561. *
  562. * _.isObjectLike(null);
  563. * // => false
  564. */
  565. function isObjectLike(value) {
  566. return !!value && typeof value == 'object';
  567. }
  568. /**
  569. * Checks if `value` is classified as a typed array.
  570. *
  571. * @static
  572. * @memberOf _
  573. * @category Lang
  574. * @param {*} value The value to check.
  575. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  576. * @example
  577. *
  578. * _.isTypedArray(new Uint8Array);
  579. * // => true
  580. *
  581. * _.isTypedArray([]);
  582. * // => false
  583. */
  584. function isTypedArray(value) {
  585. return isObjectLike(value) &&
  586. isLength(value.length) && !!typedArrayTags[objectToString.call(value)];
  587. }
  588. /**
  589. * Converts `value` to a plain object flattening inherited enumerable
  590. * properties of `value` to own properties of the plain object.
  591. *
  592. * @static
  593. * @memberOf _
  594. * @category Lang
  595. * @param {*} value The value to convert.
  596. * @returns {Object} Returns the converted plain object.
  597. * @example
  598. *
  599. * function Foo() {
  600. * this.b = 2;
  601. * }
  602. *
  603. * Foo.prototype.c = 3;
  604. *
  605. * _.assign({ 'a': 1 }, new Foo);
  606. * // => { 'a': 1, 'b': 2 }
  607. *
  608. * _.assign({ 'a': 1 }, _.toPlainObject(new Foo));
  609. * // => { 'a': 1, 'b': 2, 'c': 3 }
  610. */
  611. function toPlainObject(value) {
  612. return copyObject(value, keysIn(value));
  613. }
  614. /**
  615. * This method is like `_.defaults` except that it recursively assigns
  616. * default properties.
  617. *
  618. * **Note:** This method mutates `object`.
  619. *
  620. * @static
  621. * @memberOf _
  622. * @category Object
  623. * @param {Object} object The destination object.
  624. * @param {...Object} [sources] The source objects.
  625. * @returns {Object} Returns `object`.
  626. * @example
  627. *
  628. * _.defaultsDeep({ 'user': { 'name': 'barney' } }, { 'user': { 'name': 'fred', 'age': 36 } });
  629. * // => { 'user': { 'name': 'barney', 'age': 36 } }
  630. *
  631. */
  632. var defaultsDeep = rest(function(args) {
  633. args.push(undefined, mergeDefaults);
  634. return apply(mergeWith, undefined, args);
  635. });
  636. module.exports = defaultsDeep;