lodash.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. var LazyWrapper = require('../internal/LazyWrapper'),
  2. LodashWrapper = require('../internal/LodashWrapper'),
  3. baseLodash = require('../internal/baseLodash'),
  4. isArray = require('../lang/isArray'),
  5. isObjectLike = require('../internal/isObjectLike'),
  6. wrapperClone = require('../internal/wrapperClone');
  7. /** Used for native method references. */
  8. var objectProto = Object.prototype;
  9. /** Used to check objects for own properties. */
  10. var hasOwnProperty = objectProto.hasOwnProperty;
  11. /**
  12. * Creates a `lodash` object which wraps `value` to enable implicit chaining.
  13. * Methods that operate on and return arrays, collections, and functions can
  14. * be chained together. Methods that retrieve a single value or may return a
  15. * primitive value will automatically end the chain returning the unwrapped
  16. * value. Explicit chaining may be enabled using `_.chain`. The execution of
  17. * chained methods is lazy, that is, execution is deferred until `_#value`
  18. * is implicitly or explicitly called.
  19. *
  20. * Lazy evaluation allows several methods to support shortcut fusion. Shortcut
  21. * fusion is an optimization strategy which merge iteratee calls; this can help
  22. * to avoid the creation of intermediate data structures and greatly reduce the
  23. * number of iteratee executions.
  24. *
  25. * Chaining is supported in custom builds as long as the `_#value` method is
  26. * directly or indirectly included in the build.
  27. *
  28. * In addition to lodash methods, wrappers have `Array` and `String` methods.
  29. *
  30. * The wrapper `Array` methods are:
  31. * `concat`, `join`, `pop`, `push`, `reverse`, `shift`, `slice`, `sort`,
  32. * `splice`, and `unshift`
  33. *
  34. * The wrapper `String` methods are:
  35. * `replace` and `split`
  36. *
  37. * The wrapper methods that support shortcut fusion are:
  38. * `compact`, `drop`, `dropRight`, `dropRightWhile`, `dropWhile`, `filter`,
  39. * `first`, `initial`, `last`, `map`, `pluck`, `reject`, `rest`, `reverse`,
  40. * `slice`, `take`, `takeRight`, `takeRightWhile`, `takeWhile`, `toArray`,
  41. * and `where`
  42. *
  43. * The chainable wrapper methods are:
  44. * `after`, `ary`, `assign`, `at`, `before`, `bind`, `bindAll`, `bindKey`,
  45. * `callback`, `chain`, `chunk`, `commit`, `compact`, `concat`, `constant`,
  46. * `countBy`, `create`, `curry`, `debounce`, `defaults`, `defaultsDeep`,
  47. * `defer`, `delay`, `difference`, `drop`, `dropRight`, `dropRightWhile`,
  48. * `dropWhile`, `fill`, `filter`, `flatten`, `flattenDeep`, `flow`, `flowRight`,
  49. * `forEach`, `forEachRight`, `forIn`, `forInRight`, `forOwn`, `forOwnRight`,
  50. * `functions`, `groupBy`, `indexBy`, `initial`, `intersection`, `invert`,
  51. * `invoke`, `keys`, `keysIn`, `map`, `mapKeys`, `mapValues`, `matches`,
  52. * `matchesProperty`, `memoize`, `merge`, `method`, `methodOf`, `mixin`,
  53. * `modArgs`, `negate`, `omit`, `once`, `pairs`, `partial`, `partialRight`,
  54. * `partition`, `pick`, `plant`, `pluck`, `property`, `propertyOf`, `pull`,
  55. * `pullAt`, `push`, `range`, `rearg`, `reject`, `remove`, `rest`, `restParam`,
  56. * `reverse`, `set`, `shuffle`, `slice`, `sort`, `sortBy`, `sortByAll`,
  57. * `sortByOrder`, `splice`, `spread`, `take`, `takeRight`, `takeRightWhile`,
  58. * `takeWhile`, `tap`, `throttle`, `thru`, `times`, `toArray`, `toPlainObject`,
  59. * `transform`, `union`, `uniq`, `unshift`, `unzip`, `unzipWith`, `values`,
  60. * `valuesIn`, `where`, `without`, `wrap`, `xor`, `zip`, `zipObject`, `zipWith`
  61. *
  62. * The wrapper methods that are **not** chainable by default are:
  63. * `add`, `attempt`, `camelCase`, `capitalize`, `ceil`, `clone`, `cloneDeep`,
  64. * `deburr`, `endsWith`, `escape`, `escapeRegExp`, `every`, `find`, `findIndex`,
  65. * `findKey`, `findLast`, `findLastIndex`, `findLastKey`, `findWhere`, `first`,
  66. * `floor`, `get`, `gt`, `gte`, `has`, `identity`, `includes`, `indexOf`,
  67. * `inRange`, `isArguments`, `isArray`, `isBoolean`, `isDate`, `isElement`,
  68. * `isEmpty`, `isEqual`, `isError`, `isFinite` `isFunction`, `isMatch`,
  69. * `isNative`, `isNaN`, `isNull`, `isNumber`, `isObject`, `isPlainObject`,
  70. * `isRegExp`, `isString`, `isUndefined`, `isTypedArray`, `join`, `kebabCase`,
  71. * `last`, `lastIndexOf`, `lt`, `lte`, `max`, `min`, `noConflict`, `noop`,
  72. * `now`, `pad`, `padLeft`, `padRight`, `parseInt`, `pop`, `random`, `reduce`,
  73. * `reduceRight`, `repeat`, `result`, `round`, `runInContext`, `shift`, `size`,
  74. * `snakeCase`, `some`, `sortedIndex`, `sortedLastIndex`, `startCase`,
  75. * `startsWith`, `sum`, `template`, `trim`, `trimLeft`, `trimRight`, `trunc`,
  76. * `unescape`, `uniqueId`, `value`, and `words`
  77. *
  78. * The wrapper method `sample` will return a wrapped value when `n` is provided,
  79. * otherwise an unwrapped value is returned.
  80. *
  81. * @name _
  82. * @constructor
  83. * @category Chain
  84. * @param {*} value The value to wrap in a `lodash` instance.
  85. * @returns {Object} Returns the new `lodash` wrapper instance.
  86. * @example
  87. *
  88. * var wrapped = _([1, 2, 3]);
  89. *
  90. * // returns an unwrapped value
  91. * wrapped.reduce(function(total, n) {
  92. * return total + n;
  93. * });
  94. * // => 6
  95. *
  96. * // returns a wrapped value
  97. * var squares = wrapped.map(function(n) {
  98. * return n * n;
  99. * });
  100. *
  101. * _.isArray(squares);
  102. * // => false
  103. *
  104. * _.isArray(squares.value());
  105. * // => true
  106. */
  107. function lodash(value) {
  108. if (isObjectLike(value) && !isArray(value) && !(value instanceof LazyWrapper)) {
  109. if (value instanceof LodashWrapper) {
  110. return value;
  111. }
  112. if (hasOwnProperty.call(value, '__chain__') && hasOwnProperty.call(value, '__wrapped__')) {
  113. return wrapperClone(value);
  114. }
  115. }
  116. return new LodashWrapper(value);
  117. }
  118. // Ensure wrappers are instances of `baseLodash`.
  119. lodash.prototype = baseLodash.prototype;
  120. module.exports = lodash;