Permalink
Browse files

Fixing minor regression in assocPath (#2041)

  • Loading branch information...
1 parent d79974f commit 727411c3252a49b8e3263a74b53e21900e131260 @CrossEye CrossEye committed on GitHub Jan 6, 2017
Showing with 10 additions and 1 deletion.
  1. +2 −1 src/assocPath.js
  2. +8 −0 test/assocPath.js
View
@@ -3,6 +3,7 @@ var _has = require('./internal/_has');
var _isArray = require('./internal/_isArray');
var _isInteger = require('./internal/_isInteger');
var assoc = require('./assoc');
+var isNil = require('./isNil');
/**
@@ -35,7 +36,7 @@ module.exports = _curry3(function assocPath(path, val, obj) {
}
var idx = path[0];
if (path.length > 1) {
- var nextObj = _has(idx, obj) ? obj[idx] : _isInteger(path[1]) ? [] : {};
+ var nextObj = (!isNil(obj) && _has(idx, obj)) ? obj[idx] : _isInteger(path[1]) ? [] : {};
val = assocPath(Array.prototype.slice.call(path, 1), val, nextObj);
}
if (_isInteger(idx) && _isArray(obj)) {
View
@@ -41,4 +41,12 @@ describe('assocPath', function() {
eq(R.assocPath([], 3, {a: 1, b: 2}), 3);
});
+ it('replaces `undefined` with a new object', function() {
+ eq(assocPath(['foo', 'bar', 'baz'], 42, {foo: undefined}), {foo: {bar: {baz: 42}}});
+ });
+
+ it('replaces `null` with a new object', function() {
+ eq(assocPath(['foo', 'bar', 'baz'], 42, {foo: null}), {foo: {bar: {baz: 42}}});
+ });
+
});

0 comments on commit 727411c

Please sign in to comment.