Skip to content

Commit

Permalink
feat: compile & clamp alias
Browse files Browse the repository at this point in the history
* renamed "create" method to "compile"
* declared "create" as alias for "compile" method
* added "clamp" as alias for "limit" method
  • Loading branch information
manferlo81 committed Nov 27, 2019
1 parent 15fb8fe commit 364987b
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 45 deletions.
11 changes: 10 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@
"error",
"always-multiline"
],
"object-shorthand": "error"
"object-shorthand": "error",
"no-useless-rename": "error",
"no-multiple-empty-lines": [
"error",
{
"max": 1,
"maxBOF": 0,
"maxEOF": 0
}
]
}
}
43 changes: 24 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ npm i map-number
<script src="https://cdn.jsdelivr.net/npm/map-number@latest/dist/map.umd.min.js"></script>
```

> *for production you may want to replace the "latest" version for a specific one.*
*[more options...](https://www.jsdelivr.com/package/npm/map-number?version=latest)*

### unpkg
Expand All @@ -38,30 +40,32 @@ npm i map-number
<script src="https://unpkg.com/map-number@latest/dist/map.umd.min.js"></script>
```

> *for production you may want to replace the "latest" version for a specific one.*
*[more options...](https://unpkg.com/map-number@latest/)*

## Usage

### Node.js

```javascript
const mapNum = require("map-number");
const y = mapNum.map(Math.sin(angle), -1, 1, 100, 0);
const { map } = require("map-number");
const result = 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);
const result = 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.*
*Maps a number in a range to a different range, returning a floting point number. The result **WILL NOT** be limited (clamped) to the the given output range.*

> *This is the core function and all other map function variants depend on it.*
Expand All @@ -73,7 +77,7 @@ function map(num: number, inMin: number, inMax: number, outMin: number, outMax:

### 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.*
*Maps a number in a range to a different range, returning a number rounded **down** to the **previous** integer number. The result **WILL NOT** be limited (clamped) to the the given output range.*

***syntax***

Expand All @@ -83,7 +87,7 @@ function floor(num: number, inMin: number, inMax: number, outMin: number, outMax

### 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.*
*Maps a number in a range to a different range, returning a number rounded **up** to the **next** integer number. The result **WILL NOT** be limited (clamped) to the the given output range.*

***syntax***

Expand All @@ -93,7 +97,7 @@ function ceil(num: number, inMin: number, inMax: number, outMin: number, outMax:

### 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.*
*Maps a number in a range to a different range, returning a number **rounded** to the **closest** integer number. The result **WILL NOT** be limited (clamped) to the the given output range.*

***syntax***

Expand All @@ -103,44 +107,45 @@ function round(num: number, inMin: number, inMax: number, outMin: number, outMax

### 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.*
#### alias: `clamp`

*Maps a number in a range to a different range, returning a floting point number. The result will be limited (clamped) to the given output range.*

***syntax***

```typescript
function limit(num: number, inMin: number, inMax: number, outMin: number, outMax: number): number;
```

### wrap
### compile

*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).*
#### alias: `wrap`, `create`

*Creates a single argument function implementing the given [`map`](#map), [`floor`](#floor), [`ceil`](#ceil), [`round`](#round), [`limit`](#limit) or [`clamp`](#limit) function. Useful when you need to map values multiple times within the same range, see example below.*

***syntax***

```typescript
function wrap(func: MapFunction, inMin: number, inMax: number, outMin: number, outMax: number): (num: number) => number;
function compile(func: MapNumberFunction, inMin: number, inMax: number, outMin: number, outMax: number): (num: number) => number;
```

***example***

```javascript
import { map, wrap } from "map-number";
import { map, round, compile } from "map-number";

const myMap = wrap(map, -1, 1, 100, 0);
const myMap = compile(map, -1, 1, 100, 0);
const myRound = compile(round, -1, 1, 100, 0);

myMap(-0.2);
myMap(0.33);
myRound(0.33);

// ... is equivalent to...

map(-0.2, -1, 1, 100, 0);
map(0.33, -1, 1, 100, 0);
round(0.33, -1, 1, 100, 0);
```

### create

*An alias for [wrap](#wrap) method, deprecated in version* `1.2.0`*, use [wrap](#wrap) instead.*

## License

[MIT](LICENSE) &copy; [Manuel Fernández](https://github.com/manferlo81)
14 changes: 14 additions & 0 deletions __test__/alias.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { compile, create, wrap, limit, clamp } from '../src'

describe('alias', () => {

test('should export compile alias', () => {
expect(create).toBe(compile)
expect(wrap).toBe(compile)
})

test('should export limit alias', () => {
expect(clamp).toBe(limit)
})

})
4 changes: 2 additions & 2 deletions __test__/wrap.test.ts → __test__/compile.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { map, wrap } from '../src'
import { map, compile } from '../src'

describe('wrap method', () => {

test('should return the correct output', () => {

const boundaries: [number, number, number, number] = [2, 5, 20, 50]
const myMap = wrap(map, ...boundaries)
const myMap = compile(map, ...boundaries)

const nums = [3.3, 0.01, 10]

Expand Down
3 changes: 2 additions & 1 deletion src/ceil.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import map from './map'
import { MapNumberFunction } from './types'

function ceil(num: number, inMin: number, inMax: number, outMin: number, outMax: number): number {
return Math.ceil(
map(num, inMin, inMax, outMin, outMax),
)
}

export default ceil
export default ceil as MapNumberFunction
7 changes: 7 additions & 0 deletions src/compile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { CompiledMapNumberFunction, MapNumberFunction } from './types'

function compile(func: MapNumberFunction, inMin: number, inMax: number, outMin: number, outMax: number): CompiledMapNumberFunction {
return (num: number): number => func(num, inMin, inMax, outMin, outMax)
}

export default compile
8 changes: 0 additions & 8 deletions src/create.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/floor.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import map from './map'
import { MapNumberFunction } from './types'

function floor(num: number, inMin: number, inMax: number, outMin: number, outMax: number): number {
return Math.floor(
map(num, inMin, inMax, outMin, outMax),
)
}

export default floor
export default floor as MapNumberFunction
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export { default as map } from './map'
export { default as floor } from './floor'
export { default as ceil } from './ceil'
export { default as round } from './round'
export { default as limit } from './limit'
export { default as create, default as wrap } from './create'
export { default as limit, default as clamp } from './limit'
export { default as compile, default as create, default as wrap } from './compile'
export { MapNumberFunction, CompiledMapNumberFunction } from './types'
13 changes: 4 additions & 9 deletions src/limit.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import map from './map'
import { max, min } from './math'
import { MapNumberFunction } from './types'

function limit(num: number, inMin: number, inMax: number, outMin: number, outMax: number): number {
return max(
min(outMin, outMax),
min(
outMin,
outMax,
),
min(
max(
outMin,
outMax,
),
max(outMin, outMax),
map(num, inMin, inMax, outMin, outMax),
),
)
}

export default limit
export default limit as MapNumberFunction
4 changes: 3 additions & 1 deletion src/map.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { MapNumberFunction } from './types'

function map(num: number, inMin: number, inMax: number, outMin: number, outMax: number): number {
return (num - inMin) * (outMax - outMin) / (inMax - inMin) + outMin
}

export default map
export default map as MapNumberFunction
3 changes: 2 additions & 1 deletion src/round.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import map from './map'
import { MapNumberFunction } from './types'

function round(num: number, inMin: number, inMax: number, outMin: number, outMax: number): number {
return Math.round(
map(num, inMin, inMax, outMin, outMax),
)
}

export default round
export default round as MapNumberFunction
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type MapNumberFunction = (num: number, inMin: number, inMax: number, outMin: number, outMax: number) => number;
export type CompiledMapNumberFunction = (num: number) => number;

0 comments on commit 364987b

Please sign in to comment.