https://github.com/pgilad/fit-to-range
Fit a number to a given range
https://github.com/pgilad/fit-to-range
Last synced: about 1 year ago
JSON representation
Fit a number to a given range
- Host: GitHub
- URL: https://github.com/pgilad/fit-to-range
- Owner: pgilad
- License: mit
- Created: 2015-08-23T13:41:30.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2019-02-13T09:29:39.000Z (over 7 years ago)
- Last Synced: 2025-02-05T16:15:33.688Z (over 1 year ago)
- Language: JavaScript
- Size: 4.88 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fit-to-range
> Fit a number to a given range
[](https://npmjs.org/package/fit-to-range)
[](https://npmjs.org/package/fit-to-range)
[](https://travis-ci.org/pgilad/fit-to-range)
Fits a number to a given range. The (simple) algorithm is:
1. If number is equal or lower than lower bound - return lower bound.
2. If number is equal or higher than upper bound - return upper bound.
3. Return number.
A valid number is any regular integer/float, +-`Infinity`. `NaN` is invalid.
## Installation
```sh
npm install --save fit-to-range
```
## Usage
`fitToRange(num, lowerBound, upperBound)`
```js
var fitToRange = require('fit-to-range');
var fitted;
fitted = fitToRange(5, 10, 20);
// => 10
fitted = fitToRange(25, 10, 20);
// => 20
fitted = fitToRange(0, -1, 1);
// => 0
fitted = fitToRange(1, -Infinity, 0);
// => 0
fitted = fitToRange(10.1, 0, Infinity);
// => 10.1
```
## License
MIT © [Gilad Peleg](https://www.giladpeleg.com)