Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@alwatr/math

Alwatr240MIT1.3.1TypeScript support: included

Simple useful Math library written in tiny TypeScript module.

math, mathematics, typescript, esm, alwatr

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]

changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

1.3.1 (2023-12-19)

Note: Version bump only for package @alwatr/math

1.3.0 (2023-12-19)

Features

  • use globalScope instead globalThis (94342bb) by @njfamirm

1.2.4 (2023-12-11)

Note: Version bump only for package @alwatr/math

1.2.3 (2023-12-09)

Note: Version bump only for package @alwatr/math

1.2.2 (2023-11-29)

Note: Version bump only for package @alwatr/math

1.2.1 (2023-11-23)

Note: Version bump only for package @alwatr/math

1.2.0 (2023-11-14)

Features

1.1.12 (2023-11-08)

Bug Fixes

  • move repo urls (719aa3e) by @AliMD

1.1.11 (2023-11-01)

Note: Version bump only for package @alwatr/math

1.1.10 (2023-10-23)

Note: Version bump only for package @alwatr/math

1.1.9 (2023-10-23)

Note: Version bump only for package @alwatr/math

1.1.8 (2023-10-23)

Bug Fixes

  • math: compatible with new logger api (49d1a28) by @AliMD

1.1.7 (2023-10-23)

Note: Version bump only for package @alwatr/math

1.1.6 (2023-10-23)

Note: Version bump only for package @alwatr/math

1.1.5 (2023-09-19)

Bug Fixes

  • package repo urls (466cbe9) by @AliMD

1.1.4 (2023-09-19)

Note: Version bump only for package @alwatr/math

1.1.3 (2023-09-19)

Note: Version bump only for package @alwatr/math

1.1.2 (2023-09-12)

Note: Version bump only for package @alwatr/math

1.1.1 (2023-09-12)

Note: Version bump only for package @alwatr/math

1.1.0 (2023-09-12)

1.0.0 (2023-06-14)

0.32.0 (2023-05-27)

0.31.0 (2023-05-08)

Features

0.30.0 (2023-03-06)

Bug Fixes

  • math: remove getClientId (b5894d6)

Features

0.29.0 (2023-02-10)

Bug Fixes

  • typescript: rollback to 4.9.5 (cc30f85)

Features

  • i18n: add replaceNumber and auto detect setLocale from html (3413471)
  • math: clamp function (6fe4423)
  • math: getDeviceUuid (946dad3)
  • math: random uuid (738f51e)
  • math: rename deviceId to clientId (b211fd4)

Performance Improvements

  • math: enhance getClientId (3187039)
  • math: enhance getClientId (af42959)
  • unicode-digits: enhance translate (104bdba)

0.28.0 (2023-01-20)

Bug Fixes

Features

  • type: add optional text on ChatPhotoMessage (af83f5b)

0.27.0 (2022-12-29)

Features

  • type: define math types (8c19f40)

0.26.0 (2022-12-22)

Bug Fixes

1.0.0 (2023-06-14)

Note: Version bump only for package @alwatr/math

0.32.0 (2023-05-27)

Note: Version bump only for package @alwatr/math

0.31.0 (2023-05-08)

Features

0.30.0 (2023-03-06)

Bug Fixes

  • math: remove getClientId (b5894d6)

Features

0.29.0 (2023-02-10)

Bug Fixes

  • typescript: rollback to 4.9.5 (cc30f85)

Features

  • i18n: add replaceNumber and auto detect setLocale from html (3413471)
  • math: clamp function (6fe4423)
  • math: getDeviceUuid (946dad3)
  • math: random uuid (738f51e)
  • math: rename deviceId to clientId (b211fd4)

Performance Improvements

  • math: enhance getClientId (3187039)
  • math: enhance getClientId (af42959)
  • unicode-digits: enhance translate (104bdba)

0.28.0 (2023-01-20)

Bug Fixes

Features

  • type: add optional text on ChatPhotoMessage (af83f5b)
  • type: define math types (8c19f40)

0.27.0 (2022-12-29)

Note: Version bump only for package @alwatr/math

0.26.0 (2022-12-22)

Bug Fixes

0.25.0 (2022-12-07)

Bug Fixes

  • math: UnicodeDigits type issue (508a9b2)

0.24.1 (2022-12-01)

Bug Fixes

  • set correct dependencies in package.json and references in tsconfig.json (1fb05a8)

0.24.0 (2022-11-28)

Bug Fixes

  • use ~ for package version (4e027ff)

0.23.0 (2022-11-23)

Note: Version bump only for package @alwatr/math

0.22.1 (2022-11-21)

Note: Version bump only for package @alwatr/math

0.22.0 (2022-11-20)

Note: Version bump only for package @alwatr/math

0.21.0 (2022-11-13)

Note: Version bump only for package @alwatr/math

0.20.0 (2022-11-05)

Note: Version bump only for package @alwatr/math

0.19.0 (2022-11-01)

Bug Fixes

  • Import error in webpack (1a52f67)

0.18.0 (2022-10-22)

Note: Version bump only for package @alwatr/math

0.17.0 (2022-10-21)

Features

  • Math: translateUnicodeDigits (996c507)

0.16.0 (2022-09-08)

Note: Version bump only for package @alwatr/math

0.15.0 (2022-09-01)

Note: Version bump only for package @alwatr/math

0.14.0 (2022-08-19)

Note: Version bump only for package @alwatr/math

0.13.0 (2022-08-06)

Features

  • token: generate and verify HOTP tpkens (d0372f8)

0.12.0 (2022-07-22)

Note: Version bump only for package @alwatr/math

0.11.0 (2022-04-16)

Note: Version bump only for package @alwatr/math

0.10.0 (2022-04-02)

Note: Version bump only for package @alwatr/math

0.9.0 (2022-03-22)

Note: Version bump only for package @alwatr/math

0.8.0 (2022-03-14)

Note: Version bump only for package @alwatr/math

0.7.1 (2022-03-12)

Note: Version bump only for package @alwatr/math

0.7.0 (2022-03-12)

Features