Scalr is a lightweight module of pure functions for normalizing and scaling numbers stored within arrays and objects to arbitrary unit lengths.
All inputs are treated as immutable.
import { normalize, scale } from 'scalr';
normalize([1, 1, 1, 1]); // => [0.25, 0.25, 0.25, 0.25]
normalize({ a: 4, b: 4, c: 8 }); // => { a: 0.25, b: 0.25, c: 0.5 }
scale([1, 1], 3); // => [1.5, 1.5]
scale({ a: 2, b: 8 }, 2); // => { a: 0.4, b: 1.6 }
Run:
npm install -s scalr
Normalizes an object or an array so that all of the contained values add up to 1.
normalize([1, 1, 1, 1]); // => [0.25, 0.25, 0.25, 0.25]
normalize({ a: 4, b: 4, c: 8 }); // => { a: 0.25, b: 0.25, c: 0.5 }
Normalizes an object or an array so that all of the contained values add up to the unit value of the provided scale.
scale([1, 1], 3); // => [1.5, 1.5]
scale({ a: 2, b: 8 }, 2); // => { a: 0.4, b: 1.6 }
Because we're working with floating point numbers, these validation methods need to define an acceptable tolerance of variability between the sum of the values in the data structure and 1. This defaults to Number.EPSILON.
Validates whether or not an array or object of values is normalized.
isNormalized([0.2, 0.2, 0.2], 3); // => TRUE
isNormalized({ a: 0.2, b: 0.8 }); // => TRUE
isScaled(collection: number[] | { [key: string | number]: number }, scale = 1, tolerance = Number.EPSILON)
Validates whether or not an array or object of values is scaled to a unit value.
isScaled([1, 1, 1], 3); // => TRUE
isScaled({ a: 0.5, b: 1 }, 2); // => TRUE
Utility functions are also provided for performing math and statistics operations with objects and arrays.
Finds the sum of an array or object containing numbers.
sum([1, 1, 1]); // => 3
sum({ a: 0.5, b: 1 }); // => 1.5
Finds the difference of an array or object containing numbers. The first entry populates the initial value, while any subsequent values are subtracted from that number. Objects will have their keys sorted alphabetically before subtraction to ensure consistent order.
difference([3, 5, 1]); // => 3 - 5 - 1 => -3
difference({ a: 12, b: 6 }); // => 12 - 6 => 6
Finds the product of an array or object containing numbers.
product([3, 5, 2]); // => 3 * 5 * 2 => 30
product({ a: 12, b: 6 }); // => 12 * 6 => 72
Finds the quotient of an array or object containing numbers. The first entry populates the initial value, while any subsequent values are used to divide that number. Objects will have their keys sorted alphabetically before subtraction to ensure consistent order.
quotient([20, 5, 2]); // => 20 / 5 / 2 => 2
quotient({ a: 12, b: 6 }); // => 12 / 6 => 2
Finds the average of an array or an object of number values. Can also be called with mean()
average([1, 2, 3]); // => 2
average({ a: 50, b: 25, c: 15 }); // => 30
Finds the standard deviation of an array or an object of number values. Can also be called with stDev()
standardDeviation([6, 60]); // => 27
standardDeviation({ a: 5, b: 10 }); // => 2.5
For documentation of underlying functions, please see the docs.