1 var LazyWrapper = require('./_LazyWrapper'),
2 LodashWrapper = require('./_LodashWrapper'),
3 baseLodash = require('./_baseLodash'),
4 isArray = require('./isArray'),
5 isObjectLike = require('./isObjectLike'),
6 wrapperClone = require('./_wrapperClone');
8 /** Used for built-in method references. */
9 var objectProto = Object.prototype;
11 /** Used to check objects for own properties. */
12 var hasOwnProperty = objectProto.hasOwnProperty;
15 * Creates a `lodash` object which wraps `value` to enable implicit method
16 * chaining. Methods that operate on and return arrays, collections, and
17 * functions can be chained together. Methods that retrieve a single value or
18 * may return a primitive value will automatically end the chain sequence and
19 * return the unwrapped value. Otherwise, the value must be unwrapped with
22 * Explicit chaining, which must be unwrapped with `_#value` in all cases,
23 * may be enabled using `_.chain`.
25 * The execution of chained methods is lazy, that is, it's deferred until
26 * `_#value` is implicitly or explicitly called.
28 * Lazy evaluation allows several methods to support shortcut fusion. Shortcut
29 * fusion is an optimization to merge iteratee calls; this avoids the creation
30 * of intermediate arrays and can greatly reduce the number of iteratee executions.
31 * Sections of a chain sequence qualify for shortcut fusion if the section is
32 * applied to an array of at least two hundred elements and any iteratees
33 * accept only one argument. The heuristic for whether a section qualifies
34 * for shortcut fusion is subject to change.
36 * Chaining is supported in custom builds as long as the `_#value` method is
37 * directly or indirectly included in the build.
39 * In addition to lodash methods, wrappers have `Array` and `String` methods.
41 * The wrapper `Array` methods are:
42 * `concat`, `join`, `pop`, `push`, `shift`, `sort`, `splice`, and `unshift`
44 * The wrapper `String` methods are:
45 * `replace` and `split`
47 * The wrapper methods that support shortcut fusion are:
48 * `at`, `compact`, `drop`, `dropRight`, `dropWhile`, `filter`, `find`,
49 * `findLast`, `head`, `initial`, `last`, `map`, `reject`, `reverse`, `slice`,
50 * `tail`, `take`, `takeRight`, `takeRightWhile`, `takeWhile`, and `toArray`
52 * The chainable wrapper methods are:
53 * `after`, `ary`, `assign`, `assignIn`, `assignInWith`, `assignWith`,
54 * `at`, `before`, `bind`, `bindAll`, `bindKey`, `chain`, `chunk`, `commit`,
55 * `compact`, `concat`, `conforms`, `constant`, `countBy`, `create`, `curry`,
56 * `debounce`, `defaults`, `defaultsDeep`, `defer`, `delay`, `difference`,
57 * `differenceBy`, `differenceWith`, `drop`, `dropRight`, `dropRightWhile`,
58 * `dropWhile`, `fill`, `filter`, `flatten`, `flattenDeep`, `flip`, `flow`,
59 * `flowRight`, `fromPairs`, `functions`, `functionsIn`, `groupBy`, `initial`,
60 * `intersection`, `intersectionBy`, `intersectionWith`, `invert`, `invertBy`,
61 * `invokeMap`, `iteratee`, `keyBy`, `keys`, `keysIn`, `map`, `mapKeys`,
62 * `mapValues`, `matches`, `matchesProperty`, `memoize`, `merge`, `mergeWith`,
63 * `method`, `methodOf`, `mixin`, `negate`, `nthArg`, `omit`, `omitBy`, `once`,
64 * `orderBy`, `over`, `overArgs`, `overEvery`, `overSome`, `partial`,
65 * `partialRight`, `partition`, `pick`, `pickBy`, `plant`, `property`,
66 * `propertyOf`, `pull`, `pullAll`, `pullAllBy`, `pullAt`, `push`, `range`,
67 * `rangeRight`, `rearg`, `reject`, `remove`, `rest`, `reverse`, `sampleSize`,
68 * `set`, `setWith`, `shuffle`, `slice`, `sort`, `sortBy`, `splice`, `spread`,
69 * `tail`, `take`, `takeRight`, `takeRightWhile`, `takeWhile`, `tap`, `throttle`,
70 * `thru`, `toArray`, `toPairs`, `toPairsIn`, `toPath`, `toPlainObject`,
71 * `transform`, `unary`, `union`, `unionBy`, `unionWith`, `uniq`, `uniqBy`,
72 * `uniqWith`, `unset`, `unshift`, `unzip`, `unzipWith`, `values`, `valuesIn`,
73 * `without`, `wrap`, `xor`, `xorBy`, `xorWith`, `zip`, `zipObject`,
74 * `zipObjectDeep`, and `zipWith`
76 * The wrapper methods that are **not** chainable by default are:
77 * `add`, `attempt`, `camelCase`, `capitalize`, `ceil`, `clamp`, `clone`,
78 * `cloneDeep`, `cloneDeepWith`, `cloneWith`, `deburr`, `endsWith`, `eq`,
79 * `escape`, `escapeRegExp`, `every`, `find`, `findIndex`, `findKey`,
80 * `findLast`, `findLastIndex`, `findLastKey`, `floor`, `forEach`, `forEachRight`,
81 * `forIn`, `forInRight`, `forOwn`, `forOwnRight`, `get`, `gt`, `gte`, `has`,
82 * `hasIn`, `head`, `identity`, `includes`, `indexOf`, `inRange`, `invoke`,
83 * `isArguments`, `isArray`, `isArrayLike`, `isArrayLikeObject`, `isBoolean`,
84 * `isDate`, `isElement`, `isEmpty`, `isEqual`, `isEqualWith`, `isError`,
85 * `isFinite`, `isFunction`, `isInteger`, `isLength`, `isMatch`, `isMatchWith`,
86 * `isNaN`, `isNative`, `isNil`, `isNull`, `isNumber`, `isObject`, `isObjectLike`,
87 * `isPlainObject`, `isRegExp`, `isSafeInteger`, `isString`, `isUndefined`,
88 * `isTypedArray`, `join`, `kebabCase`, `last`, `lastIndexOf`, `lowerCase`,
89 * `lowerFirst`, `lt`, `lte`, `max`, `maxBy`, `mean`, `min`, `minBy`,
90 * `noConflict`, `noop`, `now`, `pad`, `padEnd`, `padStart`, `parseInt`,
91 * `pop`, `random`, `reduce`, `reduceRight`, `repeat`, `result`, `round`,
92 * `runInContext`, `sample`, `shift`, `size`, `snakeCase`, `some`, `sortedIndex`,
93 * `sortedIndexBy`, `sortedLastIndex`, `sortedLastIndexBy`, `startCase`,
94 * `startsWith`, `subtract`, `sum`, `sumBy`, `template`, `times`, `toLower`,
95 * `toInteger`, `toLength`, `toNumber`, `toSafeInteger`, `toString`, `toUpper`,
96 * `trim`, `trimEnd`, `trimStart`, `truncate`, `unescape`, `uniqueId`,
97 * `upperCase`, `upperFirst`, `value`, and `words`
102 * @param {*} value The value to wrap in a `lodash` instance.
103 * @returns {Object} Returns the new `lodash` wrapper instance.
106 * function square(n) {
110 * var wrapped = _([1, 2, 3]);
112 * // Returns an unwrapped value.
113 * wrapped.reduce(_.add);
116 * // Returns a wrapped value.
117 * var squares = wrapped.map(square);
119 * _.isArray(squares);
122 * _.isArray(squares.value());
125 function lodash(value) {
126 if (isObjectLike(value) && !isArray(value) && !(value instanceof LazyWrapper)) {
127 if (value instanceof LodashWrapper) {
130 if (hasOwnProperty.call(value, '__wrapped__')) {
131 return wrapperClone(value);
134 return new LodashWrapper(value);
137 // Ensure wrappers are instances of `baseLodash`.
138 lodash.prototype = baseLodash.prototype;
140 module.exports = lodash;