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