map-number
Version:
processing/p5.js map like function, including floating point numbers support
135 lines (81 loc) • 4.66 kB
Markdown
# map-number
[](https://circleci.com/gh/manferlo81/map-number) [](https://greenkeeper.io/) [](https://www.npmjs.com/package/map-number) [](https://www.jsdelivr.com/package/npm/map-number) [](https://david-dm.org/manferlo81/map-number) [](https://david-dm.org/manferlo81/map-number?type=dev) [](https://packagephobia.now.sh/result?p=map-number) [](https://bundlephobia.com/result?p=map-number) [](https://github.com/microsoft/typescript) [](https://codecov.io/gh/manferlo81/map-number) [](https://snyk.io/test/github/manferlo81/map-number?targetFile=package.json) [](LICENSE)
[processing](https://processing.org/reference/map_.html)/[p5.js](http://p5js.org/reference/#/p5/map) map like function, including floating point numbers support
> :warning: this `map` function has nothing to do with `Array.prototype.map` method.
## Install
```bash
npm i map-number
```
## CDN
### jsDelivr
*[www.jsdelivr.com](https://www.jsdelivr.com)*
```html
<script src="https://cdn.jsdelivr.net/npm/map-number/dist/map.umd.js"></script>
```
##### for production
```html
<script src="https://cdn.jsdelivr.net/npm/map-number/dist/map.umd.min.js"></script>
```
*[more options...](https://www.jsdelivr.com/package/npm/map-number)*
#### unpkg
```html
<script src="https://unpkg.com/map-number/dist/map.umd.js"></script>
```
*[more options...](https://unpkg.com/map-number/)*
## Usage
### Node.js
```javascript
const mapNum = require("map-number");
const y = mapNum.map(Math.sin(angle), -1, 1, 100, 0);
```
### Browser
*after the* `script` *tag has been added,* `mapNum` *will be available globally.*
```javascript
const y = mapNum.map(Math.sin(angle), -1, 1, 100, 0);
```
## API
### map
*maps a number in a range to a different range, returning a floting point number, it may include number outside the given output range.*
> *this is the core function and all other map function variants depend on this it.*
###### syntax
```typescript
function map(num: number, inMin: number, inMax: number, outMin: number, outMax: number): number;
```
### floor
*maps a number in a range to a different range, returning a number truncated to the inmediate previous integer number, it may include number outside the given output range.*
###### syntax
```typescript
function floor(num: number, inMin: number, inMax: number, outMin: number, outMax: number): number;
```
### round
*maps a number in a range to a different range, returning a number rounded to the closest integer number, it may include number outside the given output range.*
###### syntax
```typescript
function round(num: number, inMin: number, inMax: number, outMin: number, outMax: number): number;
```
### limit
*maps a number in a range to a different range, returning a floting point number, the result will bounded to the given output range.*
###### syntax
```typescript
function limit(num: number, inMin: number, inMax: number, outMin: number, outMax: number): number;
```
### create
*creates a single argument function implementing the given [`map`](#map), [`floor`](#floor), [`round`](#round) or [`limit`](#limit) function, useful when you need to map values multiple times within the same range, [see example](#example)*
###### syntax
```typescript
function create(func: MapFunction, inMin: number, inMax: number, outMin: number, outMax: number): (num: number) => number;
```
###### example
```javascript
import { map, create } from "map-number";
const myMap = create(map, -1, 1, 100, 0);
myMap(Math.sin(angle1));
myMap(Math.sin(angle2));
// ... is equivalent to...
map(Math.sin(angle1), -1, 1, 100, 0);
map(Math.sin(angle2), -1, 1, 100, 0);
```
### wrap
*an alias for [create](#create) method*
## License
[MIT](LICENSE) © Manuel Fernández