map-number
⚠️ this
map
function has nothing to do withArray.prototype.map
method.
Install
npm i map-number
CDN
jsDelivr
for production
for production you may want to replace the "latest" version for a specific one.
unpkg
for production
for production you may want to replace the "latest" version for a specific one.
Usage
Node.js
const map = ;const result = ;
using javascript modules...
;const result = ;
Browser
After the script
tag has been added, mapNum
will be available globally.
const result = mapNum;
API
map
Maps a number within a given 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.
syntax
;
floor
Maps a number within a given 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
;
ceil
Maps a number within a given 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
;
round
Maps a number within a given 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
;
If you need to round to a specific number of decimal places, you can use the
transform
method and write your own round function.
limit
alias: clamp
Maps a number within a given range to a different range, returning a floting point number. The result will be limited (clamped) to the given output range.
syntax
;
compile
alias: wrap
, create
Creates a single argument function implementing the given map
, floor
, ceil
, round
, limit
, clamp
or user created function. Useful when you need to map values multiple times within the same range, see example below.
syntax
; ;
example
; const myMap = ; ;;; // ... is equivalent to... ;;;
transform
Creates a map function where the result of the given function is transformed to a different value. This method is used internally to create the floor
, ceil
and round
methods.
syntax
;
example
; const plusOne = ; ; // => 41