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