Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
normalize-range
Advanced tools
Utility for normalizing a numeric range, with a wrapping function useful for polar coordinates
The normalize-range npm package is designed to normalize or map a given range of numbers to another range. This can be particularly useful in scenarios where you need to adjust values to fit within a certain scale, such as adjusting sound volume levels, scaling graphical elements, or normalizing data for machine learning purposes.
Normalizing values within a range
This feature allows you to take a value within an original range and normalize it to a new range. The example code demonstrates normalizing the value 5 from the range of 0-10 to the equivalent value in the range of 0-100.
const normalize = require('normalize-range');
let normalizedValue = normalize(5, 0, 10, 0, 100); // Normalizes 5 from a range of 0-10 to a range of 0-100
Part of the larger D3.js library, 'd3-scale' is a powerful tool for scaling numbers, especially within the context of visual data representation. It supports a wide variety of scales, including linear, logarithmic, and time-based. While more complex, it offers greater flexibility and functionality compared to normalize-range, especially for data visualization purposes.
Utility for normalizing a numeric range, with a wrapping function useful for polar coordinates.
var nr = require('normalize-range');
nr.wrap(0, 360, 400);
//=> 40
nr.wrap(0, 360, -90);
//=> 270
nr.limit(0, 100, 500);
//=> 100
nr.limit(0, 100, -20);
//=> 0
// There is a convenient currying function
var wrapAngle = nr.curry(0, 360).wrap;
var limitTo10 = nr.curry(0, 10).limit;
wrapAngle(-30);
//=> 330
Normalizes a values that "wraps around". For example, in a polar coordinate system, 270˚ can also be
represented as -90˚.
For wrapping purposes we assume max
is functionally equivalent to min
, and that wrap(max + 1) === wrap(min + 1)
.
Wrap always assumes that min
is inclusive, and max
is exclusive.
In other words, if value === max
the function will wrap it, and return min
, but min
will not be wrapped.
nr.wrap(0, 360, 0) === 0;
nr.wrap(0, 360, 360) === 0;
nr.wrap(0, 360, 361) === 1;
nr.wrap(0, 360, -1) === 359;
You are not restricted to whole numbers, and ranges can be negative.
var π = Math.PI;
var radianRange = nr.curry(-π, π);
redianRange.wrap(0) === 0;
nr.wrap(π) === -π;
nr.wrap(4 * π / 3) === -2 * π / 3;
Normalize the value by bringing it within the range.
If value
is greater than max
, max
will be returned.
If value
is less than min
, min
will be returned.
Otherwise, value
is returned unaltered.
Both ends of this range are inclusive.
Returns true
if value
is within the range, false
otherwise.
It defaults to inclusive
on both ends of the range, but that can be
changed by setting minExclusive
and/or maxExclusive
to a truthy value.
Returns value
or throws an error if value
is outside the specified range.
Returns a string representing this range in range notation.
Convenience method for currying all method arguments except value
.
var angle = require('normalize-range').curry(-180, 180, false, true);
angle.wrap(270)
//=> -90
angle.limit(200)
//=> 180
angle.test(0)
//=> true
angle.validate(300)
//=> throws an Error
angle.toString() // or angle.name()
//=> "[-180,180)"
Required
Type: number
The minimum value (inclusive) of the range.
Required
Type: number
The maximum value (exclusive) of the range.
Required
Type: number
The value to be normalized.
Type: number
The normalized value.
npm test
: tests, linting, coverage and style checks.npm run watch
: autotest mode for active development.npm run debug
: run tests without coverage (istanbul can obscure line #'s)Release via cut-release
tool.
MIT © James Talmage
FAQs
Utility for normalizing a numeric range, with a wrapping function useful for polar coordinates
The npm package normalize-range receives a total of 8,637,097 weekly downloads. As such, normalize-range popularity was classified as popular.
We found that normalize-range demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.