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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@snigo.dev/mathx

v0.0.6

Published

Math functions for JavaScript

Downloads

17

Readme

MathX

Collection of useful math functions for everyday JS development.

Usage

In the terminal:


$ npm install @snigo.dev/mathx

Then in the module:


// JavaScript modules
import { num } from '@snigo.dev/mathx';

const myNum = num('33%'); // 0.33

Motivation

There is always a time in our developer's life when we start noticing some small functions that are just being reused from project to project. Sounds familiar? The only reasonable step from there is to make it a package and reuse them in a centralized manner. Also why not keep it open-source so people can benefit from it as well, and this is exactly what MathX is.

API


MathX.approx()

Checks if the first argument approximately equals to the second argument within delta, the third argument. Tries best to account for precision errors. Returns a boolean.

| Parameter | Type | Default value | Notes | |---------------|----------|-------------------|------------------------------------------------| | a | number | | | | b | number | | | | delta | number | 0 | Optional, defaults to 0 |


import { approx } from '@snigo.dev/mathx';

approx(0.34, 0.45, 0.1); // false
approx(0.34, 0.44, 0.1); // true
approx(0.3, 0.2, 0.1); // true

MathX.precision()

Calculates precision of the provided number, including negative precision, aka number of trailing zeros of the integer

| Parameter | Type | Default value | Notes | |---------------|----------|-------------------|------------------------------------------------| | number | number | | |


import { precision } from '@snigo.dev/mathx';

precision(0.45); // 2
precision('12.300'); // 1
precision(-1.2e-11); // 12
precision(0.45); // 2
precision(0.45); // 2
precision(0.45); // 2
precision(12000000); // -6
precision(1.45e+100); // -98
precision(Infinity); // 0

MathX.mod()

Calculates modulo in the correct way including negative numbers. Fixes so called JavaScript modulo bug: https://web.archive.org/web/20090717035140if_/javascript.about.com/od/problemsolving/a/modulobug.htm

| Parameter | Type | Default value | Notes | |---------------|-----------|-------------------|----------------| | a |number | | | | b |number | | |


import { mod } from '@snigo.dev/mathx';

mod(-2, 5); // 3
mod(2, -5); // -3
mod(21, 4); // 1

MathX.random()

This one is super obvious and I know that many libraries have it, but still. Generates random number within given range with certain precision. Negative precision will work as well. Returns generated number.

| Parameter | Type | Default value | Notes | |---------------|-----------|-------------------|--------------------------------------------------| | range |number[] | | Array in [min, max] format | | precision |number | 12 | Optional |


import { random } from '@snigo.dev/mathx';

random([-5, 5], 4); // -4.7824
random([0, 10000], -2); // 2200
random([0, 100], 0); // 71
random(); // 0.897057820671

MathX.round()

Another popular one. Similarly to _.round(), rounds number to certain precision. Negative precision will work as well. Digests strings if needed. Returns a rounded number.

| Parameter | Type | Default value | Notes | |---------------|-----------|-------------------|--------------------------------------------------| | number |number | | | | precision |number | 12 | Optional |


import { round } from '@snigo.dev/mathx';

round('0.45876453', 4); // 0.4588
round(0.1 + 0.2); // 0.3
round(23567, -3); // 24000
round(23567, -2); // 23600
round(23567, -1); // 23570
round(23567, -5); // 0

Note unusual behavior: while many would expect default precision to be 0, MathX.round() uses default precision of 12


MathX.num()

Converts string or number to a certain precision. Understands percentage and do not coerces any other types into number.

| Parameter | Type | Default value | Notes | |---------------|-----------|-------------------|--------------------------------------------------| | numberLike |any | | | | precision |number | 12 | Optional |


import { num } from '@snigo.dev/mathx';

num('3.45e2'); // 345
num(0.1 + 0.2); // 0.3
num('13.359%', 4); // 0.1336
// Be careful with following:
num(true); // NaN
num(null); // NaN
num(); // NaN
num([1]); // NaN