UNPKG

1.79 kBJavaScriptView Raw
1var baseWrapperValue = require('./_baseWrapperValue'),
2 getView = require('./_getView'),
3 isArray = require('./isArray');
4
5/** Used to indicate the type of lazy iteratees. */
6var LAZY_FILTER_FLAG = 1,
7 LAZY_MAP_FLAG = 2;
8
9/* Built-in method references for those with the same name as other `lodash` methods. */
10var nativeMin = Math.min;
11
12/**
13 * Extracts the unwrapped value from its lazy wrapper.
14 *
15 * @private
16 * @name value
17 * @memberOf LazyWrapper
18 * @returns {*} Returns the unwrapped value.
19 */
20function lazyValue() {
21 var array = this.__wrapped__.value(),
22 dir = this.__dir__,
23 isArr = isArray(array),
24 isRight = dir < 0,
25 arrLength = isArr ? array.length : 0,
26 view = getView(0, arrLength, this.__views__),
27 start = view.start,
28 end = view.end,
29 length = end - start,
30 index = isRight ? end : (start - 1),
31 iteratees = this.__iteratees__,
32 iterLength = iteratees.length,
33 resIndex = 0,
34 takeCount = nativeMin(length, this.__takeCount__);
35
36 if (!isArr || (!isRight && arrLength == length && takeCount == length)) {
37 return baseWrapperValue(array, this.__actions__);
38 }
39 var result = [];
40
41 outer:
42 while (length-- && resIndex < takeCount) {
43 index += dir;
44
45 var iterIndex = -1,
46 value = array[index];
47
48 while (++iterIndex < iterLength) {
49 var data = iteratees[iterIndex],
50 iteratee = data.iteratee,
51 type = data.type,
52 computed = iteratee(value);
53
54 if (type == LAZY_MAP_FLAG) {
55 value = computed;
56 } else if (!computed) {
57 if (type == LAZY_FILTER_FLAG) {
58 continue outer;
59 } else {
60 break outer;
61 }
62 }
63 }
64 result[resIndex++] = value;
65 }
66 return result;
67}
68
69module.exports = lazyValue;