-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Clamp #1564
Clamp #1564
Conversation
Should we throw if |
good idea |
done |
* R.clamp(1, 10, 4) // => 4 | ||
*/ | ||
module.exports = _curry3(function clamp(min, max, value) { | ||
if (min > max) throw new Error('min must be greater than max in clamp(min, max, value)') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should read min must not be greater than max. ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
hmm. not sure whats going on here with that error... |
* R.clamp(1, 10, 4) // => 4 | ||
*/ | ||
module.exports = _curry3(function clamp(min, max, value) { | ||
if (min > max) throw new Error('min must not be greater than max in clamp(min, max, value)') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In order to appease the linter, you'll need to write this like so:
if (...) {
...
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. done
ok @davidchambers, added some tests for letters as well. I'm not sure what you want to do about |
@ccorcos, see test/shared/eq.js. ;) |
Hmm. I'm confused. Did I fork off of 0.14?! https://github.com/ccorcos/ramda/blob/master/package.json#L27 I'll have to rebase when I get home. |
You'll need to update your fork's master branch: $ git checkout master
$ git pull upstream master
$ git push origin master |
awesome. done. |
var _curry3 = require('./internal/_curry3'); | ||
|
||
/** | ||
* Clamps a number within a range. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we mention that this function applies to any type which supports ordering?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure: how about this?
Clamps any type that supports ordering within a range.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I agree.
Restricts a number to be within a range.
Also works for other ordered types such as Strings and Dates. (see Type::Ord)
That last bit it just a bit of fantasy, but something I'd love to be able to support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Alright, so what do you guys think? My first Ramda contribution? :) |
Looks good, aside from the two outstanding issues:
Once you've addressed these minor issues I'll happily merge this pull request. :) |
🌿, once those minor changes are made. |
Boom 🌿 |
Thank you very much! |
🌳 Thanks for persevering. :) |
Of course 👍 I look forward to contributing more now that I got the first one out of the way. |
About const clamp = curry((min, max, value) => Math.max(min, Math.min(max, value))); Also if you add const lerp = curry((start, end, alpha) => start*(1-alpha)+end*alpha It is linear interpolation https://en.wikipedia.org/wiki/Linear_interpolation http://codepen.io/ma77os/pen/KGIEh |
Perhaps I'm misunderstanding. The current implementation handles that case, but should work with any ordered type.
While that is certainly a useful function, my first thought is that it belongs in The Cookbook instead. I certainly would not object to hearing arguments to the contrary though if you disagree. |
#1563