npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

scalr

v1.1.4

Published

Lightweight scaling and normalization utilities.

Downloads

322

Readme

scalr

npm version GitHub version Build Status stability-stable Coverage Status

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 }

Installation

Run:

npm install -s scalr

API

Scaling & Normalization

normalize(collection: number[] | { [key: string | number]: number })

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 }

scale(collection: number[] | { [key: string | number]: number }, scale = 1)

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 }

Validation

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.

isNormalized(collection: number[] | { [key: string | number]: number }, tolerance = 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

Math & Statistics

Utility functions are also provided for performing math and statistics operations with objects and arrays.

sum(collection: number[] | { [key: string | number]: number })

Finds the sum of an array or object containing numbers.

sum([1, 1, 1]);                   // => 3
sum({ a: 0.5, b: 1 });            // => 1.5

difference(collection: number[] | { [key: string | number]: number })

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

product(collection: number[] | { [key: string | number]: number })

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

quotient(collection: number[] | { [key: string | number]: number })

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

average(collection: number[] | { [key: string | number]: number })

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

standardDeviation(collection: number[] | { [key: string | number]: number })

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

Other Functions

For documentation of underlying functions, please see the docs.