mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 18:37:50 +00:00
Ensure _.first supports shortcut fusion. [closes #2447]
This commit is contained in:
@@ -16545,6 +16545,9 @@
|
|||||||
lodash.prototype.reverse = wrapperReverse;
|
lodash.prototype.reverse = wrapperReverse;
|
||||||
lodash.prototype.toJSON = lodash.prototype.valueOf = lodash.prototype.value = wrapperValue;
|
lodash.prototype.toJSON = lodash.prototype.valueOf = lodash.prototype.value = wrapperValue;
|
||||||
|
|
||||||
|
// Add lazy aliases.
|
||||||
|
lodash.prototype.first = lodash.prototype.head;
|
||||||
|
|
||||||
if (iteratorSymbol) {
|
if (iteratorSymbol) {
|
||||||
lodash.prototype[iteratorSymbol] = wrapperToIterator;
|
lodash.prototype[iteratorSymbol] = wrapperToIterator;
|
||||||
}
|
}
|
||||||
|
|||||||
99
test/test.js
99
test/test.js
@@ -7923,63 +7923,70 @@
|
|||||||
assert.deepEqual(actual, [1, 4, 7]);
|
assert.deepEqual(actual, [1, 4, 7]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QUnit.test('should be aliased', function(assert) {
|
||||||
|
assert.expect(1);
|
||||||
|
|
||||||
|
assert.strictEqual(_.first, _.head);
|
||||||
|
});
|
||||||
|
|
||||||
QUnit.test('should return an unwrapped value when implicitly chaining', function(assert) {
|
QUnit.test('should return an unwrapped value when implicitly chaining', function(assert) {
|
||||||
assert.expect(1);
|
|
||||||
|
|
||||||
if (!isNpm) {
|
|
||||||
assert.strictEqual(_(array).head(), 1);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
skipAssert(assert);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
QUnit.test('should return a wrapped value when explicitly chaining', function(assert) {
|
|
||||||
assert.expect(1);
|
|
||||||
|
|
||||||
if (!isNpm) {
|
|
||||||
assert.ok(_(array).chain().head() instanceof _);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
skipAssert(assert);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
QUnit.test('should not execute immediately when explicitly chaining', function(assert) {
|
|
||||||
assert.expect(1);
|
|
||||||
|
|
||||||
if (!isNpm) {
|
|
||||||
var wrapped = _(array).chain().head();
|
|
||||||
assert.strictEqual(wrapped.__wrapped__, array);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
skipAssert(assert);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
QUnit.test('should work in a lazy sequence', function(assert) {
|
|
||||||
assert.expect(2);
|
assert.expect(2);
|
||||||
|
|
||||||
if (!isNpm) {
|
if (!isNpm) {
|
||||||
var largeArray = lodashStable.range(LARGE_ARRAY_SIZE),
|
var wrapped = _(array);
|
||||||
smallArray = array;
|
assert.strictEqual(wrapped.head(), 1);
|
||||||
|
assert.strictEqual(wrapped.first(), 1);
|
||||||
lodashStable.times(2, function(index) {
|
|
||||||
var array = index ? largeArray : smallArray,
|
|
||||||
wrapped = _(array).filter(isEven);
|
|
||||||
|
|
||||||
assert.strictEqual(wrapped.head(), _.head(_.filter(array, isEven)));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
skipAssert(assert, 2);
|
skipAssert(assert, 2);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test('should be aliased', function(assert) {
|
QUnit.test('should return a wrapped value when explicitly chaining', function(assert) {
|
||||||
assert.expect(1);
|
assert.expect(2);
|
||||||
|
|
||||||
assert.strictEqual(_.first, _.head);
|
if (!isNpm) {
|
||||||
|
var wrapped = _(array).chain();
|
||||||
|
assert.ok(wrapped.head() instanceof _);
|
||||||
|
assert.ok(wrapped.first() instanceof _);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
skipAssert(assert, 2);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
QUnit.test('should not execute immediately when explicitly chaining', function(assert) {
|
||||||
|
assert.expect(2);
|
||||||
|
|
||||||
|
if (!isNpm) {
|
||||||
|
var wrapped = _(array).chain();
|
||||||
|
assert.strictEqual(wrapped.head().__wrapped__, array);
|
||||||
|
assert.strictEqual(wrapped.first().__wrapped__, array);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
skipAssert(assert, 2);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
QUnit.test('should work in a lazy sequence', function(assert) {
|
||||||
|
assert.expect(4);
|
||||||
|
|
||||||
|
if (!isNpm) {
|
||||||
|
var largeArray = lodashStable.range(LARGE_ARRAY_SIZE),
|
||||||
|
smallArray = array;
|
||||||
|
|
||||||
|
lodashStable.each(['head', 'first'], function(methodName) {
|
||||||
|
lodashStable.times(2, function(index) {
|
||||||
|
var array = index ? largeArray : smallArray,
|
||||||
|
actual = _(array).filter(isEven)[methodName]();
|
||||||
|
|
||||||
|
assert.strictEqual(actual, _[methodName](_.filter(array, isEven)));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
skipAssert(assert, 4);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user