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

moremath

v3.0.0

Published

The missing JavaScript Math functions

Downloads

427

Readme

MoreMath

npm Build Status Coverage Status Dependency Status

MoreMath is a library that provides useful Math functions -- functions that either should have been part of the core Math package from the start, or that are otherwise commonly needed.

Install

npm install moremath

MoreMath is written in TypeScript. It can be used both in JavaScript and TypeScript, but in TypeScript projects you get the benefit of code completion by default.

Usage

MoreMath can be used as a separate package (in which it acts like a Static Class) or by importing the specific functions one need.

Import the whole package:

// Import everything (JavaScript ES5)
var MoreMath = require('moremath');

// Import everything (JavaScript ES6 and TypeScript)
import MoreMath from 'moremath';

Then use any of its functions:

let value = MoreMath.clamp(11, 0, 10); // 10

Another way to use it is just importing the functions you want then using them directly.

Import a function:

// Import clamp() (JavaScript ES5)
var clamp = require('moremath').clamp;

// Import clamp() (JavaScript ES6 and TypeScript)
import { clamp } from 'moremath';

Then use it:

let value = clamp(11, 0, 10); // 10

The advantage of importing only specific functions is that the resulting code is shorter and easier to read, and JavaScript packagers that perform tree-shaking can get rid of functions that are not used from inside the MoreMath library. In the above case, for example, clamp would be the only exported function included in your final project.

Full reference

map(value:number, oldMin:number, oldMax:number, newMin:number = 0, newMax:number = 1, shouldClamp:Boolean = false):number

Maps a value from a range, determined by old minimum and maximum values, to a new range, determined by new minimum and maximum values. These minimum and maximum values are referential; the new value is not clamped by them.

Parameters:

  • value: The value to be re-mapped.
  • oldMin: The previous minimum value.
  • oldMax: The previous maximum value.
  • newMin: The new minimum value.
  • newMax: The new maximum value.

Returns:

  • The new value, mapped to the new range.

clamp(value:number, min:number = 0, max:number = 1):number

Clamps a number to a range, by restricting it to a minimum and maximum values: if the passed value is lower than the minimum value, it's replaced by the minimum; if it's higher than the maximum value, it's replaced by the maximum; if neither, it's unchanged.

Parameters:

  • value: The value to be clamped.
  • min: Minimum value allowed.
  • max: Maximum value allowed.

Returns:

  • The newly clamped value.

clampAuto(value:number, clamp1:number = 0, clamp2:number = 1):number

Clamps a number to a range, by restricting it to a range of values: if the passed value is lower than the minimum value, it's replaced by the minimum; if it's higher than the maximum value, it's replaced by the maximum; if neither, it's unchanged.

This function is similar to clamp(), but it switches the range values if necessary, without assuming the first value is the lower value.

Parameters:

  • value: The value to be clamped.
  • clamp1: One end of the allowed range.
  • clamp2: Other end of the allowed range.

Returns:

  • The newly clamped value.

getHighestPowerOfTwo(value:number):number

Returns a power of two value that is higher than the passed value.

Parameters:

  • value: The minimum value desired.

Returns:

  • A power of two value that is equal to or higher than the input value.

getUniqueNumber():number

Returns a unique number for this session. This is simply a global integer sequence, starting at 1.

Returns:

  • A unique integer for the session.

isPowerOfTwo(value:number):Boolean

Returns whether a number is a power of two (2, 4, 8, 16, etc).

Parameters:

  • value: A number to be tested.

Returns:

  • Whether the input number is a power of two.

rangeMod(value:number, min:number, pseudoMax:number):number

Restricts a value to a range, by restricting it to a minimum and maximum values but folding the value to the range instead of simply clamping to the minimum and maximum. It works like a more powerful Modulo function because it allows arbitrary ranges.

Parameters:

  • value: The value to be clamped.
  • min: Minimum value allowed.
  • max: Pseudo-maximum value allowed. This value is never reached; the minimum would be used instead.

Returns:

  • The new value, mapped to the range.

Examples:

console.log(rangeMod(14, 0, 10));
// Result: 4
console.log(rangeMod(360, 0, 360));
// Result: 0
console.log(rangeMod(360, -180, 180));
// Result: 0
console.log(rangeMod(21, 0, 10));
// Result: 1
console.log(rangeMod(-98, 0, 100));
// Result: 2

License

MIT.