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');
console.log(+[]);
console.log(+'');
console.log(+' ');
console.log(typeof NaN);
console.log(typeof Infinity);
True
import {isNumber} from 'https://esm.run/@alwatr/math';
isNumber(5e3);
isNumber(0xff);
isNumber(-1.1);
isNumber(0);
isNumber(1);
isNumber(1.1);
isNumber('-1.1');
isNumber('0');
isNumber('0xff');
isNumber('1');
isNumber('1.1');
isNumber('5e3');
isNumber('012');
isNumber(parseInt('012'));
isNumber(parseFloat('012'));
False
import {isNumber} from 'https://esm.run/@alwatr/math';
isNumber(Infinity);
isNumber(NaN);
isNumber(null);
isNumber(undefined);
isNumber('');
isNumber(' ');
isNumber('foo');
isNumber([1]);
isNumber([]);
isNumber(function () {});
isNumber({});
transformToRange(x: number, options}): number
Transform a number from one range to another.
Options:
{
in: [number, number];
out: [number, number];
bound?: boolean;
}
Example
transformToRange(5, {in: [0, 10], out: [0, 100]});
Make percentage of any value
transformToRange(2000, {in: [0, 5000], out: [0, 100]});
Calculate progress-bar with
const progressOuterWith = 400;
const gap = 5;
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);
random.integer(min: number, max: number): number
Generate a random integer between min and max.
console.log(random.integer(1, 10));
random.float(min: number, max: number): number
Generate a random float between min and max.
console.log(random.float(1, 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));
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));
shuffle(array: any[]): any[]
Shuffle an array.
const array = [1, 2, 3, 4, 5];
random.shuffle(array);
console.log(array);