mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 18:17:48 +00:00
Ensure array sequence methods don't error for falsey values.
This commit is contained in:
12
lodash.js
12
lodash.js
@@ -15394,18 +15394,26 @@
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add `Array` and `String` methods to `lodash.prototype`.
|
// Add `Array` methods to `lodash.prototype`.
|
||||||
arrayEach(['pop', 'push', 'shift', 'sort', 'splice', 'unshift'], function(methodName) {
|
arrayEach(['pop', 'push', 'shift', 'sort', 'splice', 'unshift'], function(methodName) {
|
||||||
var func = arrayProto[methodName],
|
var func = arrayProto[methodName],
|
||||||
chainName = /^(?:push|sort|unshift)$/.test(methodName) ? 'tap' : 'thru',
|
chainName = /^(?:push|sort|unshift)$/.test(methodName) ? 'tap' : 'thru',
|
||||||
|
mutates = methodName != 'sort',
|
||||||
retUnwrapped = /^(?:pop|shift)$/.test(methodName);
|
retUnwrapped = /^(?:pop|shift)$/.test(methodName);
|
||||||
|
|
||||||
lodash.prototype[methodName] = function() {
|
lodash.prototype[methodName] = function() {
|
||||||
var args = arguments;
|
var args = arguments;
|
||||||
if (retUnwrapped && !this.__chain__) {
|
if (retUnwrapped && !this.__chain__) {
|
||||||
return func.apply(this.value(), args);
|
var value = this.value();
|
||||||
|
if ((mutates ? !isArray(value) : (value == null))) {
|
||||||
|
value = [];
|
||||||
|
}
|
||||||
|
return func.apply(value, args);
|
||||||
}
|
}
|
||||||
return this[chainName](function(value) {
|
return this[chainName](function(value) {
|
||||||
|
if ((mutates ? !isArray(value) : (value == null))) {
|
||||||
|
value = [];
|
||||||
|
}
|
||||||
return func.apply(value, args);
|
return func.apply(value, args);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
90
test/test.js
90
test/test.js
@@ -24331,6 +24331,21 @@
|
|||||||
skipAssert(assert, 5);
|
skipAssert(assert, 5);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QUnit.test('should accept falsey arguments', function(assert) {
|
||||||
|
assert.expect(1);
|
||||||
|
|
||||||
|
var expected = lodashStable.map(falsey, alwaysTrue);
|
||||||
|
|
||||||
|
var actual = lodashStable.map(falsey, function(value, index) {
|
||||||
|
try {
|
||||||
|
var result = index ? _(value).pop() : _().pop();
|
||||||
|
return result === undefined;
|
||||||
|
} catch (e) {}
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(actual, expected);
|
||||||
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
@@ -24353,6 +24368,21 @@
|
|||||||
skipAssert(assert, 2);
|
skipAssert(assert, 2);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QUnit.test('should accept falsey arguments', function(assert) {
|
||||||
|
assert.expect(1);
|
||||||
|
|
||||||
|
var expected = lodashStable.map(falsey, alwaysTrue);
|
||||||
|
|
||||||
|
var actual = lodashStable.map(falsey, function(value, index) {
|
||||||
|
try {
|
||||||
|
var result = index ? _(value).push(1).value() : _().push(1).value();
|
||||||
|
return lodashStable.eq(result, value);
|
||||||
|
} catch (e) {}
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(actual, expected);
|
||||||
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
@@ -24379,6 +24409,21 @@
|
|||||||
skipAssert(assert, 5);
|
skipAssert(assert, 5);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QUnit.test('should accept falsey arguments', function(assert) {
|
||||||
|
assert.expect(1);
|
||||||
|
|
||||||
|
var expected = lodashStable.map(falsey, alwaysTrue);
|
||||||
|
|
||||||
|
var actual = lodashStable.map(falsey, function(value, index) {
|
||||||
|
try {
|
||||||
|
var result = index ? _(value).shift() : _().shift();
|
||||||
|
return result === undefined;
|
||||||
|
} catch (e) {}
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(actual, expected);
|
||||||
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
@@ -24401,6 +24446,21 @@
|
|||||||
skipAssert(assert, 2);
|
skipAssert(assert, 2);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QUnit.test('should accept falsey arguments', function(assert) {
|
||||||
|
assert.expect(1);
|
||||||
|
|
||||||
|
var expected = lodashStable.map(falsey, alwaysTrue);
|
||||||
|
|
||||||
|
var actual = lodashStable.map(falsey, function(value, index) {
|
||||||
|
try {
|
||||||
|
var result = index ? _(value).sort().value() : _().sort().value();
|
||||||
|
return lodashStable.eq(result, value);
|
||||||
|
} catch (e) {}
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(actual, expected);
|
||||||
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
@@ -24427,6 +24487,21 @@
|
|||||||
skipAssert(assert, 5);
|
skipAssert(assert, 5);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QUnit.test('should accept falsey arguments', function(assert) {
|
||||||
|
assert.expect(1);
|
||||||
|
|
||||||
|
var expected = lodashStable.map(falsey, alwaysTrue);
|
||||||
|
|
||||||
|
var actual = lodashStable.map(falsey, function(value, index) {
|
||||||
|
try {
|
||||||
|
var result = index ? _(value).splice(0, 1).value() : _().splice(0, 1).value();
|
||||||
|
return lodashStable.isEqual(result, []);
|
||||||
|
} catch (e) {}
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(actual, expected);
|
||||||
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
@@ -24449,6 +24524,21 @@
|
|||||||
skipAssert(assert, 2);
|
skipAssert(assert, 2);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QUnit.test('should accept falsey arguments', function(assert) {
|
||||||
|
assert.expect(1);
|
||||||
|
|
||||||
|
var expected = lodashStable.map(falsey, alwaysTrue);
|
||||||
|
|
||||||
|
var actual = lodashStable.map(falsey, function(value, index) {
|
||||||
|
try {
|
||||||
|
var result = index ? _(value).unshift(1).value() : _().unshift(1).value();
|
||||||
|
return lodashStable.eq(result, value);
|
||||||
|
} catch (e) {}
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(actual, expected);
|
||||||
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|||||||
Reference in New Issue
Block a user