Skip to content

Commit 77dae23

Browse files
authored
Merge pull request #2843 from jgonggrijp/array-method-null-check
2 parents 35579bc + a63d54c commit 77dae23

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

modules/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,8 +1653,12 @@ each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(
16531653
var method = ArrayProto[name];
16541654
_.prototype[name] = function() {
16551655
var obj = this._wrapped;
1656-
method.apply(obj, arguments);
1657-
if ((name === 'shift' || name === 'splice') && obj.length === 0) delete obj[0];
1656+
if (obj != null) {
1657+
method.apply(obj, arguments);
1658+
if ((name === 'shift' || name === 'splice') && obj.length === 0) {
1659+
delete obj[0];
1660+
}
1661+
}
16581662
return chainResult(this, obj);
16591663
};
16601664
});
@@ -1663,7 +1667,9 @@ each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift'], function(
16631667
each(['concat', 'join', 'slice'], function(name) {
16641668
var method = ArrayProto[name];
16651669
_.prototype[name] = function() {
1666-
return chainResult(this, method.apply(this._wrapped, arguments));
1670+
var obj = this._wrapped;
1671+
if (obj != null) obj = method.apply(obj, arguments);
1672+
return chainResult(this, obj);
16671673
};
16681674
});
16691675

test/chaining.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,14 @@
9696
assert.strictEqual('' + wrapped, '512');
9797
});
9898

99+
QUnit.test('wrapper methods handle undefined and null', function(assert) {
100+
var w1 = _(), w2 = _(null);
101+
_.each([w1, w2], function(wrapped) {
102+
assert.equal(wrapped.extend({a: 1}), void 0);
103+
assert.equal(wrapped.first(), void 0);
104+
assert.equal(wrapped.push(1), void 0);
105+
assert.equal(wrapped.concat([1]), void 0);
106+
});
107+
});
108+
99109
}());

underscore.js

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

underscore.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)