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