mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 02:17:50 +00:00
Fix shortcut fusion.
This commit is contained in:
129
lodash.js
129
lodash.js
@@ -254,7 +254,6 @@
|
|||||||
|
|
||||||
/** Used to map lazy iteratee flags to lazy methods */
|
/** Used to map lazy iteratee flags to lazy methods */
|
||||||
var lazyIterateeTypes = {
|
var lazyIterateeTypes = {
|
||||||
'dropWhile': LAZY_WHILE_FLAG,
|
|
||||||
'filter': LAZY_FILTER_FLAG,
|
'filter': LAZY_FILTER_FLAG,
|
||||||
'map': LAZY_MAP_FLAG,
|
'map': LAZY_MAP_FLAG,
|
||||||
'takeWhile': LAZY_WHILE_FLAG
|
'takeWhile': LAZY_WHILE_FLAG
|
||||||
@@ -4713,17 +4712,32 @@
|
|||||||
* @returns {Object} Returns a `LazyWrapper` instance.
|
* @returns {Object} Returns a `LazyWrapper` instance.
|
||||||
*/
|
*/
|
||||||
function LazyWrapper(value) {
|
function LazyWrapper(value) {
|
||||||
if (value instanceof LazyWrapper) {
|
this.dir = 1;
|
||||||
this.dir = value.dir;
|
this.dropCount = 0;
|
||||||
this.iteratees = baseSlice(value.iteratees);
|
this.filtered = false;
|
||||||
this.views = baseSlice(value.views);
|
this.iteratees = [];
|
||||||
this.wrapped = value.wrapped;
|
this.takeCount = POSITIVE_INFINITY;
|
||||||
} else {
|
this.views = [];
|
||||||
this.dir = 1;
|
this.wrapped = value;
|
||||||
this.iteratees = [];
|
}
|
||||||
this.views = [];
|
|
||||||
this.wrapped = value;
|
/**
|
||||||
}
|
* Creates a clone of the `LazyWrapper` object.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @name clone
|
||||||
|
* @memberOf LazyWrapper
|
||||||
|
* @returns {Object} Returns the cloned `LazyWrapper` object.
|
||||||
|
*/
|
||||||
|
function lazyClone() {
|
||||||
|
var result = new LazyWrapper(this.wrapped);
|
||||||
|
result.dir = this.dir;
|
||||||
|
result.dropCount = this.dropCount;
|
||||||
|
result.filtered = this.filtered;
|
||||||
|
result.takeCount = this.takeCount;
|
||||||
|
push.apply(result.iteratees, this.iteratees);
|
||||||
|
push.apply(result.views, this.views);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -4735,8 +4749,11 @@
|
|||||||
* @returns {Object} Returns the new reversed `LazyWrapper` object.
|
* @returns {Object} Returns the new reversed `LazyWrapper` object.
|
||||||
*/
|
*/
|
||||||
function lazyReverse() {
|
function lazyReverse() {
|
||||||
var result = new LazyWrapper(this);
|
var filtered = this.filtered,
|
||||||
result.dir *= -1;
|
result = filtered ? new LazyWrapper(this) : this.clone();
|
||||||
|
|
||||||
|
result.dir = this.dir * -1;
|
||||||
|
result.filtered = filtered;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4750,17 +4767,18 @@
|
|||||||
*/
|
*/
|
||||||
function lazyValue() {
|
function lazyValue() {
|
||||||
var array = this.wrapped;
|
var array = this.wrapped;
|
||||||
if (array instanceof LodashWrapper) {
|
if (array instanceof LodashWrapper || array instanceof LazyWrapper) {
|
||||||
array = array.value();
|
array = array.value();
|
||||||
}
|
}
|
||||||
var length = array.length,
|
var length = array.length,
|
||||||
start = 0,
|
start = 0,
|
||||||
end = length,
|
end = length,
|
||||||
views = this.views,
|
views = this.views,
|
||||||
|
viewIndex = -1,
|
||||||
viewsLength = views.length;
|
viewsLength = views.length;
|
||||||
|
|
||||||
while (viewsLength--) {
|
while (++viewIndex < viewsLength) {
|
||||||
var view = views[viewsLength],
|
var view = views[viewIndex],
|
||||||
size = view.size;
|
size = view.size;
|
||||||
|
|
||||||
switch (view.type) {
|
switch (view.type) {
|
||||||
@@ -4771,15 +4789,19 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
var dir = this.dir,
|
var dir = this.dir,
|
||||||
index = (dir == 1 ? start - 1 : end),
|
dropCount = this.dropCount,
|
||||||
|
droppedCount = 0,
|
||||||
|
doneDropping = !dropCount,
|
||||||
|
takeCount = nativeMin(end - start, this.takeCount - dropCount),
|
||||||
|
isRight = dir < 0,
|
||||||
|
index = isRight ? end : start - 1,
|
||||||
iteratees = this.iteratees,
|
iteratees = this.iteratees,
|
||||||
iterateesLength = iteratees.length,
|
iterateesLength = iteratees.length,
|
||||||
resIndex = 0,
|
resIndex = 0,
|
||||||
resLimit = end - start,
|
|
||||||
result = [];
|
result = [];
|
||||||
|
|
||||||
outer:
|
outer:
|
||||||
while (length-- && resIndex < resLimit) {
|
while (length-- && resIndex < takeCount) {
|
||||||
var iterateesIndex = -1,
|
var iterateesIndex = -1,
|
||||||
value = array[index += dir];
|
value = array[index += dir];
|
||||||
|
|
||||||
@@ -4799,9 +4821,13 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result[resIndex++] = value;
|
if (doneDropping) {
|
||||||
|
result[resIndex++] = value;
|
||||||
|
} else {
|
||||||
|
doneDropping = ++droppedCount < dropCount;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return isRight ? result.reverse() : result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*------------------------------------------------------------------------*/
|
/*------------------------------------------------------------------------*/
|
||||||
@@ -9896,26 +9922,34 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
// add `LazyWrapper` methods that accept an `iteratee` value
|
// add `LazyWrapper` methods that accept an `iteratee` value
|
||||||
arrayEach(['dropWhile', 'filter', 'map', 'takeWhile'], function(methodName) {
|
arrayEach(['filter', 'map', 'takeWhile'], function(methodName, index) {
|
||||||
|
var isFilter = !index;
|
||||||
|
|
||||||
LazyWrapper.prototype[methodName] = function(iteratee, thisArg) {
|
LazyWrapper.prototype[methodName] = function(iteratee, thisArg) {
|
||||||
iteratee = getCallback(iteratee, thisArg, 3);
|
iteratee = getCallback(iteratee, thisArg, 3);
|
||||||
|
|
||||||
var result = new LazyWrapper(this);
|
var result = this.clone();
|
||||||
|
result.filtered = isFilter || result.filtered;
|
||||||
result.iteratees.push({ 'iteratee': iteratee, 'type': lazyIterateeTypes[methodName] });
|
result.iteratees.push({ 'iteratee': iteratee, 'type': lazyIterateeTypes[methodName] });
|
||||||
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) {
|
arrayEach(['drop', 'take'], function(methodName, index) {
|
||||||
var whileName = methodName + 'While';
|
var countName = methodName + 'Count',
|
||||||
|
whileName = methodName + 'While';
|
||||||
|
|
||||||
LazyWrapper.prototype[methodName] = function(n) {
|
LazyWrapper.prototype[methodName] = function(n) {
|
||||||
n = n == null ? 1 : (+n || 0);
|
n = n == null ? 1 : nativeMax(+n || 0, 0);
|
||||||
|
|
||||||
var result = new LazyWrapper(this);
|
var result = this.clone();
|
||||||
|
if (this.filtered) {
|
||||||
|
result[countName] = n;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
result.views.push({
|
result.views.push({
|
||||||
'size': (n < 0 ? 0 : n),
|
'size': n,
|
||||||
'type': methodName + (result.dir < 0 ? 'Right' : '')
|
'type': methodName + (result.dir < 0 ? 'Right' : '')
|
||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
@@ -9931,21 +9965,45 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
// add `LazyWrapper` methods for `_.first` and `_.last`
|
// add `LazyWrapper` methods for `_.first` and `_.last`
|
||||||
arrayEach(['first', 'last'], function(methodName) {
|
arrayEach(['first', 'last'], function(methodName, index) {
|
||||||
var takeName = 'take' + (methodName == 'last' ? 'Right': '');
|
var takeName = 'take' + (index ? 'Right': '');
|
||||||
|
|
||||||
LazyWrapper.prototype[methodName] = function() {
|
LazyWrapper.prototype[methodName] = function() {
|
||||||
return this[takeName](1).value()[0];
|
return this[takeName](1).value()[0];
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// add `LazyWrapper` methods for `_.initial` and `_.rest`
|
// add `LazyWrapper` methods for `_.initial` and `_.rest`
|
||||||
arrayEach(['initial', 'rest'], function(methodName) {
|
arrayEach(['initial', 'rest'], function(methodName, index) {
|
||||||
var dropName = 'drop' + (methodName == 'initial' ? 'Right': '');
|
var dropName = 'drop' + (index ? '' : 'Right');
|
||||||
|
|
||||||
LazyWrapper.prototype[methodName] = function() {
|
LazyWrapper.prototype[methodName] = function() {
|
||||||
return this[dropName](1);
|
return this[dropName](1);
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
LazyWrapper.prototype.dropWhile = function(iteratee, thisArg) {
|
||||||
|
iteratee = getCallback(iteratee, thisArg, 3);
|
||||||
|
|
||||||
|
var done,
|
||||||
|
lastIndex,
|
||||||
|
isRight = this.dir < 0;
|
||||||
|
|
||||||
|
return this.filter(function(value, index, array) {
|
||||||
|
done = done && (isRight ? index < lastIndex : index > lastIndex);
|
||||||
|
lastIndex = index;
|
||||||
|
return done || (done = !iteratee(value, index, array));
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
LazyWrapper.prototype.reject = function(iteratee, thisArg) {
|
||||||
|
iteratee = getCallback(iteratee, thisArg, 3);
|
||||||
|
|
||||||
|
return this.filter(function(value, index, array) {
|
||||||
|
return !iteratee(value, index, array);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
// add `LazyWrapper` methods to `LodashWrapper`
|
// add `LazyWrapper` methods to `LodashWrapper`
|
||||||
baseForOwn(LazyWrapper.prototype, function(func, methodName) {
|
baseForOwn(LazyWrapper.prototype, function(func, methodName) {
|
||||||
var retUnwrapped = /^(?:first|last)$/.test(methodName);
|
var retUnwrapped = /^(?:first|last)$/.test(methodName);
|
||||||
@@ -9965,8 +10023,8 @@
|
|||||||
return lodash[methodName].apply(lodash, otherArgs);
|
return lodash[methodName].apply(lodash, otherArgs);
|
||||||
}
|
}
|
||||||
if (isLazy || isArray(value)) {
|
if (isLazy || isArray(value)) {
|
||||||
var result = func.apply(isLazy ? value : new LazyWrapper(value), args);
|
var result = func.apply(isLazy ? value : new LazyWrapper(this), args);
|
||||||
return new LodashWrapper(result, chainAll, baseSlice(this.__queue__));
|
return new LodashWrapper(result, chainAll);
|
||||||
}
|
}
|
||||||
return this.thru(function(value) {
|
return this.thru(function(value) {
|
||||||
var otherArgs = [value];
|
var otherArgs = [value];
|
||||||
@@ -10008,6 +10066,7 @@
|
|||||||
LodashWrapper.prototype = lodash.prototype;
|
LodashWrapper.prototype = lodash.prototype;
|
||||||
|
|
||||||
// add functions to the lazy wrapper
|
// add functions to the lazy wrapper
|
||||||
|
LazyWrapper.prototype.clone = lazyClone;
|
||||||
LazyWrapper.prototype.reverse = lazyReverse;
|
LazyWrapper.prototype.reverse = lazyReverse;
|
||||||
LazyWrapper.prototype.value = lazyValue;
|
LazyWrapper.prototype.value = lazyValue;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user