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

@alwatr/math

v1.3.1

Published

Simple useful Math library written in tiny TypeScript module.

Downloads

393

Readme

Alwatr Math - @alwatr/math

Simple useful Math library written in tiny TypeScript module.

API

UnicodeDigits(fromLanguages: Array<UnicodeLangKeys> | 'all' | 'common', toLanguage: UnicodeLangKeys)

Translate number.

  • fromLanguages The source language to be translated.
  • toLanguages The dest language to be translated.

Example:

const unicodeDigits = new UnicodeDigits('common', 'en');

const list = [
  '0123456789',
  '٠١٢٣٤٥٦٧٨٩',
  '߀߁߂߃߄߅߆߇߈߉',
  '०१२३४५६७८९',
  '০১২৩৪৫৬৭৮৯',
  '੦੧੨੩੪੫੬੭੮੯',
  '૦૧૨૩૪૫૬૭૮૯',
  '୦୧୨୩୪୫୬୭୮୯',
  '௦௧௨௩௪௫௬௭௮௯',
].join('\n');

console.log(unicodeDigits.translate(list));

unicodeDigits.translate(str: string): string

Convert the String of number of the source language to the destination language.

  • str is String of number of the source language.

@TODO: update from ts files docs

isNumber(value: unknown): boolean

Check the value is number or can convert to a number, for example string ' 123 ' can be converted to 123.

Why is this needed?

console.log(typeof '123'); //=> 'string'
console.log(+[]); //=> 0
console.log(+''); //=> 0
console.log(+'   '); //=> 0
console.log(typeof NaN); //=> 'number'
console.log(typeof Infinity); //=> 'number'

True

import {isNumber} from 'https://esm.run/@alwatr/math';

isNumber(5e3);               // true
isNumber(0xff);              // true
isNumber(-1.1);              // true
isNumber(0);                 // true
isNumber(1);                 // true
isNumber(1.1);               // true
isNumber('-1.1');            // true
isNumber('0');               // true
isNumber('0xff');            // true
isNumber('1');               // true
isNumber('1.1');             // true
isNumber('5e3');             // true
isNumber('012');             // true
isNumber(parseInt('012'));   // true
isNumber(parseFloat('012')); // true

False

import {isNumber} from 'https://esm.run/@alwatr/math';

isNumber(Infinity);          // false
isNumber(NaN);               // false
isNumber(null);              // false
isNumber(undefined);         // false
isNumber('');                // false
isNumber('   ');             // false
isNumber('foo');             // false
isNumber([1]);               // false
isNumber([]);                // false
isNumber(function () {});    // false
isNumber({});                // false

transformToRange(x: number, options}): number

Transform a number from one range to another.

Options:

{
  /**
   * The input range [min, max].
   *
   */
  in: [number, number];

  /**
   * The output (request) range [min, max].
   */
  out: [number, number];

  /**
   * If true, the output will be bounded to the output range (between min and max).
   *
   * In default behavior when x (input number) does not between input min~max range,
   * the output value will be out of output min~max range.
   *
   */
  bound?: boolean;
}

Example

transformToRange(5, {in: [0, 10], out: [0, 100]}); // => 50

Make percentage of any value

transformToRange(2000, {in: [0, 5000], out: [0, 100]}); // => 40

Calculate progress-bar with

const progressOuterWith = 400; //px
const gap = 5; //px (the visual gap between progressBar and component outer).
const currentProgress = 30; //%

const progressBarWith = transformToRange(currentProgress, {
  in: [0, 100],
  out: [componentPadding, progressOuterWith - componentPadding],
  bound: true,
});

this.progressBar.style.width = `${progressBarWith}px`;

Generate Random

value

Returns a float random number between 0 and 1 (1 Not included).

console.log(random.value); // 0.7124123

random.integer(min: number, max: number): number

Generate a random integer between min and max.

console.log(random.integer(1, 10)); // somewhere between 1 and 10

random.float(min: number, max: number): number

Generate a random float between min and max.

console.log(random.float(1, 10)); // somewhere between 1 and 10

string: (min: number, max?: number): string

Generate a random string with random length. The string will contain only characters from the characters list. The length of the string will be between min and max (max included). If max not specified, the length will be set to min.

console.log(random.string(6)); // something like 'Aab1V2'

step(min: number, max: number, step: number): number

Generate a random integer between min and max with a step.

console.log(random.step(6, 10, 2)); // 6 or 8 or 10

shuffle(array: any[]): any[]

Shuffle an array.

const array = [1, 2, 3, 4, 5];
random.shuffle(array);
console.log(array); // [2, 4, 3, 1, 5]