Skip to content

Commit 0c0694e

Browse files
committed
add support for array paths.
see: skratchdot/object-path-get#2
1 parent 8567c15 commit 0c0694e

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ var setPath = function (obj, path, value, delimiter) {
77
obj = {};
88
}
99
if (typeof path === 'string') {
10-
arr = path.split(delimiter || '.');
10+
path = path.split(delimiter || '.');
11+
}
12+
if (Array.isArray(path) && path.length > 0) {
13+
arr = path;
1114
key = arr[0];
1215
if (arr.length > 1) {
1316
arr.shift();
14-
obj[key] = setPath(obj[key], arr.join(delimiter || '.'), value, delimiter);
17+
obj[key] = setPath(obj[key], arr, value, delimiter);
1518
} else {
1619
obj[key] = value;
1720
}

test.js

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,28 @@ describe('object-path-set', function () {
4747
expect(obj.nested.foo).to.equal(newValue);
4848
});
4949
it('should covert things to objects', function () {
50-
expect(setPath(1234, 'a', 42)).to.deep.equal({a: 42});
51-
expect(setPath(null, 'a', 42)).to.deep.equal({a: 42});
52-
expect(setPath(true, 'a', 42)).to.deep.equal({a: 42});
53-
expect(setPath({a: 123}, 'a.b', 42)).to.deep.equal({a: {b: 42}});
54-
expect(setPath(null, 'a.b.c.d', null)).to.deep.equal({a:{b:{c:{d:null}}}});
50+
expect(setPath(1234, 'a', 42)).to.eql({a: 42});
51+
expect(setPath(null, 'a', 42)).to.eql({a: 42});
52+
expect(setPath(true, 'a', 42)).to.eql({a: 42});
53+
expect(setPath({a: 123}, 'a.b', 42)).to.eql({a: {b: 42}});
54+
expect(setPath(null, 'a.b.c.d', null)).to.eql({a:{b:{c:{d:null}}}});
5555
});
5656
it('should be able to use custom delimiters', function () {
57-
expect(setPath({}, 'a|b|c|d', 42)).to.deep.equal({'a|b|c|d': 42});
58-
expect(setPath({}, 'a|b|c|d', 42, '|')).to.deep.equal({a:{b:{c:{d:42}}}});
59-
expect(setPath({}, 'a.b.c.d', 42, '|')).to.deep.equal({'a.b.c.d': 42});
57+
expect(setPath({}, 'a|b|c|d', 42)).to.eql({'a|b|c|d': 42});
58+
expect(setPath({}, 'a|b|c|d', 42, '|')).to.eql({a:{b:{c:{d:42}}}});
59+
expect(setPath({}, 'a.b.c.d', 42, '|')).to.eql({'a.b.c.d': 42});
6060
});
61-
it('should set the correct values', function () {
62-
expect(setPath({}, 'a.b', 42)).to.deep.equal({a:{b: 42}});
63-
expect(setPath({}, 'a.b', undefined)).to.deep.equal({a:{b: undefined}});
64-
expect(setPath({}, 'a.b', true)).to.deep.equal({a:{b: true}});
65-
expect(setPath({}, 'a.b', 'wow')).to.deep.equal({a:{b: 'wow'}});
61+
it('should set the correct values', function () {
62+
expect(setPath({}, 'a.b', 42)).to.eql({a:{b: 42}});
63+
expect(setPath({}, 'a.b', undefined)).to.eql({a:{b: undefined}});
64+
expect(setPath({}, 'a.b', true)).to.eql({a:{b: true}});
65+
expect(setPath({}, 'a.b', 'wow')).to.eql({a:{b: 'wow'}});
66+
});
67+
it('should handle arrays as paths', function () {
68+
expect(setPath({}, ['a', 'b'], 42)).to.eql({a:{b: 42}});
69+
expect(setPath({}, ['a', 'b'], undefined)).to.eql({a:{b: undefined}});
70+
expect(setPath({}, ['a', 'b'], true)).to.eql({a:{b: true}});
71+
expect(setPath({}, ['a', 'b'], 'wow')).to.eql({a:{b: 'wow'}});
6672
});
6773
it('should be able to be called multiple times', function () {
6874
obj = {};
@@ -71,14 +77,22 @@ describe('object-path-set', function () {
7177
obj = setPath(obj, 'c.d', {});
7278
obj = setPath(obj, 'c.d.e', {});
7379
obj = setPath(obj, 'c.d.f', 'foo');
74-
expect(obj).to.deep.equal({a: 42, b: true, c:{d:{e:{}, f:'foo'}}});
80+
expect(obj).to.eql({a: 42, b: true, c:{d:{e:{}, f:'foo'}}});
7581
});
76-
it('should return the default object when key is not a string', function () {
82+
it('should return the default object when key is not a string or array', function () {
7783
var defaultValue = Math.random();
78-
expect(setPath(obj, {}, defaultValue)).to.deep.equal(obj);
79-
expect(setPath(obj, [], defaultValue)).to.deep.equal(obj);
80-
expect(setPath(obj, null, defaultValue)).to.deep.equal(obj);
81-
expect(setPath(obj, 11, defaultValue)).to.deep.equal(obj);
82-
expect(setPath(obj, undefined, defaultValue)).to.deep.equal(obj);
84+
[{}, null, 42, undefined, true].forEach(function (path) {
85+
expect(setPath(getDefaultObject(), path, defaultValue)).to.eql(getDefaultObject());
86+
});
8387
});
88+
it('should return the default object when key is an empty array', function () {
89+
var defaultValue = Math.random();
90+
expect(setPath(obj, [], defaultValue)).to.eql(getDefaultObject());
91+
});
92+
it('should allow empty strings as a path', function () {
93+
var defaultValue = Math.random();
94+
var obj2 = getDefaultObject();
95+
obj2[''] = defaultValue;
96+
expect(setPath(obj, '', defaultValue)).to.eql(obj2);
97+
});
8498
});

0 commit comments

Comments
 (0)