mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-11 03:17:49 +00:00
Add getView and cleanup Lazy chaining.
This commit is contained in:
135
lodash.js
135
lodash.js
@@ -32,9 +32,9 @@
|
|||||||
HOT_SPAN = 16;
|
HOT_SPAN = 16;
|
||||||
|
|
||||||
/** Used to indicate the type of lazy iteratees */
|
/** Used to indicate the type of lazy iteratees */
|
||||||
var LAZY_FILTER_FLAG = 1,
|
var LAZY_FILTER_FLAG = 0,
|
||||||
LAZY_MAP_FLAG = 2,
|
LAZY_MAP_FLAG = 1,
|
||||||
LAZY_WHILE_FLAG = 3;
|
LAZY_WHILE_FLAG = 2;
|
||||||
|
|
||||||
/** Used as the `TypeError` message for "Functions" methods */
|
/** Used as the `TypeError` message for "Functions" methods */
|
||||||
var FUNC_ERROR_TEXT = 'Expected a function';
|
var FUNC_ERROR_TEXT = 'Expected a function';
|
||||||
@@ -253,13 +253,6 @@
|
|||||||
'`': '`'
|
'`': '`'
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Used to map iteratee types to lazy methods */
|
|
||||||
var lazyIterateeTypes = {
|
|
||||||
'filter': LAZY_FILTER_FLAG,
|
|
||||||
'map': LAZY_MAP_FLAG,
|
|
||||||
'takeWhile': LAZY_WHILE_FLAG
|
|
||||||
};
|
|
||||||
|
|
||||||
/** Used to determine if values are of the language type `Object` */
|
/** Used to determine if values are of the language type `Object` */
|
||||||
var objectTypes = {
|
var objectTypes = {
|
||||||
'function': true,
|
'function': true,
|
||||||
@@ -2966,6 +2959,34 @@
|
|||||||
return collection ? result(collection, target, fromIndex) : result;
|
return collection ? result(collection, target, fromIndex) : result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the view applying any `transforms` to the `start` and `end` positions.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @param {number} start The start of the view.
|
||||||
|
* @param {number} end The end of the view.
|
||||||
|
* @param {Array} [transforms] The transformations to apply to the view.
|
||||||
|
* @returns {Object} Returns an object containing the `start` and `end`
|
||||||
|
* positions of the view.
|
||||||
|
*/
|
||||||
|
function getView(start, end, transforms) {
|
||||||
|
var index = -1,
|
||||||
|
length = transforms ? transforms.length : 0;
|
||||||
|
|
||||||
|
while (++index < length) {
|
||||||
|
var data = transforms[index],
|
||||||
|
size = data.size;
|
||||||
|
|
||||||
|
switch (data.type) {
|
||||||
|
case 'drop': start += size; break;
|
||||||
|
case 'dropRight': end -= size; break;
|
||||||
|
case 'take': end = nativeMin(end, start + size); break;
|
||||||
|
case 'takeRight': start = nativeMax(start, end - size); break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { 'start': start, 'end': end };
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes an array clone.
|
* Initializes an array clone.
|
||||||
*
|
*
|
||||||
@@ -4798,17 +4819,12 @@
|
|||||||
*/
|
*/
|
||||||
function LazyWrapper(value) {
|
function LazyWrapper(value) {
|
||||||
this.dir = 1;
|
this.dir = 1;
|
||||||
|
this.dropCount = 0;
|
||||||
this.filtered = false;
|
this.filtered = false;
|
||||||
this.iteratees = [];
|
this.iteratees = null;
|
||||||
|
this.takeCount = POSITIVE_INFINITY;
|
||||||
|
this.views = null;
|
||||||
this.wrapped = value;
|
this.wrapped = value;
|
||||||
|
|
||||||
this.end =
|
|
||||||
this.endAfter =
|
|
||||||
this.startRight = POSITIVE_INFINITY;
|
|
||||||
|
|
||||||
this.start =
|
|
||||||
this.startAfter =
|
|
||||||
this.endRight = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -4820,19 +4836,16 @@
|
|||||||
* @returns {Object} Returns the cloned `LazyWrapper` object.
|
* @returns {Object} Returns the cloned `LazyWrapper` object.
|
||||||
*/
|
*/
|
||||||
function lazyClone() {
|
function lazyClone() {
|
||||||
var result = new LazyWrapper(this.wrapped);
|
var iteratees = this.iteratees,
|
||||||
|
views = this.views,
|
||||||
|
result = new LazyWrapper(this.wrapped);
|
||||||
|
|
||||||
result.dir = this.dir;
|
result.dir = this.dir;
|
||||||
|
result.dropCount = this.dropCount;
|
||||||
result.filtered = this.filtered;
|
result.filtered = this.filtered;
|
||||||
|
result.iteratees = iteratees ? baseSlice(iteratees) : null;
|
||||||
result.end = this.end;
|
result.takeCount = this.takeCount;
|
||||||
result.endAfter = this.endAfter;
|
result.views = views ? baseSlice(views) : null;
|
||||||
result.endRight = this.endRight;
|
|
||||||
|
|
||||||
result.start = this.start;
|
|
||||||
result.startAfter = this.startAfter;
|
|
||||||
result.startRight = this.startRight;
|
|
||||||
|
|
||||||
push.apply(result.iteratees, this.iteratees);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4863,32 +4876,29 @@
|
|||||||
*/
|
*/
|
||||||
function lazyValue() {
|
function lazyValue() {
|
||||||
var array = this.wrapped.value(),
|
var array = this.wrapped.value(),
|
||||||
length = array.length,
|
dir = this.dir,
|
||||||
start = this.start,
|
|
||||||
end = length - this.endRight;
|
|
||||||
|
|
||||||
start = nativeMax(start, end - this.startRight);
|
|
||||||
end = nativeMin(end, start + this.end);
|
|
||||||
|
|
||||||
var dir = this.dir,
|
|
||||||
startAfter = this.startAfter,
|
|
||||||
endAfter = nativeMin(end - start, this.endAfter - startAfter),
|
|
||||||
isRight = dir < 0,
|
isRight = dir < 0,
|
||||||
|
length = array.length,
|
||||||
|
view = getView(0, length, this.views),
|
||||||
|
start = view.start,
|
||||||
|
end = view.end,
|
||||||
|
dropCount = this.dropCount,
|
||||||
|
takeCount = nativeMin(end - start, this.takeCount - dropCount),
|
||||||
index = isRight ? end : start - 1,
|
index = isRight ? end : start - 1,
|
||||||
iteratees = this.iteratees,
|
iteratees = this.iteratees,
|
||||||
iterateesLength = iteratees.length,
|
iterLength = iteratees ? iteratees.length : 0,
|
||||||
resIndex = 0,
|
resIndex = 0,
|
||||||
result = [];
|
result = [];
|
||||||
|
|
||||||
outer:
|
outer:
|
||||||
while (length-- && resIndex < endAfter) {
|
while (length-- && resIndex < takeCount) {
|
||||||
index += dir;
|
index += dir;
|
||||||
|
|
||||||
var iterateesIndex = -1,
|
var iterIndex = -1,
|
||||||
value = array[index];
|
value = array[index];
|
||||||
|
|
||||||
while (++iterateesIndex < iterateesLength) {
|
while (++iterIndex < iterLength) {
|
||||||
var data = iteratees[iterateesIndex],
|
var data = iteratees[iterIndex],
|
||||||
iteratee = data.iteratee,
|
iteratee = data.iteratee,
|
||||||
computed = iteratee(value, index, array),
|
computed = iteratee(value, index, array),
|
||||||
type = data.type;
|
type = data.type;
|
||||||
@@ -4903,8 +4913,8 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (startAfter) {
|
if (dropCount) {
|
||||||
startAfter--;
|
dropCount--;
|
||||||
} else {
|
} else {
|
||||||
result[resIndex++] = value;
|
result[resIndex++] = value;
|
||||||
}
|
}
|
||||||
@@ -10006,27 +10016,32 @@
|
|||||||
LazyWrapper.prototype[methodName] = function(iteratee, thisArg) {
|
LazyWrapper.prototype[methodName] = function(iteratee, thisArg) {
|
||||||
iteratee = getCallback(iteratee, thisArg, 3);
|
iteratee = getCallback(iteratee, thisArg, 3);
|
||||||
|
|
||||||
var result = this.clone();
|
var result = this.clone(),
|
||||||
result.filtered = isFilter || result.filtered;
|
filtered = result.filtered,
|
||||||
result.iteratees.push({ 'iteratee': iteratee, 'type': lazyIterateeTypes[methodName] });
|
iteratees = result.iteratees || (result.iteratees = []);
|
||||||
|
|
||||||
|
result.filtered = filtered || index == LAZY_FILTER_FLAG || (index == LAZY_WHILE_FLAG && result.dir < 0);
|
||||||
|
iteratees.push({ 'iteratee': iteratee, 'type': index });
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// add `LazyWrapper` methods for `_.drop` and `_.take` variants
|
// add `LazyWrapper` methods for `_.drop` and `_.take` variants
|
||||||
arrayEach(['drop', 'take'], function(methodName, index) {
|
arrayEach(['drop', 'take'], function(methodName, index) {
|
||||||
var rangeName = index ? 'end' : 'start',
|
var countName = methodName + 'Count',
|
||||||
afterName = rangeName + 'After',
|
|
||||||
rightName = (index ? 'start' : 'end') + 'Right',
|
|
||||||
whileName = methodName + 'While';
|
whileName = methodName + 'While';
|
||||||
|
|
||||||
LazyWrapper.prototype[methodName] = function(n) {
|
LazyWrapper.prototype[methodName] = function(n) {
|
||||||
var result = this.clone(),
|
|
||||||
key = result.filtered ? afterName : (result.dir < 0 ? rightName : rangeName),
|
|
||||||
value = result[key];
|
|
||||||
|
|
||||||
n = n == null ? 1 : nativeMax(+n || 0, 0);
|
n = n == null ? 1 : nativeMax(+n || 0, 0);
|
||||||
result[key] = (nativeIsFinite(value) ? value : 0) + n;
|
|
||||||
|
var result = this.clone();
|
||||||
|
if (result.filtered) {
|
||||||
|
var value = result[countName];
|
||||||
|
result[countName] = index ? nativeMin(value, n) : (value + n);
|
||||||
|
} else {
|
||||||
|
var views = result.views || (result.views = []);
|
||||||
|
views.push({ 'size': n, 'type': methodName + (result.dir < 0 ? 'Right' : '') });
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -10035,9 +10050,7 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
LazyWrapper.prototype[methodName + 'RightWhile'] = function(predicate, thisArg) {
|
LazyWrapper.prototype[methodName + 'RightWhile'] = function(predicate, thisArg) {
|
||||||
var result = this.reverse()[whileName](predicate, thisArg);
|
return this.reverse()[whileName](predicate, thisArg).reverse();
|
||||||
result.filtered = true;
|
|
||||||
return result.reverse();
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user