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

math-plus

v0.6.1

Published

A math library with a Vector class and added methods.

Downloads

125

Readme

Math Plus


Why

There are dozens of math libraries out there. Why use this one?

No good reason, personal preference. It's a lightweight Math library that extends the standard Math object with some overridden functionality, added methods and classes.

Usage

The main reason I use this package is because I like the rest/spread syntax in es6.

Rather than this:

const { random, floor, ceil } = Math

I can do this:

import { random, floor, ceil } from 'math-plus'

Overridden Methods

The round(), floor() and ceil() methods is overridden to take a second parameter place.

import { round, ceil, floor } from 'math-plus'

round(10.575, 0.1) // rounds to the nearest 0.1 = 10.6
ceil(109.10, 10) // ceil to the nearest 10 = 110
floor(86.5, 100) // floor to the nearest 100 = 0

The random() method is overridden to take up to three parameters: min, max and seed.

import { random } from 'math-plus'

random(2, 5) // random value between 2 and 5

random(-10, 10, 1000) // Third argument acts as a seed, providing the same number

random can also be used to return a random element from an array, string, or array-like. Min and max are treated as index ranges.

import { random } from 'math-plus'

const string = 'letters-are-cool'

// Returns a letter between index 0 (min) and array.length(max) (non-inclusive)
const randomChar = random(0, string.length, string)

const array = [ 'one', 2, { three: 3 } ]
// undefined as max will default to array.length
const randomItem = random(0, undefined, array)

// array-likes must have have number indexes and a length property
const arrayLike = { 0: 'one', 1: 'two', 2: 'three', length: 3 }
const randomArrayLikeItem = random(0, undefined, arrayLike)

// iterables also work (Set, Map, any Object with Symbol.iterator)
const iterable = new Set([1, 2, 3, 4])
const randomIterable = random(0, undefined, iterable)

See the Bindable Methods section for a cleaner syntax for the random method.

Added Methods

The lerp() method, which interpolates one value to another:

import { lerp } from 'math-plus'

// lerp(from, to, interpolator)
lerp(5, 10, 0.5) // 7.5
lerp(2, 1, 0.5) // 1.5
lerp(3, 6, 2) // 9

The clamp() method, which is self explanatory

import { clamp } from 'math-plus'

// clamp(value, min = 0, max = 1)
clamp(2) // 1
clamp(0, 1, 2) // 1

The isPrime() method, also self explanatory

import { isPrime } from 'math-plus'

// isPrime(value)
isPrime(5) // true
isPrime(4) // false

The primes() method, which returns a generator that iterates prime numbers

import { primes } from 'math-plus'

const to10 = [...primes(9)] // [2,3,5,7,9]

for (const prime of primes(50, 100))
  console.log(prime) //logs every prime between 50 and 100

Bindable Methods ::

I don't know if you have heard of the bind operator, but I LOVE the bind operator. Read more here: https://babeljs.io/docs/plugins/transform-function-bind/ Where-ever prudent, the overridden and custom methods allow numbers to be bound to them:


import { round, random, primes } from 'math-plus'

10.4::round() //10

1000::random(-10, 10) // binding a value to random gives it a seed

// You can also bind arrays, iterables, array-likes or strings to the random method

'string'::random() // random char
[1,2,3,4]::random() // random item

// primes isn't bindable, but it does return an iterator
primes(1000)::random() // random prime number between 0 and 1000

List of all bindable methods:

lerp, clamp, isPrime, round, floor, ceil, pow, abs, acos, acosh, asin, asinh, atan, atanh, cbrt, clz32, cos, cosh, exp, expm1, fround, log, log10, log1p, log2, sign, sin, sinh, sqrt, tan, tanh, trunc