normalizedim
v1.0.0
Published
Normalizes an N-dimensional array by setting one specific dimension to an exact value, while maintaining proportions for all other dimensions. It's an atomic, pure, and performant.
Downloads
147
Maintainers
Readme
normalizedim
Archetype: Node.js package
normalizeDim normalizes an N-dimensional array by setting one specific dimension (axis
) to an exact value
, while maintaining proportions for all other dimensions. It's an atomic, pure, and performant.
Details
- Works with any number of dimensions 2D, 3D, 4D, and beyond.
- Locks one dimension and rescales all others proportionally.
- Pure & atomic function ideal for composable utility pipelines.
Install
npm install normalizedim
Usage
import { normalizeDim } from 'normalizedim';
const dims = [1920, 1080, 400];
// ✅ Normalize width (index 0) to 1280
console.log(normalizeDim(dims, 0, 1280));
// [1280, 720, 266.67]
// ✅ Normalize height (index 1) to 800
console.log(normalizeDim(dims, 1, 800));
// [1422.22, 800, 296.30]
// ✅ Normalize depth (index 2) to 500
console.log(normalizeDim(dims, 2, 500));
// [2400, 1350, 500]
🔧 API
normalizeDim(dims: number[], axis: number, value: number): number[]
Scales an N-dimensional array such that the selected axis (axis
) is exactly value
, while maintaining the original proportions for all other dimensions.
dims
– Array of dimensions (e.g.,[width, height, depth]
).axis
– The index of the dimension to normalize (0-based).value
– The target size for the chosen dimension.- Returns: A new array with adjusted dimensions.
⚠️ Errors
Throws an error if:
axis
is out of bounds (< 0
or>= dims.length
).
Cheatsheet Patterns
These patterns help apply common transformations using normalizeDim
! They are excluded to preserve the atomicness of the package.
import normalizeDim from 'normalizedim';
// 🔢 Round the result (Ceil, Floor, Nearest)
const dims = normalizeDim([1920, 1080], 0, 1280);
const roundedDims = dims.map(Math.round);
//or Math.floor or Math.ceil
// ❓ Check if normalization occurred (detect changes)
const originalDims = [1920, 1080];
const normalizedDims = normalizeDim(originalDims, 0, 1920);
const isNormalized = !originalDims.every((dim, i) => dim === normalizedDims[i]);
console.log(isNormalized); // false (already at target size)
Example Use Cases
- Image Processing – Prevent excessive pixel count while keeping aspect ratio.
- 3D Modeling – Ensure objects stay within a total volume constraint.
- Physics Simulations – Rescale multi-dimensional datasets.
- Grid/Matrix Operations – Adjust data structures without exceeding capacity.
Related Packages
- https://github.com/alexstevovich/capdim – Caps total volume while keeping proportions.
- https://github.com/alexstevovich/fitdim – Ensures dimensions fit within a bounding box.
These link might be suffixed with "-node" in the future if conflicts arise.
Links
Development Homepage
https://github.com/alexstevovich/normalizedim
This link might be suffixed with "-node" in the future if conflicts arise.
License
Licensed under the Apache License 2.0.