Skip to content
New issue

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

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

Already on GitHub? # to your account

Return object from the .set() method #42

Merged
merged 4 commits into from
Jul 24, 2017

Conversation

kjellmorten
Copy link
Contributor

Just a small change to return the object from the set method. I find it convenient in some cases, and it does not add any significant overhead.

Feel free to merge if this makes sense to you. Tests and readme are updated accordingly.

@sindresorhus
Copy link
Owner

I find it convenient in some cases

Do you have an example of that? I'm trying to think of when it would be useful.

readme.md Outdated
@@ -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.

@kjellmorten
Copy link
Contributor Author

@sindresorhus As an example, I have a function that takes a value, does something to it, and returns it at a path in a new object. (The path is provided at run-time, so regular JS object notation is not an option.)

When set does not return the object, I have to write:

const object = {}
set(object, path, value)
return object

With this PR I could simply write:

return set({}, path, value)

It's not a big issue – just for convenience. :)

@sindresorhus
Copy link
Owner

Makes sense. Can you use something like that in the readme example too? const foo = obj => dotProp.set(obj, 'unicorn', '🦄'); Makes it easier to understand the use-case of it.

@kjellmorten
Copy link
Contributor Author

Sure. I had an example of the return value, but it wasn't clear enough. Changed the input to an empty object, so it is clearer what the point of it is.

Sorry I didn't return a unicorn.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants