map-number
Version:
processing/p5.js map like function, including floating point numbers support
145 lines (87 loc) • 5.01 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. The result is not limited to the the given output range.*
> *This is the core function and all other map function variants depend on 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 rounded **down** to the **previous** integer number. The result is not limited to the the given output range.*
###### syntax
```typescript
function floor(num: number, inMin: number, inMax: number, outMin: number, outMax: number): number;
```
### ceil
*Maps a number in a range to a different range, returning a number rounded **up** to the **next** integer number. The result is not blimitedto the the given output range.*
###### syntax
```typescript
function ceil(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. The result is not blimitedto the 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 be 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), [`ceil`](#ceil), [`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