Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
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
8
/** Used for native 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 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
21
* called.
22
*
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.
26
*
27
* Chaining is supported in custom builds as long as the `_#value` method is
28
* directly or indirectly included in the build.
29
*
30
* In addition to lodash methods, wrappers have `Array` and `String` methods.
31
*
32
* The wrapper `Array` methods are:
33
* `concat`, `join`, `pop`, `push`, `reverse`, `shift`, `slice`, `sort`,
34
* `splice`, and `unshift`
35
*
36
* The wrapper `String` methods are:
37
* `replace` and `split`
38
*
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`,
43
* and `where`
44
*
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`, `mapKeys`, `mapValues`, `matches`, `matchesProperty`,
54
* `memoize`, `merge`, `method`, `methodOf`, `mixin`, `negate`, `omit`, `once`,
55
* `pairs`, `partial`, `partialRight`, `partition`, `pick`, `plant`, `pluck`,
56
* `property`, `propertyOf`, `pull`, `pullAt`, `push`, `range`, `rearg`,
57
* `reject`, `remove`, `rest`, `restParam`, `reverse`, `set`, `shuffle`,
58
* `slice`, `sort`, `sortBy`, `sortByAll`, `sortByOrder`, `splice`, `spread`,
59
* `take`, `takeRight`, `takeRightWhile`, `takeWhile`, `tap`, `throttle`,
60
* `thru`, `times`, `toArray`, `toPlainObject`, `transform`, `union`, `uniq`,
61
* `unshift`, `unzip`, `unzipWith`, `values`, `valuesIn`, `where`, `without`,
62
* `wrap`, `xor`, `zip`, `zipObject`, `zipWith`
63
*
64
* The wrapper methods that are **not** chainable by default are:
65
* `add`, `attempt`, `camelCase`, `capitalize`, `clone`, `cloneDeep`, `deburr`,
66
* `endsWith`, `escape`, `escapeRegExp`, `every`, `find`, `findIndex`, `findKey`,
67
* `findLast`, `findLastIndex`, `findLastKey`, `findWhere`, `first`, `get`,
68
* `gt`, `gte`, `has`, `identity`, `includes`, `indexOf`, `inRange`, `isArguments`,
69
* `isArray`, `isBoolean`, `isDate`, `isElement`, `isEmpty`, `isEqual`, `isError`,
70
* `isFinite` `isFunction`, `isMatch`, `isNative`, `isNaN`, `isNull`, `isNumber`,
71
* `isObject`, `isPlainObject`, `isRegExp`, `isString`, `isUndefined`,
72
* `isTypedArray`, `join`, `kebabCase`, `last`, `lastIndexOf`, `lt`, `lte`,
73
* `max`, `min`, `noConflict`, `noop`, `now`, `pad`, `padLeft`, `padRight`,
74
* `parseInt`, `pop`, `random`, `reduce`, `reduceRight`, `repeat`, `result`,
75
* `runInContext`, `shift`, `size`, `snakeCase`, `some`, `sortedIndex`,
76
* `sortedLastIndex`, `startCase`, `startsWith`, `sum`, `template`, `trim`,
77
* `trimLeft`, `trimRight`, `trunc`, `unescape`, `uniqueId`, `value`, and `words`
78
*
79
* The wrapper method `sample` will return a wrapped value when `n` is provided,
80
* otherwise an unwrapped value is returned.
81
*
82
* @name _
83
* @constructor
84
* @category Chain
85
* @param {*} value The value to wrap in a `lodash` instance.
86
* @returns {Object} Returns the new `lodash` wrapper instance.
87
* @example
88
*
89
* var wrapped = _([1, 2, 3]);
90
*
91
* // returns an unwrapped value
92
* wrapped.reduce(function(total, n) {
93
* return total + n;
94
* });
95
* // => 6
96
*
97
* // returns a wrapped value
98
* var squares = wrapped.map(function(n) {
99
* return n * n;
100
* });
101
*
102
* _.isArray(squares);
103
* // => false
104
*
105
* _.isArray(squares.value());
106
* // => true
107
*/
108
function lodash(value) {
109
if (isObjectLike(value) && !isArray(value) && !(value instanceof LazyWrapper)) {
110
if (value instanceof LodashWrapper) {
111
return value;
112
}
113
if (hasOwnProperty.call(value, '__chain__') && hasOwnProperty.call(value, '__wrapped__')) {
114
return wrapperClone(value);
115
}
116
}
117
return new LodashWrapper(value);
118
}
119
120
// Ensure wrappers are instances of `baseLodash`.
121
lodash.prototype = baseLodash.prototype;
122
123
module.exports = lodash;
124
125