Skip to content

Commit

Permalink
Merge pull request #291 from AliMD/feat/math
Browse files Browse the repository at this point in the history
Feat/math
  • Loading branch information
alimd authored Sep 19, 2022
2 parents 04be227 + 996c507 commit d9656f6
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 5 deletions.
18 changes: 18 additions & 0 deletions demo/math/translate-unicode-digits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {translateUnicodeDigits} from '@alwatr/math';

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

console.log(list);

console.log(translateUnicodeDigits(list, 'fa'));
2 changes: 2 additions & 0 deletions packages/core/math/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Simple useful Math library written in tiny TypeScript module.

## API

@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.
Expand Down
119 changes: 114 additions & 5 deletions packages/core/math/src/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function isNumber(value: unknown): boolean {
* this.progressBar.style.width = `${progressBarWith}px`;
* ```
*/
export const transformToRange = (x: number, options: TransformRangeOptions): number => {
export function transformToRange(x: number, options: TransformRangeOptions): number {
// prettier-ignore
let y = ((options.out[1] - options.out[0]) * (x - options.in[0])) / (options.in[1] - options.in[0]) + options.out[0];
if (options.bound) {
Expand All @@ -64,7 +64,7 @@ export const transformToRange = (x: number, options: TransformRangeOptions): num
}

return y;
};
}

const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
Expand Down Expand Up @@ -162,12 +162,121 @@ const unitConversion = {
y: 31_536_000_000,
};

export function parseDuration(duration: DurationString, unit: DurationUnit | 'ms' = 'ms'): number {
/**
* Parse duration string to target unit.
*
* Example:
*
* ```js
* parseDuration('10s'); // 10,000
* parseDuration('10m'); // 600,000
* parseDuration('10h'); // 36,000,000
* parseDuration('10d'); // 864,000,000
* parseDuration('10w'); // 6,048,000,000
* parseDuration('10M'); // 25,920,000,000
* parseDuration('10y'); // 315,360,000,000
* parseDuration('10d', 'h'); // 240
* ```
*/
export function parseDuration(duration: DurationString, toUnit: DurationUnit | 'ms' = 'ms'): number {
duration = duration.trim() as DurationString;
const durationNumber = +duration.substring(0, duration.length - 1).trimEnd(); // trimEnd for `10 m`
const durationNumberStr = duration.substring(0, duration.length - 1).trimEnd(); // trimEnd for `10 m`
if (!isNumber(durationNumberStr)) {
throw new Error(`not_a_number`);
}
const durationNumber = +durationNumberStr;
const durationUnit = duration.substring(duration.length - 1) as DurationUnit;
if (unitConversion[durationUnit] == null) {
throw new Error(`invalid_init`);
}
return (durationNumber * unitConversion[durationUnit]) / (unit === 'ms' ? 1 : unitConversion[unit]);
return (durationNumber * unitConversion[durationUnit]) / (toUnit === 'ms' ? 1 : unitConversion[toUnit]);
}

export const unicodeDigits = {
'en': 0x0030,
'ar': 0x0660,
'fa': 0x06f0,
'nko': 0x07c0,
'hi': 0x0966, // devanagari
'bn': 0x09e6, // bengali
'pa': 0x0a66, // gurmukhi, punjabi
'gu': 0x0ae6, // gujarati
'or': 0x0b66, // oriya
'ta': 0x0be6, // tamil
'te': 0x0c66, // telugu
'kn': 0x0ce6, // kannada

'mal': 0x0d66, // malayalam
'sinhala_lith': 0x0de6,
'thai': 0x0e50,
'lao': 0x0ed0,
'tibetan': 0x0f20,
'myanmar': 0x1040,
'myanmar_shan': 0x1090,
'khmer': 0x17e0,
'mongolian': 0x1810,
'limbu': 0x1946,
'new_tai_lue': 0x19d0,
'tai_tham_hora': 0x1a80,
'tai_tham_tham': 0x1a90,
'balinese': 0x1b50,
'sundanese': 0x1bb0,
'lepcha': 0x1c40,
'ol_chiki': 0x1c50,
'vai': 0xa620,
'saurashtra': 0xa8d0,
'kayah_li': 0xa900,
'javanese': 0xa9d0,
'myanmar_tai_laing': 0xa9f0,
'cham': 0xaa50,
'meetei_mayek': 0xabf0,
'fullwidth': 0xff10,
'osmanya': 0x104a0,
'brahmi': 0x11066,
'sora_sompeng': 0x110f0,
'chakma': 0x11136,
'sharada': 0x111d0,
'khudawadi': 0x112f0,
'newa': 0x11450,
'tirhuta': 0x114d0,
'modi': 0x11650,
'takri': 0x116c0,
'ahom': 0x11730,
'warang_citi': 0x118e0,
'bhaiksuki': 0x11c50,
'mro': 0x16a60,
'pahawh_hmong': 0x16b50,
'mathematical_bold': 0x1d7ce,
'mathematical_double-struck': 0x1d7d8,
'mathematical_sans-serif': 0x1d7e2,
'mathematical_sans-serif_bold': 0x1d7ec,
'mathematical_monospace': 0x1d7f6,
'fula': 0x1e950, // adlam script in fula lang
} as const;

export type LangKeys = keyof typeof unicodeDigits;
const allLangKeys: Array<LangKeys> = Object.keys(unicodeDigits) as Array<LangKeys>;

/**
* Replace all digital number in all languages to requested language.
*
* Example:
*
* ```js
* translateUnicodeDigits('123 ߁߂߃ ੧੨੩ ٣٤٦', 'fa'); // ۱۲۳ ۱۲۳ ۱۲۳ ۳۴۶
* ```
*/
export function translateUnicodeDigits(
string: string,
toLanguage: LangKeys = 'en',
fromLanguages: Array<LangKeys> = allLangKeys,
): string {
const toLangZeroCode = unicodeDigits[toLanguage];
for (const langKey of fromLanguages) {
const fromLangZeroCode = unicodeDigits[langKey];
for (let n = 0; n < 10; n++) {
string = string.replaceAll(String.fromCharCode(fromLangZeroCode + n), String.fromCharCode(toLangZeroCode + n));
}
}
return string;
}

0 comments on commit d9656f6

Please sign in to comment.