Skip to content

Commit

Permalink
Return object from the .set() method (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellmorten authored and sindresorhus committed Jul 24, 2017
1 parent 49f0809 commit df49d33
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
5 changes: 4 additions & 1 deletion index.js
Expand Up @@ -53,9 +53,10 @@ module.exports = {

set(obj, path, value) {
if (!isObj(obj) || typeof path !== 'string') {
return;
return obj;
}

const root = obj;
const pathArr = getPathSegments(path);

for (let i = 0; i < pathArr.length; i++) {
Expand All @@ -71,6 +72,8 @@ module.exports = {

obj = obj[p];
}

return root;
},

delete(obj, path) {
Expand Down
6 changes: 6 additions & 0 deletions readme.md
Expand Up @@ -34,6 +34,10 @@ dotProp.set(obj, 'foo.bar', 'b');
console.log(obj);
//=> {foo: {bar: 'b'}}

const foo = dotProp.set({}, 'foo.bar', 'c');
console.log(foo);
//=> {foo: {bar: 'c'}}

dotProp.set(obj, 'foo.baz', 'x');
console.log(obj);
//=> {foo: {bar: 'b', baz: 'x'}}
Expand Down Expand Up @@ -61,6 +65,8 @@ console.log(obj);

### set(obj, path, value)

Returns the object.

### has(obj, path)

### delete(obj, path)
Expand Down
8 changes: 7 additions & 1 deletion test.js
Expand Up @@ -56,8 +56,9 @@ test('set', t => {
const func = () => 'test';
let f1 = {};

m.set(f1, 'foo', 2);
const o1 = m.set(f1, 'foo', 2);
t.is(f1.foo, 2);
t.is(o1, f1);

f1 = {foo: {bar: 1}};
m.set(f1, 'foo.bar', 2);
Expand Down Expand Up @@ -105,6 +106,11 @@ test('set', t => {

m.set(f1, 'fo\\.ob\\.ar.baz', true);
t.is(f1['fo.ob.ar'].baz, true);

const f4 = 'noobject';
const o4 = m.set(f4, 'foo.bar', 2);
t.is(f4, 'noobject');
t.is(o4, f4);
});

test('delete', t => {
Expand Down

0 comments on commit df49d33

Please sign in to comment.