Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return object from the .set() method #42

Merged
merged 4 commits into from Jul 24, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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: 5 additions & 1 deletion readme.md
Expand Up @@ -34,6 +34,10 @@ dotProp.set(obj, 'foo.bar', 'b');
console.log(obj);
//=> {foo: {bar: 'b'}}

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

dotProp.set(obj, 'foo.baz', 'x');
console.log(obj);
//=> {foo: {bar: 'b', baz: 'x'}}
Expand All @@ -59,7 +63,7 @@ console.log(obj);

### get(obj, path, [defaultValue])

### set(obj, path, value)
### obj = set(obj, path, value)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to write it in text. Something like:

Returns the object.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! See new commit.


### has(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