Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
map-number
Advanced tools
processing/p5.js map like function, including floating point numbers support
processing/p5.js map like function, including floating point numbers support
:warning: this
map
function has nothing to do withArray.prototype.map
method.
npm install map-number
yarn add map-number
pnpm add map-number
<script src="https://cdn.jsdelivr.net/npm/map-number@latest/dist/umd/map.js"></script>
for production
<script src="https://cdn.jsdelivr.net/npm/map-number@latest/dist/umd/map.min.js"></script>
for production you may want to replace the "latest" version for a specific one.
<script src="https://unpkg.com/map-number@latest/dist/umd/map.js"></script>
for production
<script src="https://unpkg.com/map-number@latest/dist/umd/map.min.js"></script>
for production you may want to replace the "latest" version for a specific one.
const { map } = require('map-number');
const result = map(Math.sin(angle), -1, 1, 100, 0);
using javascript modules...
import { map } from 'map-number';
const result = map(Math.sin(angle), -1, 1, 100, 0);
After the script
tag has been added, mapNum
will be available globally.
const result = mapNum.map(Math.sin(angle), -1, 1, 100, 0);
map
Maps a number within a given range to a different range, returning a floating 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.
function map(
input: number,
inputMin: number,
inputMax: number,
outputMin: number,
outputMax: number,
): number;
map(4, 0, 10, 0, 100); // => returns 40
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
function floor(
input: number,
inputMin: number,
inputMax: number,
outputMin: number,
outputMax: number,
): number;
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
function ceil(
input: number,
inputMin: number,
inputMax: number,
outputMin: number,
outputMax: number,
): number;
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
function round(
input: number,
inputMin: number,
inputMax: number,
outputMin: number,
outputMax: number,
): number;
If you need to round to a specific number of decimal places, you can use the
transformed
method and write your own round function.
limit
alias: clamp
Maps a number within a given range to a different range, returning a floating point number. The result will be limited (clamped) to the given output range.
syntax
function limit(
input: number,
inputMin: number,
inputMax: number,
outputMin: number,
outputMax: number,
): number;
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
function compile<O, I>(
map: MapFunction<O, I>,
inputMin: number,
inputMax: number,
outputMin: number,
outputMax: number,
): CompiledMapFunction<O, I>;
See MapFunction
and CompiledMapFunction
.
example
import { map, compile } from "map-number";
const myMap = compile(map, -1, 1, 100, 0);
myMap(-0.2);
myMap(0.33);
myMap(0.51);
// ... is equivalent to...
map(-0.2, -1, 1, 100, 0);
map(0.33, -1, 1, 100, 0);
map(0.51, -1, 1, 100, 0);
transformed
alias: 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
function transformed<O = number, M = number, I = number>(
map: MapFunction<M, I>,
transform: TransformFunction<M, O>,
): MapFunction<O, I>;
See MapFunction
and TransformFunction
.
example
import { transform, map } from "map-number";
const plusOne = transform(
map,
(value) => value + 1,
);
plusOne(0.4, 0, 1, 0, 100); // => 41 instead of 40
MapFunction
type MapFunction<O = number, I = number> = (value: I, inMin: number, inMax: number, outMin: number, outMax: number) => O;
CompiledMapFunction
type CompiledMapFunction<O = number, I = number> = (value: I) => O;
TransformFunction
type TransformFunction<I = number, O = number> = (value: I) => O;
MapNumberFunction
type MapNumberFunction = MapFunction<number, number>;
See MapFunction
CompiledMapNumberFunction
type CompiledMapNumberFunction = CompiledMapFunction<number, number>;
MIT © 2019-2024 Manuel Fernández
FAQs
processing/p5.js map like function, including floating point numbers support
The npm package map-number receives a total of 608 weekly downloads. As such, map-number popularity was classified as not popular.
We found that map-number demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.