Add LazyWrapper#slice.

This commit is contained in:
John-David Dalton
2014-10-19 22:08:05 -07:00
parent f5a34d5191
commit 6dfc2aca4b
2 changed files with 38 additions and 2 deletions

View File

@@ -10075,6 +10075,17 @@
});
};
LazyWrapper.prototype.slice = function(start, end) {
start = start == null ? 0 : (+start || 0);
var result = start < 0 ? this.takeRight(-start) : this.drop(start);
if (typeof end != 'undefined') {
end = (+end || 0);
result = end < 0 ? result.dropRight(-end) : result.take(end - start);
}
return result;
};
// add `LazyWrapper` methods to `LodashWrapper`
baseForOwn(LazyWrapper.prototype, function(func, methodName) {
var retUnwrapped = /^(?:first|last)$/.test(methodName);

View File

@@ -10006,7 +10006,7 @@
test('should work with a negative `start` <= negative `array.length`', 3, function() {
_.each([-3, -4, -Infinity], function(start) {
deepEqual(_.slice(array, start), [1, 2, 3]);
deepEqual(_.slice(array, start), array);
});
});
@@ -10022,7 +10022,7 @@
test('should work with a `end` >= `array.length`', 4, function() {
_.each([3, 4, Math.pow(2, 32), Infinity], function(end) {
deepEqual(_.slice(array, 0, end), [1, 2, 3]);
deepEqual(_.slice(array, 0, end), array);
});
});
@@ -10060,6 +10060,31 @@
deepEqual(actual, array);
notStrictEqual(actual, array)
});
test('should work in a lazy chain sequence', 12, function() {
if (!isNpm) {
var wrapped = _(array);
deepEqual(wrapped.slice(0, -1).value(), [1, 2]);
deepEqual(wrapped.slice(1).value(), [2, 3]);
deepEqual(wrapped.slice(-1).value(), [3]);
deepEqual(wrapped.slice(4).value(), []);
deepEqual(wrapped.slice(3, 2).value(), []);
deepEqual(wrapped.slice(0, -4).value(), []);
deepEqual(wrapped.slice(0, null).value(), []);
deepEqual(wrapped.slice(0, 4).value(), array);
deepEqual(wrapped.slice(-4).value(), array);
deepEqual(wrapped.slice(null).value(), array);
deepEqual(wrapped.slice(0, 1).value(), [1]);
deepEqual(wrapped.slice(NaN, '1').value(), [1]);
}
else {
skipTest(12);
}
});
}());
/*--------------------------------------------------------------------------*/