Numeric Range Normalization Utility
normalize-range is a utility package designed to normalize numeric values within specified ranges, particularly useful for applications involving cyclical systems like angles or polar coordinates. The current stable version is 0.1.2, released approximately 8 years ago, indicating the package is no longer actively maintained. Its core functionality includes `wrap` for values that "wrap around" (e.g., 361 degrees becoming 1 degree in a 0-360 range), `limit` for clamping values within a fixed inclusive range, `test` for range checking, `validate` for asserting values within a range, and `curry` for creating partially applied range functions. A key differentiator is its explicit handling of inclusive/exclusive range boundaries, which differs between `wrap` and `limit` operations, providing precise control for various normalization scenarios.
Common errors
-
ReferenceError: require is not defined in ES module scope
cause Attempting to use `require()` within a JavaScript file that is being treated as an ES module (e.g., due to `"type": "module"` in `package.json` or `.mjs` extension).fixIf your project is ESM-only, use dynamic import: `import('normalize-range').then(nrModule => { const nr = nrModule; /* ... */ });`. Alternatively, ensure the file using `require()` is a CommonJS module (e.g., `.js` file in a non-module project, or explicitly marked as `"type": "commonjs"`). -
TypeError: (0, _normalizeRange.wrap) is not a function
cause This typically occurs when a bundler or transpiler tries to convert a CommonJS `require` into an ESM `import` in a way that doesn't correctly handle the module's default export, or when directly trying to destructure from an incorrectly imported CJS module.fixEnsure that `normalize-range` is imported using the standard CommonJS `require` syntax: `const nr = require('normalize-range');` and then access methods like `nr.wrap(min, max, value)`.
Warnings
- breaking This package is CommonJS-only and lacks native ES module (ESM) support. Attempting to `import` it directly in an ES module environment will result in errors.
- gotcha The `wrap` function treats the `min` value as inclusive and the `max` value as exclusive (e.g., `wrap(0, 360, 360)` returns `0`). In contrast, the `limit` function treats both `min` and `max` as inclusive.
- deprecated The `normalize-range` package is no longer actively maintained. The last commit and release occurred approximately 8 years ago. Consider potential security implications or lack of compatibility with newer JavaScript features or environments.
Install
-
npm install normalize-range -
yarn add normalize-range -
pnpm add normalize-range
Imports
- normalize-range module
import nr from 'normalize-range'; // or import * as nr from 'normalize-range';
const nr = require('normalize-range'); - wrap function
import { wrap } from 'normalize-range';const { wrap } = require('normalize-range'); - curry function
import { curry } from 'normalize-range';const curryRange = require('normalize-range').curry;
Quickstart
const nr = require('normalize-range');
console.log('Wrapping 400 in (0, 360):', nr.wrap(0, 360, 400));
//=> 40
console.log('Wrapping -90 in (0, 360):', nr.wrap(0, 360, -90));
//=> 270
console.log('Limiting 500 in (0, 100):', nr.limit(0, 100, 500));
//=> 100
console.log('Limiting -20 in (0, 100):', nr.limit(0, 100, -20));
//=> 0
// Currying example for convenience
const wrapAngle = nr.curry(0, 360).wrap;
const limitTo10 = nr.curry(0, 10).limit;
console.log('Curried wrapAngle(-30):', wrapAngle(-30));
//=> 330
console.log('Curried limitTo10(15):', limitTo10(15));
//=> 10