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