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.

1254 line
36 KiB

3 年之前
  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 the `TypeError` message for "Functions" methods. */
  10. var FUNC_ERROR_TEXT = 'Expected a function';
  11. /** Used as the internal argument placeholder. */
  12. var PLACEHOLDER = '__lodash_placeholder__';
  13. /** Used to compose bitmasks for function metadata. */
  14. var BIND_FLAG = 1,
  15. BIND_KEY_FLAG = 2,
  16. CURRY_BOUND_FLAG = 4,
  17. CURRY_FLAG = 8,
  18. CURRY_RIGHT_FLAG = 16,
  19. PARTIAL_FLAG = 32,
  20. PARTIAL_RIGHT_FLAG = 64,
  21. ARY_FLAG = 128,
  22. REARG_FLAG = 256,
  23. FLIP_FLAG = 512;
  24. /** Used as references for various `Number` constants. */
  25. var INFINITY = 1 / 0,
  26. MAX_SAFE_INTEGER = 9007199254740991,
  27. MAX_INTEGER = 1.7976931348623157e+308,
  28. NAN = 0 / 0;
  29. /** Used to associate wrap methods with their bit flags. */
  30. var wrapFlags = [
  31. ['ary', ARY_FLAG],
  32. ['bind', BIND_FLAG],
  33. ['bindKey', BIND_KEY_FLAG],
  34. ['curry', CURRY_FLAG],
  35. ['curryRight', CURRY_RIGHT_FLAG],
  36. ['flip', FLIP_FLAG],
  37. ['partial', PARTIAL_FLAG],
  38. ['partialRight', PARTIAL_RIGHT_FLAG],
  39. ['rearg', REARG_FLAG]
  40. ];
  41. /** `Object#toString` result references. */
  42. var funcTag = '[object Function]',
  43. genTag = '[object GeneratorFunction]',
  44. symbolTag = '[object Symbol]';
  45. /**
  46. * Used to match `RegExp`
  47. * [syntax characters](http://ecma-international.org/ecma-262/7.0/#sec-patterns).
  48. */
  49. var reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
  50. /** Used to match leading and trailing whitespace. */
  51. var reTrim = /^\s+|\s+$/g;
  52. /** Used to match wrap detail comments. */
  53. var reWrapComment = /\{(?:\n\/\* \[wrapped with .+\] \*\/)?\n?/,
  54. reWrapDetails = /\{\n\/\* \[wrapped with (.+)\] \*/,
  55. reSplitDetails = /,? & /;
  56. /** Used to detect bad signed hexadecimal string values. */
  57. var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
  58. /** Used to detect binary string values. */
  59. var reIsBinary = /^0b[01]+$/i;
  60. /** Used to detect host constructors (Safari). */
  61. var reIsHostCtor = /^\[object .+?Constructor\]$/;
  62. /** Used to detect octal string values. */
  63. var reIsOctal = /^0o[0-7]+$/i;
  64. /** Used to detect unsigned integer values. */
  65. var reIsUint = /^(?:0|[1-9]\d*)$/;
  66. /** Built-in method references without a dependency on `root`. */
  67. var freeParseInt = parseInt;
  68. /** Detect free variable `global` from Node.js. */
  69. var freeGlobal = typeof global == 'object' && global && global.Object === Object && global;
  70. /** Detect free variable `self`. */
  71. var freeSelf = typeof self == 'object' && self && self.Object === Object && self;
  72. /** Used as a reference to the global object. */
  73. var root = freeGlobal || freeSelf || Function('return this')();
  74. /**
  75. * A faster alternative to `Function#apply`, this function invokes `func`
  76. * with the `this` binding of `thisArg` and the arguments of `args`.
  77. *
  78. * @private
  79. * @param {Function} func The function to invoke.
  80. * @param {*} thisArg The `this` binding of `func`.
  81. * @param {Array} args The arguments to invoke `func` with.
  82. * @returns {*} Returns the result of `func`.
  83. */
  84. function apply(func, thisArg, args) {
  85. switch (args.length) {
  86. case 0: return func.call(thisArg);
  87. case 1: return func.call(thisArg, args[0]);
  88. case 2: return func.call(thisArg, args[0], args[1]);
  89. case 3: return func.call(thisArg, args[0], args[1], args[2]);
  90. }
  91. return func.apply(thisArg, args);
  92. }
  93. /**
  94. * A specialized version of `_.forEach` for arrays without support for
  95. * iteratee shorthands.
  96. *
  97. * @private
  98. * @param {Array} [array] The array to iterate over.
  99. * @param {Function} iteratee The function invoked per iteration.
  100. * @returns {Array} Returns `array`.
  101. */
  102. function arrayEach(array, iteratee) {
  103. var index = -1,
  104. length = array ? array.length : 0;
  105. while (++index < length) {
  106. if (iteratee(array[index], index, array) === false) {
  107. break;
  108. }
  109. }
  110. return array;
  111. }
  112. /**
  113. * A specialized version of `_.includes` for arrays without support for
  114. * specifying an index to search from.
  115. *
  116. * @private
  117. * @param {Array} [array] The array to inspect.
  118. * @param {*} target The value to search for.
  119. * @returns {boolean} Returns `true` if `target` is found, else `false`.
  120. */
  121. function arrayIncludes(array, value) {
  122. var length = array ? array.length : 0;
  123. return !!length && baseIndexOf(array, value, 0) > -1;
  124. }
  125. /**
  126. * The base implementation of `_.findIndex` and `_.findLastIndex` without
  127. * support for iteratee shorthands.
  128. *
  129. * @private
  130. * @param {Array} array The array to inspect.
  131. * @param {Function} predicate The function invoked per iteration.
  132. * @param {number} fromIndex The index to search from.
  133. * @param {boolean} [fromRight] Specify iterating from right to left.
  134. * @returns {number} Returns the index of the matched value, else `-1`.
  135. */
  136. function baseFindIndex(array, predicate, fromIndex, fromRight) {
  137. var length = array.length,
  138. index = fromIndex + (fromRight ? 1 : -1);
  139. while ((fromRight ? index-- : ++index < length)) {
  140. if (predicate(array[index], index, array)) {
  141. return index;
  142. }
  143. }
  144. return -1;
  145. }
  146. /**
  147. * The base implementation of `_.indexOf` without `fromIndex` bounds checks.
  148. *
  149. * @private
  150. * @param {Array} array The array to inspect.
  151. * @param {*} value The value to search for.
  152. * @param {number} fromIndex The index to search from.
  153. * @returns {number} Returns the index of the matched value, else `-1`.
  154. */
  155. function baseIndexOf(array, value, fromIndex) {
  156. if (value !== value) {
  157. return baseFindIndex(array, baseIsNaN, fromIndex);
  158. }
  159. var index = fromIndex - 1,
  160. length = array.length;
  161. while (++index < length) {
  162. if (array[index] === value) {
  163. return index;
  164. }
  165. }
  166. return -1;
  167. }
  168. /**
  169. * The base implementation of `_.isNaN` without support for number objects.
  170. *
  171. * @private
  172. * @param {*} value The value to check.
  173. * @returns {boolean} Returns `true` if `value` is `NaN`, else `false`.
  174. */
  175. function baseIsNaN(value) {
  176. return value !== value;
  177. }
  178. /**
  179. * Gets the number of `placeholder` occurrences in `array`.
  180. *
  181. * @private
  182. * @param {Array} array The array to inspect.
  183. * @param {*} placeholder The placeholder to search for.
  184. * @returns {number} Returns the placeholder count.
  185. */
  186. function countHolders(array, placeholder) {
  187. var length = array.length,
  188. result = 0;
  189. while (length--) {
  190. if (array[length] === placeholder) {
  191. result++;
  192. }
  193. }
  194. return result;
  195. }
  196. /**
  197. * Gets the value at `key` of `object`.
  198. *
  199. * @private
  200. * @param {Object} [object] The object to query.
  201. * @param {string} key The key of the property to get.
  202. * @returns {*} Returns the property value.
  203. */
  204. function getValue(object, key) {
  205. return object == null ? undefined : object[key];
  206. }
  207. /**
  208. * Checks if `value` is a host object in IE < 9.
  209. *
  210. * @private
  211. * @param {*} value The value to check.
  212. * @returns {boolean} Returns `true` if `value` is a host object, else `false`.
  213. */
  214. function isHostObject(value) {
  215. // Many host objects are `Object` objects that can coerce to strings
  216. // despite having improperly defined `toString` methods.
  217. var result = false;
  218. if (value != null && typeof value.toString != 'function') {
  219. try {
  220. result = !!(value + '');
  221. } catch (e) {}
  222. }
  223. return result;
  224. }
  225. /**
  226. * Replaces all `placeholder` elements in `array` with an internal placeholder
  227. * and returns an array of their indexes.
  228. *
  229. * @private
  230. * @param {Array} array The array to modify.
  231. * @param {*} placeholder The placeholder to replace.
  232. * @returns {Array} Returns the new array of placeholder indexes.
  233. */
  234. function replaceHolders(array, placeholder) {
  235. var index = -1,
  236. length = array.length,
  237. resIndex = 0,
  238. result = [];
  239. while (++index < length) {
  240. var value = array[index];
  241. if (value === placeholder || value === PLACEHOLDER) {
  242. array[index] = PLACEHOLDER;
  243. result[resIndex++] = index;
  244. }
  245. }
  246. return result;
  247. }
  248. /** Used for built-in method references. */
  249. var funcProto = Function.prototype,
  250. objectProto = Object.prototype;
  251. /** Used to detect overreaching core-js shims. */
  252. var coreJsData = root['__core-js_shared__'];
  253. /** Used to detect methods masquerading as native. */
  254. var maskSrcKey = (function() {
  255. var uid = /[^.]+$/.exec(coreJsData && coreJsData.keys && coreJsData.keys.IE_PROTO || '');
  256. return uid ? ('Symbol(src)_1.' + uid) : '';
  257. }());
  258. /** Used to resolve the decompiled source of functions. */
  259. var funcToString = funcProto.toString;
  260. /** Used to check objects for own properties. */
  261. var hasOwnProperty = objectProto.hasOwnProperty;
  262. /**
  263. * Used to resolve the
  264. * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
  265. * of values.
  266. */
  267. var objectToString = objectProto.toString;
  268. /** Used to detect if a method is native. */
  269. var reIsNative = RegExp('^' +
  270. funcToString.call(hasOwnProperty).replace(reRegExpChar, '\\$&')
  271. .replace(/hasOwnProperty|(function).*?(?=\\\()| for .+?(?=\\\])/g, '$1.*?') + '$'
  272. );
  273. /** Built-in value references. */
  274. var objectCreate = Object.create;
  275. /* Built-in method references for those with the same name as other `lodash` methods. */
  276. var nativeMax = Math.max,
  277. nativeMin = Math.min;
  278. /* Used to set `toString` methods. */
  279. var defineProperty = (function() {
  280. var func = getNative(Object, 'defineProperty'),
  281. name = getNative.name;
  282. return (name && name.length > 2) ? func : undefined;
  283. }());
  284. /**
  285. * The base implementation of `_.create` without support for assigning
  286. * properties to the created object.
  287. *
  288. * @private
  289. * @param {Object} prototype The object to inherit from.
  290. * @returns {Object} Returns the new object.
  291. */
  292. function baseCreate(proto) {
  293. return isObject(proto) ? objectCreate(proto) : {};
  294. }
  295. /**
  296. * The base implementation of `_.isNative` without bad shim checks.
  297. *
  298. * @private
  299. * @param {*} value The value to check.
  300. * @returns {boolean} Returns `true` if `value` is a native function,
  301. * else `false`.
  302. */
  303. function baseIsNative(value) {
  304. if (!isObject(value) || isMasked(value)) {
  305. return false;
  306. }
  307. var pattern = (isFunction(value) || isHostObject(value)) ? reIsNative : reIsHostCtor;
  308. return pattern.test(toSource(value));
  309. }
  310. /**
  311. * The base implementation of `_.rest` which doesn't validate or coerce arguments.
  312. *
  313. * @private
  314. * @param {Function} func The function to apply a rest parameter to.
  315. * @param {number} [start=func.length-1] The start position of the rest parameter.
  316. * @returns {Function} Returns the new function.
  317. */
  318. function baseRest(func, start) {
  319. start = nativeMax(start === undefined ? (func.length - 1) : start, 0);
  320. return function() {
  321. var args = arguments,
  322. index = -1,
  323. length = nativeMax(args.length - start, 0),
  324. array = Array(length);
  325. while (++index < length) {
  326. array[index] = args[start + index];
  327. }
  328. index = -1;
  329. var otherArgs = Array(start + 1);
  330. while (++index < start) {
  331. otherArgs[index] = args[index];
  332. }
  333. otherArgs[start] = array;
  334. return apply(func, this, otherArgs);
  335. };
  336. }
  337. /**
  338. * Creates an array that is the composition of partially applied arguments,
  339. * placeholders, and provided arguments into a single array of arguments.
  340. *
  341. * @private
  342. * @param {Array} args The provided arguments.
  343. * @param {Array} partials The arguments to prepend to those provided.
  344. * @param {Array} holders The `partials` placeholder indexes.
  345. * @params {boolean} [isCurried] Specify composing for a curried function.
  346. * @returns {Array} Returns the new array of composed arguments.
  347. */
  348. function composeArgs(args, partials, holders, isCurried) {
  349. var argsIndex = -1,
  350. argsLength = args.length,
  351. holdersLength = holders.length,
  352. leftIndex = -1,
  353. leftLength = partials.length,
  354. rangeLength = nativeMax(argsLength - holdersLength, 0),
  355. result = Array(leftLength + rangeLength),
  356. isUncurried = !isCurried;
  357. while (++leftIndex < leftLength) {
  358. result[leftIndex] = partials[leftIndex];
  359. }
  360. while (++argsIndex < holdersLength) {
  361. if (isUncurried || argsIndex < argsLength) {
  362. result[holders[argsIndex]] = args[argsIndex];
  363. }
  364. }
  365. while (rangeLength--) {
  366. result[leftIndex++] = args[argsIndex++];
  367. }
  368. return result;
  369. }
  370. /**
  371. * This function is like `composeArgs` except that the arguments composition
  372. * is tailored for `_.partialRight`.
  373. *
  374. * @private
  375. * @param {Array} args The provided arguments.
  376. * @param {Array} partials The arguments to append to those provided.
  377. * @param {Array} holders The `partials` placeholder indexes.
  378. * @params {boolean} [isCurried] Specify composing for a curried function.
  379. * @returns {Array} Returns the new array of composed arguments.
  380. */
  381. function composeArgsRight(args, partials, holders, isCurried) {
  382. var argsIndex = -1,
  383. argsLength = args.length,
  384. holdersIndex = -1,
  385. holdersLength = holders.length,
  386. rightIndex = -1,
  387. rightLength = partials.length,
  388. rangeLength = nativeMax(argsLength - holdersLength, 0),
  389. result = Array(rangeLength + rightLength),
  390. isUncurried = !isCurried;
  391. while (++argsIndex < rangeLength) {
  392. result[argsIndex] = args[argsIndex];
  393. }
  394. var offset = argsIndex;
  395. while (++rightIndex < rightLength) {
  396. result[offset + rightIndex] = partials[rightIndex];
  397. }
  398. while (++holdersIndex < holdersLength) {
  399. if (isUncurried || argsIndex < argsLength) {
  400. result[offset + holders[holdersIndex]] = args[argsIndex++];
  401. }
  402. }
  403. return result;
  404. }
  405. /**
  406. * Copies the values of `source` to `array`.
  407. *
  408. * @private
  409. * @param {Array} source The array to copy values from.
  410. * @param {Array} [array=[]] The array to copy values to.
  411. * @returns {Array} Returns `array`.
  412. */
  413. function copyArray(source, array) {
  414. var index = -1,
  415. length = source.length;
  416. array || (array = Array(length));
  417. while (++index < length) {
  418. array[index] = source[index];
  419. }
  420. return array;
  421. }
  422. /**
  423. * Creates a function that wraps `func` to invoke it with the optional `this`
  424. * binding of `thisArg`.
  425. *
  426. * @private
  427. * @param {Function} func The function to wrap.
  428. * @param {number} bitmask The bitmask flags. See `createWrap` for more details.
  429. * @param {*} [thisArg] The `this` binding of `func`.
  430. * @returns {Function} Returns the new wrapped function.
  431. */
  432. function createBind(func, bitmask, thisArg) {
  433. var isBind = bitmask & BIND_FLAG,
  434. Ctor = createCtor(func);
  435. function wrapper() {
  436. var fn = (this && this !== root && this instanceof wrapper) ? Ctor : func;
  437. return fn.apply(isBind ? thisArg : this, arguments);
  438. }
  439. return wrapper;
  440. }
  441. /**
  442. * Creates a function that produces an instance of `Ctor` regardless of
  443. * whether it was invoked as part of a `new` expression or by `call` or `apply`.
  444. *
  445. * @private
  446. * @param {Function} Ctor The constructor to wrap.
  447. * @returns {Function} Returns the new wrapped function.
  448. */
  449. function createCtor(Ctor) {
  450. return function() {
  451. // Use a `switch` statement to work with class constructors. See
  452. // http://ecma-international.org/ecma-262/7.0/#sec-ecmascript-function-objects-call-thisargument-argumentslist
  453. // for more details.
  454. var args = arguments;
  455. switch (args.length) {
  456. case 0: return new Ctor;
  457. case 1: return new Ctor(args[0]);
  458. case 2: return new Ctor(args[0], args[1]);
  459. case 3: return new Ctor(args[0], args[1], args[2]);
  460. case 4: return new Ctor(args[0], args[1], args[2], args[3]);
  461. case 5: return new Ctor(args[0], args[1], args[2], args[3], args[4]);
  462. case 6: return new Ctor(args[0], args[1], args[2], args[3], args[4], args[5]);
  463. case 7: return new Ctor(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
  464. }
  465. var thisBinding = baseCreate(Ctor.prototype),
  466. result = Ctor.apply(thisBinding, args);
  467. // Mimic the constructor's `return` behavior.
  468. // See https://es5.github.io/#x13.2.2 for more details.
  469. return isObject(result) ? result : thisBinding;
  470. };
  471. }
  472. /**
  473. * Creates a function that wraps `func` to enable currying.
  474. *
  475. * @private
  476. * @param {Function} func The function to wrap.
  477. * @param {number} bitmask The bitmask flags. See `createWrap` for more details.
  478. * @param {number} arity The arity of `func`.
  479. * @returns {Function} Returns the new wrapped function.
  480. */
  481. function createCurry(func, bitmask, arity) {
  482. var Ctor = createCtor(func);
  483. function wrapper() {
  484. var length = arguments.length,
  485. args = Array(length),
  486. index = length,
  487. placeholder = getHolder(wrapper);
  488. while (index--) {
  489. args[index] = arguments[index];
  490. }
  491. var holders = (length < 3 && args[0] !== placeholder && args[length - 1] !== placeholder)
  492. ? []
  493. : replaceHolders(args, placeholder);
  494. length -= holders.length;
  495. if (length < arity) {
  496. return createRecurry(
  497. func, bitmask, createHybrid, wrapper.placeholder, undefined,
  498. args, holders, undefined, undefined, arity - length);
  499. }
  500. var fn = (this && this !== root && this instanceof wrapper) ? Ctor : func;
  501. return apply(fn, this, args);
  502. }
  503. return wrapper;
  504. }
  505. /**
  506. * Creates a function that wraps `func` to invoke it with optional `this`
  507. * binding of `thisArg`, partial application, and currying.
  508. *
  509. * @private
  510. * @param {Function|string} func The function or method name to wrap.
  511. * @param {number} bitmask The bitmask flags. See `createWrap` for more details.
  512. * @param {*} [thisArg] The `this` binding of `func`.
  513. * @param {Array} [partials] The arguments to prepend to those provided to
  514. * the new function.
  515. * @param {Array} [holders] The `partials` placeholder indexes.
  516. * @param {Array} [partialsRight] The arguments to append to those provided
  517. * to the new function.
  518. * @param {Array} [holdersRight] The `partialsRight` placeholder indexes.
  519. * @param {Array} [argPos] The argument positions of the new function.
  520. * @param {number} [ary] The arity cap of `func`.
  521. * @param {number} [arity] The arity of `func`.
  522. * @returns {Function} Returns the new wrapped function.
  523. */
  524. function createHybrid(func, bitmask, thisArg, partials, holders, partialsRight, holdersRight, argPos, ary, arity) {
  525. var isAry = bitmask & ARY_FLAG,
  526. isBind = bitmask & BIND_FLAG,
  527. isBindKey = bitmask & BIND_KEY_FLAG,
  528. isCurried = bitmask & (CURRY_FLAG | CURRY_RIGHT_FLAG),
  529. isFlip = bitmask & FLIP_FLAG,
  530. Ctor = isBindKey ? undefined : createCtor(func);
  531. function wrapper() {
  532. var length = arguments.length,
  533. args = Array(length),
  534. index = length;
  535. while (index--) {
  536. args[index] = arguments[index];
  537. }
  538. if (isCurried) {
  539. var placeholder = getHolder(wrapper),
  540. holdersCount = countHolders(args, placeholder);
  541. }
  542. if (partials) {
  543. args = composeArgs(args, partials, holders, isCurried);
  544. }
  545. if (partialsRight) {
  546. args = composeArgsRight(args, partialsRight, holdersRight, isCurried);
  547. }
  548. length -= holdersCount;
  549. if (isCurried && length < arity) {
  550. var newHolders = replaceHolders(args, placeholder);
  551. return createRecurry(
  552. func, bitmask, createHybrid, wrapper.placeholder, thisArg,
  553. args, newHolders, argPos, ary, arity - length
  554. );
  555. }
  556. var thisBinding = isBind ? thisArg : this,
  557. fn = isBindKey ? thisBinding[func] : func;
  558. length = args.length;
  559. if (argPos) {
  560. args = reorder(args, argPos);
  561. } else if (isFlip && length > 1) {
  562. args.reverse();
  563. }
  564. if (isAry && ary < length) {
  565. args.length = ary;
  566. }
  567. if (this && this !== root && this instanceof wrapper) {
  568. fn = Ctor || createCtor(fn);
  569. }
  570. return fn.apply(thisBinding, args);
  571. }
  572. return wrapper;
  573. }
  574. /**
  575. * Creates a function that wraps `func` to invoke it with the `this` binding
  576. * of `thisArg` and `partials` prepended to the arguments it receives.
  577. *
  578. * @private
  579. * @param {Function} func The function to wrap.
  580. * @param {number} bitmask The bitmask flags. See `createWrap` for more details.
  581. * @param {*} thisArg The `this` binding of `func`.
  582. * @param {Array} partials The arguments to prepend to those provided to
  583. * the new function.
  584. * @returns {Function} Returns the new wrapped function.
  585. */
  586. function createPartial(func, bitmask, thisArg, partials) {
  587. var isBind = bitmask & BIND_FLAG,
  588. Ctor = createCtor(func);
  589. function wrapper() {
  590. var argsIndex = -1,
  591. argsLength = arguments.length,
  592. leftIndex = -1,
  593. leftLength = partials.length,
  594. args = Array(leftLength + argsLength),
  595. fn = (this && this !== root && this instanceof wrapper) ? Ctor : func;
  596. while (++leftIndex < leftLength) {
  597. args[leftIndex] = partials[leftIndex];
  598. }
  599. while (argsLength--) {
  600. args[leftIndex++] = arguments[++argsIndex];
  601. }
  602. return apply(fn, isBind ? thisArg : this, args);
  603. }
  604. return wrapper;
  605. }
  606. /**
  607. * Creates a function that wraps `func` to continue currying.
  608. *
  609. * @private
  610. * @param {Function} func The function to wrap.
  611. * @param {number} bitmask The bitmask flags. See `createWrap` for more details.
  612. * @param {Function} wrapFunc The function to create the `func` wrapper.
  613. * @param {*} placeholder The placeholder value.
  614. * @param {*} [thisArg] The `this` binding of `func`.
  615. * @param {Array} [partials] The arguments to prepend to those provided to
  616. * the new function.
  617. * @param {Array} [holders] The `partials` placeholder indexes.
  618. * @param {Array} [argPos] The argument positions of the new function.
  619. * @param {number} [ary] The arity cap of `func`.
  620. * @param {number} [arity] The arity of `func`.
  621. * @returns {Function} Returns the new wrapped function.
  622. */
  623. function createRecurry(func, bitmask, wrapFunc, placeholder, thisArg, partials, holders, argPos, ary, arity) {
  624. var isCurry = bitmask & CURRY_FLAG,
  625. newHolders = isCurry ? holders : undefined,
  626. newHoldersRight = isCurry ? undefined : holders,
  627. newPartials = isCurry ? partials : undefined,
  628. newPartialsRight = isCurry ? undefined : partials;
  629. bitmask |= (isCurry ? PARTIAL_FLAG : PARTIAL_RIGHT_FLAG);
  630. bitmask &= ~(isCurry ? PARTIAL_RIGHT_FLAG : PARTIAL_FLAG);
  631. if (!(bitmask & CURRY_BOUND_FLAG)) {
  632. bitmask &= ~(BIND_FLAG | BIND_KEY_FLAG);
  633. }
  634. var result = wrapFunc(func, bitmask, thisArg, newPartials, newHolders, newPartialsRight, newHoldersRight, argPos, ary, arity);
  635. result.placeholder = placeholder;
  636. return setWrapToString(result, func, bitmask);
  637. }
  638. /**
  639. * Creates a function that either curries or invokes `func` with optional
  640. * `this` binding and partially applied arguments.
  641. *
  642. * @private
  643. * @param {Function|string} func The function or method name to wrap.
  644. * @param {number} bitmask The bitmask flags.
  645. * The bitmask may be composed of the following flags:
  646. * 1 - `_.bind`
  647. * 2 - `_.bindKey`
  648. * 4 - `_.curry` or `_.curryRight` of a bound function
  649. * 8 - `_.curry`
  650. * 16 - `_.curryRight`
  651. * 32 - `_.partial`
  652. * 64 - `_.partialRight`
  653. * 128 - `_.rearg`
  654. * 256 - `_.ary`
  655. * 512 - `_.flip`
  656. * @param {*} [thisArg] The `this` binding of `func`.
  657. * @param {Array} [partials] The arguments to be partially applied.
  658. * @param {Array} [holders] The `partials` placeholder indexes.
  659. * @param {Array} [argPos] The argument positions of the new function.
  660. * @param {number} [ary] The arity cap of `func`.
  661. * @param {number} [arity] The arity of `func`.
  662. * @returns {Function} Returns the new wrapped function.
  663. */
  664. function createWrap(func, bitmask, thisArg, partials, holders, argPos, ary, arity) {
  665. var isBindKey = bitmask & BIND_KEY_FLAG;
  666. if (!isBindKey && typeof func != 'function') {
  667. throw new TypeError(FUNC_ERROR_TEXT);
  668. }
  669. var length = partials ? partials.length : 0;
  670. if (!length) {
  671. bitmask &= ~(PARTIAL_FLAG | PARTIAL_RIGHT_FLAG);
  672. partials = holders = undefined;
  673. }
  674. ary = ary === undefined ? ary : nativeMax(toInteger(ary), 0);
  675. arity = arity === undefined ? arity : toInteger(arity);
  676. length -= holders ? holders.length : 0;
  677. if (bitmask & PARTIAL_RIGHT_FLAG) {
  678. var partialsRight = partials,
  679. holdersRight = holders;
  680. partials = holders = undefined;
  681. }
  682. var newData = [
  683. func, bitmask, thisArg, partials, holders, partialsRight, holdersRight,
  684. argPos, ary, arity
  685. ];
  686. func = newData[0];
  687. bitmask = newData[1];
  688. thisArg = newData[2];
  689. partials = newData[3];
  690. holders = newData[4];
  691. arity = newData[9] = newData[9] == null
  692. ? (isBindKey ? 0 : func.length)
  693. : nativeMax(newData[9] - length, 0);
  694. if (!arity && bitmask & (CURRY_FLAG | CURRY_RIGHT_FLAG)) {
  695. bitmask &= ~(CURRY_FLAG | CURRY_RIGHT_FLAG);
  696. }
  697. if (!bitmask || bitmask == BIND_FLAG) {
  698. var result = createBind(func, bitmask, thisArg);
  699. } else if (bitmask == CURRY_FLAG || bitmask == CURRY_RIGHT_FLAG) {
  700. result = createCurry(func, bitmask, arity);
  701. } else if ((bitmask == PARTIAL_FLAG || bitmask == (BIND_FLAG | PARTIAL_FLAG)) && !holders.length) {
  702. result = createPartial(func, bitmask, thisArg, partials);
  703. } else {
  704. result = createHybrid.apply(undefined, newData);
  705. }
  706. return setWrapToString(result, func, bitmask);
  707. }
  708. /**
  709. * Gets the argument placeholder value for `func`.
  710. *
  711. * @private
  712. * @param {Function} func The function to inspect.
  713. * @returns {*} Returns the placeholder value.
  714. */
  715. function getHolder(func) {
  716. var object = func;
  717. return object.placeholder;
  718. }
  719. /**
  720. * Gets the native function at `key` of `object`.
  721. *
  722. * @private
  723. * @param {Object} object The object to query.
  724. * @param {string} key The key of the method to get.
  725. * @returns {*} Returns the function if it's native, else `undefined`.
  726. */
  727. function getNative(object, key) {
  728. var value = getValue(object, key);
  729. return baseIsNative(value) ? value : undefined;
  730. }
  731. /**
  732. * Extracts wrapper details from the `source` body comment.
  733. *
  734. * @private
  735. * @param {string} source The source to inspect.
  736. * @returns {Array} Returns the wrapper details.
  737. */
  738. function getWrapDetails(source) {
  739. var match = source.match(reWrapDetails);
  740. return match ? match[1].split(reSplitDetails) : [];
  741. }
  742. /**
  743. * Inserts wrapper `details` in a comment at the top of the `source` body.
  744. *
  745. * @private
  746. * @param {string} source The source to modify.
  747. * @returns {Array} details The details to insert.
  748. * @returns {string} Returns the modified source.
  749. */
  750. function insertWrapDetails(source, details) {
  751. var length = details.length,
  752. lastIndex = length - 1;
  753. details[lastIndex] = (length > 1 ? '& ' : '') + details[lastIndex];
  754. details = details.join(length > 2 ? ', ' : ' ');
  755. return source.replace(reWrapComment, '{\n/* [wrapped with ' + details + '] */\n');
  756. }
  757. /**
  758. * Checks if `value` is a valid array-like index.
  759. *
  760. * @private
  761. * @param {*} value The value to check.
  762. * @param {number} [length=MAX_SAFE_INTEGER] The upper bounds of a valid index.
  763. * @returns {boolean} Returns `true` if `value` is a valid index, else `false`.
  764. */
  765. function isIndex(value, length) {
  766. length = length == null ? MAX_SAFE_INTEGER : length;
  767. return !!length &&
  768. (typeof value == 'number' || reIsUint.test(value)) &&
  769. (value > -1 && value % 1 == 0 && value < length);
  770. }
  771. /**
  772. * Checks if `func` has its source masked.
  773. *
  774. * @private
  775. * @param {Function} func The function to check.
  776. * @returns {boolean} Returns `true` if `func` is masked, else `false`.
  777. */
  778. function isMasked(func) {
  779. return !!maskSrcKey && (maskSrcKey in func);
  780. }
  781. /**
  782. * Reorder `array` according to the specified indexes where the element at
  783. * the first index is assigned as the first element, the element at
  784. * the second index is assigned as the second element, and so on.
  785. *
  786. * @private
  787. * @param {Array} array The array to reorder.
  788. * @param {Array} indexes The arranged array indexes.
  789. * @returns {Array} Returns `array`.
  790. */
  791. function reorder(array, indexes) {
  792. var arrLength = array.length,
  793. length = nativeMin(indexes.length, arrLength),
  794. oldArray = copyArray(array);
  795. while (length--) {
  796. var index = indexes[length];
  797. array[length] = isIndex(index, arrLength) ? oldArray[index] : undefined;
  798. }
  799. return array;
  800. }
  801. /**
  802. * Sets the `toString` method of `wrapper` to mimic the source of `reference`
  803. * with wrapper details in a comment at the top of the source body.
  804. *
  805. * @private
  806. * @param {Function} wrapper The function to modify.
  807. * @param {Function} reference The reference function.
  808. * @param {number} bitmask The bitmask flags. See `createWrap` for more details.
  809. * @returns {Function} Returns `wrapper`.
  810. */
  811. var setWrapToString = !defineProperty ? identity : function(wrapper, reference, bitmask) {
  812. var source = (reference + '');
  813. return defineProperty(wrapper, 'toString', {
  814. 'configurable': true,
  815. 'enumerable': false,
  816. 'value': constant(insertWrapDetails(source, updateWrapDetails(getWrapDetails(source), bitmask)))
  817. });
  818. };
  819. /**
  820. * Converts `func` to its source code.
  821. *
  822. * @private
  823. * @param {Function} func The function to process.
  824. * @returns {string} Returns the source code.
  825. */
  826. function toSource(func) {
  827. if (func != null) {
  828. try {
  829. return funcToString.call(func);
  830. } catch (e) {}
  831. try {
  832. return (func + '');
  833. } catch (e) {}
  834. }
  835. return '';
  836. }
  837. /**
  838. * Updates wrapper `details` based on `bitmask` flags.
  839. *
  840. * @private
  841. * @returns {Array} details The details to modify.
  842. * @param {number} bitmask The bitmask flags. See `createWrap` for more details.
  843. * @returns {Array} Returns `details`.
  844. */
  845. function updateWrapDetails(details, bitmask) {
  846. arrayEach(wrapFlags, function(pair) {
  847. var value = '_.' + pair[0];
  848. if ((bitmask & pair[1]) && !arrayIncludes(details, value)) {
  849. details.push(value);
  850. }
  851. });
  852. return details.sort();
  853. }
  854. /**
  855. * Creates a function that invokes `func` with the `this` binding of `thisArg`
  856. * and `partials` prepended to the arguments it receives.
  857. *
  858. * The `_.bind.placeholder` value, which defaults to `_` in monolithic builds,
  859. * may be used as a placeholder for partially applied arguments.
  860. *
  861. * **Note:** Unlike native `Function#bind`, this method doesn't set the "length"
  862. * property of bound functions.
  863. *
  864. * @static
  865. * @memberOf _
  866. * @since 0.1.0
  867. * @category Function
  868. * @param {Function} func The function to bind.
  869. * @param {*} thisArg The `this` binding of `func`.
  870. * @param {...*} [partials] The arguments to be partially applied.
  871. * @returns {Function} Returns the new bound function.
  872. * @example
  873. *
  874. * function greet(greeting, punctuation) {
  875. * return greeting + ' ' + this.user + punctuation;
  876. * }
  877. *
  878. * var object = { 'user': 'fred' };
  879. *
  880. * var bound = _.bind(greet, object, 'hi');
  881. * bound('!');
  882. * // => 'hi fred!'
  883. *
  884. * // Bound with placeholders.
  885. * var bound = _.bind(greet, object, _, '!');
  886. * bound('hi');
  887. * // => 'hi fred!'
  888. */
  889. var bind = baseRest(function(func, thisArg, partials) {
  890. var bitmask = BIND_FLAG;
  891. if (partials.length) {
  892. var holders = replaceHolders(partials, getHolder(bind));
  893. bitmask |= PARTIAL_FLAG;
  894. }
  895. return createWrap(func, bitmask, thisArg, partials, holders);
  896. });
  897. /**
  898. * Checks if `value` is classified as a `Function` object.
  899. *
  900. * @static
  901. * @memberOf _
  902. * @since 0.1.0
  903. * @category Lang
  904. * @param {*} value The value to check.
  905. * @returns {boolean} Returns `true` if `value` is a function, else `false`.
  906. * @example
  907. *
  908. * _.isFunction(_);
  909. * // => true
  910. *
  911. * _.isFunction(/abc/);
  912. * // => false
  913. */
  914. function isFunction(value) {
  915. // The use of `Object#toString` avoids issues with the `typeof` operator
  916. // in Safari 8-9 which returns 'object' for typed array and other constructors.
  917. var tag = isObject(value) ? objectToString.call(value) : '';
  918. return tag == funcTag || tag == genTag;
  919. }
  920. /**
  921. * Checks if `value` is the
  922. * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
  923. * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  924. *
  925. * @static
  926. * @memberOf _
  927. * @since 0.1.0
  928. * @category Lang
  929. * @param {*} value The value to check.
  930. * @returns {boolean} Returns `true` if `value` is an object, else `false`.
  931. * @example
  932. *
  933. * _.isObject({});
  934. * // => true
  935. *
  936. * _.isObject([1, 2, 3]);
  937. * // => true
  938. *
  939. * _.isObject(_.noop);
  940. * // => true
  941. *
  942. * _.isObject(null);
  943. * // => false
  944. */
  945. function isObject(value) {
  946. var type = typeof value;
  947. return !!value && (type == 'object' || type == 'function');
  948. }
  949. /**
  950. * Checks if `value` is object-like. A value is object-like if it's not `null`
  951. * and has a `typeof` result of "object".
  952. *
  953. * @static
  954. * @memberOf _
  955. * @since 4.0.0
  956. * @category Lang
  957. * @param {*} value The value to check.
  958. * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
  959. * @example
  960. *
  961. * _.isObjectLike({});
  962. * // => true
  963. *
  964. * _.isObjectLike([1, 2, 3]);
  965. * // => true
  966. *
  967. * _.isObjectLike(_.noop);
  968. * // => false
  969. *
  970. * _.isObjectLike(null);
  971. * // => false
  972. */
  973. function isObjectLike(value) {
  974. return !!value && typeof value == 'object';
  975. }
  976. /**
  977. * Checks if `value` is classified as a `Symbol` primitive or object.
  978. *
  979. * @static
  980. * @memberOf _
  981. * @since 4.0.0
  982. * @category Lang
  983. * @param {*} value The value to check.
  984. * @returns {boolean} Returns `true` if `value` is a symbol, else `false`.
  985. * @example
  986. *
  987. * _.isSymbol(Symbol.iterator);
  988. * // => true
  989. *
  990. * _.isSymbol('abc');
  991. * // => false
  992. */
  993. function isSymbol(value) {
  994. return typeof value == 'symbol' ||
  995. (isObjectLike(value) && objectToString.call(value) == symbolTag);
  996. }
  997. /**
  998. * Converts `value` to a finite number.
  999. *
  1000. * @static
  1001. * @memberOf _
  1002. * @since 4.12.0
  1003. * @category Lang
  1004. * @param {*} value The value to convert.
  1005. * @returns {number} Returns the converted number.
  1006. * @example
  1007. *
  1008. * _.toFinite(3.2);
  1009. * // => 3.2
  1010. *
  1011. * _.toFinite(Number.MIN_VALUE);
  1012. * // => 5e-324
  1013. *
  1014. * _.toFinite(Infinity);
  1015. * // => 1.7976931348623157e+308
  1016. *
  1017. * _.toFinite('3.2');
  1018. * // => 3.2
  1019. */
  1020. function toFinite(value) {
  1021. if (!value) {
  1022. return value === 0 ? value : 0;
  1023. }
  1024. value = toNumber(value);
  1025. if (value === INFINITY || value === -INFINITY) {
  1026. var sign = (value < 0 ? -1 : 1);
  1027. return sign * MAX_INTEGER;
  1028. }
  1029. return value === value ? value : 0;
  1030. }
  1031. /**
  1032. * Converts `value` to an integer.
  1033. *
  1034. * **Note:** This method is loosely based on
  1035. * [`ToInteger`](http://www.ecma-international.org/ecma-262/7.0/#sec-tointeger).
  1036. *
  1037. * @static
  1038. * @memberOf _
  1039. * @since 4.0.0
  1040. * @category Lang
  1041. * @param {*} value The value to convert.
  1042. * @returns {number} Returns the converted integer.
  1043. * @example
  1044. *
  1045. * _.toInteger(3.2);
  1046. * // => 3
  1047. *
  1048. * _.toInteger(Number.MIN_VALUE);
  1049. * // => 0
  1050. *
  1051. * _.toInteger(Infinity);
  1052. * // => 1.7976931348623157e+308
  1053. *
  1054. * _.toInteger('3.2');
  1055. * // => 3
  1056. */
  1057. function toInteger(value) {
  1058. var result = toFinite(value),
  1059. remainder = result % 1;
  1060. return result === result ? (remainder ? result - remainder : result) : 0;
  1061. }
  1062. /**
  1063. * Converts `value` to a number.
  1064. *
  1065. * @static
  1066. * @memberOf _
  1067. * @since 4.0.0
  1068. * @category Lang
  1069. * @param {*} value The value to process.
  1070. * @returns {number} Returns the number.
  1071. * @example
  1072. *
  1073. * _.toNumber(3.2);
  1074. * // => 3.2
  1075. *
  1076. * _.toNumber(Number.MIN_VALUE);
  1077. * // => 5e-324
  1078. *
  1079. * _.toNumber(Infinity);
  1080. * // => Infinity
  1081. *
  1082. * _.toNumber('3.2');
  1083. * // => 3.2
  1084. */
  1085. function toNumber(value) {
  1086. if (typeof value == 'number') {
  1087. return value;
  1088. }
  1089. if (isSymbol(value)) {
  1090. return NAN;
  1091. }
  1092. if (isObject(value)) {
  1093. var other = typeof value.valueOf == 'function' ? value.valueOf() : value;
  1094. value = isObject(other) ? (other + '') : other;
  1095. }
  1096. if (typeof value != 'string') {
  1097. return value === 0 ? value : +value;
  1098. }
  1099. value = value.replace(reTrim, '');
  1100. var isBinary = reIsBinary.test(value);
  1101. return (isBinary || reIsOctal.test(value))
  1102. ? freeParseInt(value.slice(2), isBinary ? 2 : 8)
  1103. : (reIsBadHex.test(value) ? NAN : +value);
  1104. }
  1105. /**
  1106. * Creates a function that returns `value`.
  1107. *
  1108. * @static
  1109. * @memberOf _
  1110. * @since 2.4.0
  1111. * @category Util
  1112. * @param {*} value The value to return from the new function.
  1113. * @returns {Function} Returns the new constant function.
  1114. * @example
  1115. *
  1116. * var objects = _.times(2, _.constant({ 'a': 1 }));
  1117. *
  1118. * console.log(objects);
  1119. * // => [{ 'a': 1 }, { 'a': 1 }]
  1120. *
  1121. * console.log(objects[0] === objects[1]);
  1122. * // => true
  1123. */
  1124. function constant(value) {
  1125. return function() {
  1126. return value;
  1127. };
  1128. }
  1129. /**
  1130. * This method returns the first argument it receives.
  1131. *
  1132. * @static
  1133. * @since 0.1.0
  1134. * @memberOf _
  1135. * @category Util
  1136. * @param {*} value Any value.
  1137. * @returns {*} Returns `value`.
  1138. * @example
  1139. *
  1140. * var object = { 'a': 1 };
  1141. *
  1142. * console.log(_.identity(object) === object);
  1143. * // => true
  1144. */
  1145. function identity(value) {
  1146. return value;
  1147. }
  1148. // Assign default placeholders.
  1149. bind.placeholder = {};
  1150. module.exports = bind;