Skip to content

Commit

Permalink
Merge 5578a40 into 99dc4bb
Browse files Browse the repository at this point in the history
  • Loading branch information
adierkens committed Mar 3, 2017
2 parents 99dc4bb + 5578a40 commit 1d74d5d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/timm.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ function doSetIn<T: ArrayOrObject>(
if (idx === path.length - 1) {
newValue = val;
} else {
const nestedObj = isObject(obj) ? obj[key] : {};
const nestedObj = isObject(obj) ? obj[key] : typeof path[idx + 1] === 'number' ? [] : {};
newValue = doSetIn(nestedObj, path, val, idx + 1);
}
return set(obj, key, newValue);
Expand Down
19 changes: 19 additions & 0 deletions test/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,25 @@ test('setIn: should create nested arrays for unknown paths with positive integer
t.is(obj2.unknown[0].long[1].path, 'value');
});

test('setIn: should create nested arrays for undefined objects', (t) => {
const obj2 = timm.setIn(undefined, ['unknown', 0, 'path'], 'value');
t.truthy(Array.isArray(obj2.unknown));
t.is(obj2.unknown[0].path, 'value');
});

test('setIn: should create nested arrays for unknown paths', (t) => {
const obj2 = timm.setIn(OBJ, ['unknown', 0, 1, 'path'], 'value');
t.truthy(Array.isArray(obj2.unknown));
t.truthy(Array.isArray(obj2.unknown[0]));
t.is(obj2.unknown[0][1].path, 'value');
});

test('setIn: should create nested object in arrays for unknown paths', (t) => {
const obj2 = timm.setIn(undefined, ['x', 0, 'y', 'z'], 'value');
t.truthy(Array.isArray(obj2.x));
t.is(obj2.x[0].y.z, 'value');
});

test('setIn: should create nested arrays for unknown paths with negative segments', (t) => {
const obj2 = timm.setIn(OBJ, ['unknown', -17, 'path'], 3);
t.truthy(Array.isArray(obj2.unknown));
Expand Down

0 comments on commit 1d74d5d

Please sign in to comment.