diff --git a/lodash.js b/lodash.js index 928dff9f5..22f88ec2f 100644 --- a/lodash.js +++ b/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) { var func = arrayProto[methodName], chainName = /^(?:push|sort|unshift)$/.test(methodName) ? 'tap' : 'thru', + mutates = methodName != 'sort', retUnwrapped = /^(?:pop|shift)$/.test(methodName); lodash.prototype[methodName] = function() { var args = arguments; 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) { + if ((mutates ? !isArray(value) : (value == null))) { + value = []; + } return func.apply(value, args); }); }; diff --git a/test/test.js b/test/test.js index 242007793..ec4425850 100644 --- a/test/test.js +++ b/test/test.js @@ -24331,6 +24331,21 @@ 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); } }); + + 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); } }); + + 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); } }); + + 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); } }); + + 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); } }); + + 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); + }); }()); /*--------------------------------------------------------------------------*/