mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 19:07:49 +00:00
Add lazy drop, dropRight, initial, last, rest, reverse, and takeRight.
This commit is contained in:
96
lodash.js
96
lodash.js
@@ -4678,6 +4678,24 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getLazyView(type, size, dir) {
|
||||||
|
size = size == null ? 1 : (+size || 0);
|
||||||
|
return {
|
||||||
|
'type': type + (dir < 0 ? 'Right' : ''),
|
||||||
|
'size': (size < 0 ? 0 : size)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function lazyDrop(n) {
|
||||||
|
var result = new LazyWrapper(this);
|
||||||
|
result.views.push(getLazyView('drop', n, result.dir));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function lazyDropRight(n) {
|
||||||
|
return this.reverse().drop(n).reverse();
|
||||||
|
}
|
||||||
|
|
||||||
function lazyFilter(predicate, thisArg) {
|
function lazyFilter(predicate, thisArg) {
|
||||||
predicate = getCallback(predicate, thisArg, 3);
|
predicate = getCallback(predicate, thisArg, 3);
|
||||||
|
|
||||||
@@ -4690,6 +4708,14 @@
|
|||||||
return this.take(1).value()[0];
|
return this.take(1).value()[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function lazyInitial() {
|
||||||
|
return this.dropRight(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
function lazyLast() {
|
||||||
|
return this.takeRight(1).value()[0];
|
||||||
|
}
|
||||||
|
|
||||||
function lazyMap(iteratee, thisArg) {
|
function lazyMap(iteratee, thisArg) {
|
||||||
iteratee = getCallback(iteratee, thisArg, 3);
|
iteratee = getCallback(iteratee, thisArg, 3);
|
||||||
|
|
||||||
@@ -4698,17 +4724,29 @@
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function lazyTake(n) {
|
function lazyRest() {
|
||||||
n = n == null ? 1 : n;
|
return this.drop(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
function lazyReverse() {
|
||||||
var result = new LazyWrapper(this);
|
var result = new LazyWrapper(this);
|
||||||
result.views.push({ 'type': 'take', 'size': n });
|
result.dir *= -1;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function lazyTake(n) {
|
||||||
|
var result = new LazyWrapper(this);
|
||||||
|
result.views.push(getLazyView('take', n, result.dir));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function lazyTakeRight(n) {
|
||||||
|
return this.reverse().take(n).reverse();
|
||||||
|
}
|
||||||
|
|
||||||
function lazyValue() {
|
function lazyValue() {
|
||||||
var array = this.wrapped;
|
var array = this.wrapped;
|
||||||
if (array instanceof LodashWrapper || array instanceof LazyWrapper) {
|
if (array instanceof LodashWrapper) {
|
||||||
array = array.value();
|
array = array.value();
|
||||||
}
|
}
|
||||||
var start = 0,
|
var start = 0,
|
||||||
@@ -4732,6 +4770,7 @@
|
|||||||
index = (dir == 1 ? start : end) - dir,
|
index = (dir == 1 ? start : end) - dir,
|
||||||
iteratees = this.iteratees,
|
iteratees = this.iteratees,
|
||||||
iterateesLength = iteratees.length,
|
iterateesLength = iteratees.length,
|
||||||
|
resIndex = -1,
|
||||||
resLimit = end - start,
|
resLimit = end - start,
|
||||||
result = [];
|
result = [];
|
||||||
|
|
||||||
@@ -4763,7 +4802,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result.push(value);
|
result[++resIndex] = value;
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -9818,13 +9857,22 @@
|
|||||||
*/
|
*/
|
||||||
lodash.VERSION = VERSION;
|
lodash.VERSION = VERSION;
|
||||||
|
|
||||||
// assign default placeholders
|
// add functions to the lazy wrapper
|
||||||
arrayEach(['bind', 'bindKey', 'curry', 'curryRight', 'partial', 'partialRight'], function(methodName) {
|
LazyWrapper.prototype.drop = lazyDrop;
|
||||||
lodash[methodName].placeholder = lodash;
|
LazyWrapper.prototype.dropRight = lazyDropRight;
|
||||||
});
|
LazyWrapper.prototype.filter = lazyFilter;
|
||||||
|
LazyWrapper.prototype.first = lazyFirst;
|
||||||
|
LazyWrapper.prototype.initial = lazyInitial;
|
||||||
|
LazyWrapper.prototype.last = lazyLast;
|
||||||
|
LazyWrapper.prototype.map = lazyMap;
|
||||||
|
LazyWrapper.prototype.rest = lazyRest;
|
||||||
|
LazyWrapper.prototype.reverse = lazyReverse;
|
||||||
|
LazyWrapper.prototype.take = lazyTake;
|
||||||
|
LazyWrapper.prototype.takeRight = lazyTakeRight;
|
||||||
|
LazyWrapper.prototype.value = lazyValue;
|
||||||
|
|
||||||
// ensure `new LodashWrapper` is an instance of `lodash`
|
// ensure `new LodashWrapper` is an instance of `lodash`
|
||||||
LodashWrapper.prototype = lodash.prototype
|
LodashWrapper.prototype = lodash.prototype;
|
||||||
|
|
||||||
// add functions to the memoize cache
|
// add functions to the memoize cache
|
||||||
MemCache.prototype.get = memGet;
|
MemCache.prototype.get = memGet;
|
||||||
@@ -9832,16 +9880,15 @@
|
|||||||
MemCache.prototype.set = memSet;
|
MemCache.prototype.set = memSet;
|
||||||
memoize.Cache = MemCache;
|
memoize.Cache = MemCache;
|
||||||
|
|
||||||
// add functions to the lazy wrapper
|
// assign default placeholders
|
||||||
LazyWrapper.prototype.filter = lazyFilter;
|
arrayEach(['bind', 'bindKey', 'curry', 'curryRight', 'partial', 'partialRight'], function(methodName) {
|
||||||
LazyWrapper.prototype.first = lazyFirst;
|
lodash[methodName].placeholder = lodash;
|
||||||
LazyWrapper.prototype.map = lazyMap;
|
});
|
||||||
LazyWrapper.prototype.take = lazyTake;
|
|
||||||
LazyWrapper.prototype.value = lazyValue;
|
|
||||||
|
|
||||||
// add `LazyWrapper` functions
|
// add `LazyWrapper` functions
|
||||||
arrayEach(['map', 'filter', 'take'], function(methodName) {
|
arrayEach(['drop', 'dropRight', 'filter', 'first', 'initial', 'last', 'map', 'rest', 'take', 'takeRight'], function(methodName) {
|
||||||
var func = LazyWrapper.prototype[methodName];
|
var func = LazyWrapper.prototype[methodName],
|
||||||
|
chainAll = !/^(?:first|last)$/.test(methodName);
|
||||||
|
|
||||||
lodash.prototype[methodName] = function() {
|
lodash.prototype[methodName] = function() {
|
||||||
var value = this.__wrapped__,
|
var value = this.__wrapped__,
|
||||||
@@ -9853,9 +9900,8 @@
|
|||||||
value = lodash[methodName].apply(lodash, args);
|
value = lodash[methodName].apply(lodash, args);
|
||||||
} else {
|
} else {
|
||||||
value = func.apply(isLazy ? value : new LazyWrapper(this), arguments);
|
value = func.apply(isLazy ? value : new LazyWrapper(this), arguments);
|
||||||
isLazy = true;
|
|
||||||
}
|
}
|
||||||
return (isLazy || this.__chain__) ? new LodashWrapper(value, true) : value;
|
return (chainAll || this.__chain__) ? new LodashWrapper(value, true) : value;
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -9864,6 +9910,12 @@
|
|||||||
lodash.prototype.toString = wrapperToString;
|
lodash.prototype.toString = wrapperToString;
|
||||||
lodash.prototype.toJSON = lodash.prototype.value = lodash.prototype.valueOf = wrapperValueOf;
|
lodash.prototype.toJSON = lodash.prototype.value = lodash.prototype.valueOf = wrapperValueOf;
|
||||||
|
|
||||||
|
// add function aliases to the lodash wrapper
|
||||||
|
lodash.prototype.collect = lodash.prototype.map;
|
||||||
|
lodash.prototype.head = lodash.prototype.first;
|
||||||
|
lodash.prototype.select = lodash.prototype.filter;
|
||||||
|
lodash.prototype.tail = lodash.prototype.rest;
|
||||||
|
|
||||||
// add `Array.prototype` functions
|
// add `Array.prototype` functions
|
||||||
arrayEach(['concat', 'join', 'pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(methodName) {
|
arrayEach(['concat', 'join', 'pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(methodName) {
|
||||||
var arrayFunc = arrayProto[methodName],
|
var arrayFunc = arrayProto[methodName],
|
||||||
@@ -9892,10 +9944,6 @@
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// add function aliases to the lodash wrapper
|
|
||||||
lodash.prototype.collect = lodash.prototype.map;
|
|
||||||
lodash.prototype.select = lodash.prototype.filter;
|
|
||||||
|
|
||||||
return lodash;
|
return lodash;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user