Skip to content

Refactor into generic function#1

Closed
sebastiansandqvist wants to merge 3 commits intosindresorhus:masterfrom
sebastiansandqvist:master
Closed

Refactor into generic function#1
sebastiansandqvist wants to merge 3 commits intosindresorhus:masterfrom
sebastiansandqvist:master

Conversation

@sebastiansandqvist
Copy link
Contributor

The two functions module.exports and module.exports.val were nearly identical--good candidates for a refactor. The boolean val for "value" in the stable function should be true if a value is desired for the output and false if a boolean is desired.

index.js Outdated
Copy link
Owner

Choose a reason for hiding this comment

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

If you pass val as the first argument, you could bind it:

module.exports = stable.bind(false);
module.exports.val = stable.bind(true);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's really elegant--however, I cannot get tests to pass that way. It errors out on stableFn.val() saying "false == true"

Copy link
Owner

Choose a reason for hiding this comment

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

Oops, I forgot a parameter:

module.exports = stable.bind(null, false);
module.exports.val = stable.bind(null, true);

@sindresorhus
Copy link
Owner

Excellent. Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants