@alwatr/math
Advanced tools
Comparing version 0.28.0 to 0.29.0
@@ -6,2 +6,22 @@ # Change Log | ||
# [0.29.0](https://github.com/AliMD/alwatr/compare/v0.28.0...v0.29.0) (2023-02-10) | ||
### Bug Fixes | ||
- **typescript:** rollback to 4.9.5 ([cc30f85](https://github.com/AliMD/alwatr/commit/cc30f8502bf95868ff41ba986120b2842acba36b)) | ||
### Features | ||
- **i18n:** add replaceNumber and auto detect setLocale from html ([3413471](https://github.com/AliMD/alwatr/commit/341347149f8685bc259034f5593048aa7db0b927)) | ||
- **math:** clamp function ([6fe4423](https://github.com/AliMD/alwatr/commit/6fe44236fc123a32837cfb4ea278b783f2bc2e7a)) | ||
- **math:** getDeviceUuid ([946dad3](https://github.com/AliMD/alwatr/commit/946dad3544f2741462ff239edab8b4a9ea323bd6)) | ||
- **math:** random uuid ([738f51e](https://github.com/AliMD/alwatr/commit/738f51eb323100fafca0a5f515b48d215dae5b3c)) | ||
- **math:** rename deviceId to clientId ([b211fd4](https://github.com/AliMD/alwatr/commit/b211fd42245d51d7109186ddb2f41574d0f0b786)) | ||
### Performance Improvements | ||
- **math:** enhance getClientId ([3187039](https://github.com/AliMD/alwatr/commit/3187039a9b87472bda16af6d4b0b71e31c17f272)) | ||
- **math:** enhance getClientId ([af42959](https://github.com/AliMD/alwatr/commit/af429594950c7ded35af53414494dd2f6f4fe208)) | ||
- **unicode-digits:** enhance translate ([104bdba](https://github.com/AliMD/alwatr/commit/104bdba948df11e62577f084b1e51fc4c78e0d9c)) | ||
# [0.28.0](https://github.com/AliMD/alwatr/compare/v0.27.0...v0.28.0) (2023-01-20) | ||
@@ -8,0 +28,0 @@ |
@@ -49,6 +49,6 @@ import type { TransformRangeOptions } from './type.js'; | ||
* ```js | ||
* console.log(random.value); // 0.7124123 | ||
* console.log(random.number); // 0.7124123 | ||
* ``` | ||
*/ | ||
readonly value: number; | ||
readonly number: number; | ||
/** | ||
@@ -109,2 +109,13 @@ * Generate a random integer number between min and max (max included). | ||
readonly shuffle: <T>(array: T[]) => T[]; | ||
readonly getRandomValues: <T_1 extends ArrayBufferView | null>(array: T_1) => T_1; | ||
/** | ||
* Generate Random UUID. | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* console.log(random.uuid); | ||
* ``` | ||
*/ | ||
readonly uuid: `${string}-${string}-${string}-${string}-${string}`; | ||
}; | ||
@@ -129,3 +140,9 @@ export type DurationUnit = 's' | 'm' | 'h' | 'd' | 'w' | 'M' | 'y'; | ||
*/ | ||
export declare function parseDuration(duration: DurationString, toUnit?: DurationUnit | 'ms'): number; | ||
export declare const parseDuration: (duration: DurationString, toUnit?: DurationUnit | 'ms') => number; | ||
/** | ||
* Limit number in range (min, max). | ||
*/ | ||
export declare const clamp: (val: number, min: number, max: number) => number; | ||
export declare const hex: (bytes: Uint8Array) => string; | ||
export declare const getClientId: () => string; | ||
//# sourceMappingURL=math.d.ts.map |
63
math.js
@@ -81,6 +81,6 @@ import { globalAlwatr } from '@alwatr/logger'; | ||
* ```js | ||
* console.log(random.value); // 0.7124123 | ||
* console.log(random.number); // 0.7124123 | ||
* ``` | ||
*/ | ||
get value() { | ||
get number() { | ||
return Math.random(); | ||
@@ -107,3 +107,3 @@ }, | ||
*/ | ||
float: (min, max) => random.value * (max - min) + min, | ||
float: (min, max) => random.number * (max - min) + min, | ||
/** | ||
@@ -124,3 +124,3 @@ * Generate a random string with random length. | ||
for (let i = max != null ? random.integer(min, max) : min; i > 0; i--) { | ||
result += characters.charAt(Math.floor(random.value * charactersLength)); | ||
result += characters.charAt(Math.floor(random.number * charactersLength)); | ||
} | ||
@@ -150,3 +150,28 @@ return result; | ||
*/ | ||
shuffle: (array) => array.sort(() => random.value - 0.5), | ||
shuffle: (array) => array.sort(() => random.number - 0.5), | ||
getRandomValues: (array) => { | ||
return globalThis.crypto.getRandomValues(array); | ||
// TODO: check msCrypto | ||
}, | ||
/** | ||
* Generate Random UUID. | ||
* | ||
* Example: | ||
* | ||
* ```ts | ||
* console.log(random.uuid); | ||
* ``` | ||
*/ | ||
get uuid() { | ||
var _a; | ||
if ((_a = globalThis.crypto) === null || _a === void 0 ? void 0 : _a.randomUUID) { | ||
return globalThis.crypto.randomUUID(); | ||
} | ||
// else | ||
const bytes = random.getRandomValues(new Uint8Array(16)); | ||
bytes[6] = (bytes[6] & 0x0f) | 0x40; // version | ||
bytes[8] = (bytes[8] & 0xbf) | 0x80; // variant | ||
// prettier-ignore | ||
return `${hex(bytes.subarray(0, 4))}-${hex(bytes.subarray(4, 6))}-${hex(bytes.subarray(6, 8))}-${hex(bytes.subarray(8, 10))}-${hex(bytes.subarray(10, 16))}`; | ||
}, | ||
}; | ||
@@ -178,3 +203,3 @@ const unitConversion = { | ||
*/ | ||
export function parseDuration(duration, toUnit = 'ms') { | ||
export const parseDuration = (duration, toUnit = 'ms') => { | ||
duration = duration.trim(); | ||
@@ -191,3 +216,27 @@ const durationNumberStr = duration.substring(0, duration.length - 1).trimEnd(); // trimEnd for `10 m` | ||
return (durationNumber * unitConversion[durationUnit]) / (toUnit === 'ms' ? 1 : unitConversion[toUnit]); | ||
} | ||
}; | ||
/** | ||
* Limit number in range (min, max). | ||
*/ | ||
export const clamp = (val, min, max) => (val > max ? max : val < min ? min : val); | ||
export const hex = (bytes) => { | ||
let str = ''; | ||
for (let i = 0; i < bytes.length; i++) { | ||
str += bytes[i].toString(16).padStart(2, '0'); | ||
} | ||
return str; | ||
}; | ||
let clientId = null; | ||
export const getClientId = () => { | ||
if (clientId != null) { | ||
return clientId; | ||
} | ||
// else | ||
clientId = localStorage.getItem('client-id'); | ||
if (clientId == null) { | ||
clientId = random.uuid; | ||
localStorage.setItem('client-id', clientId); | ||
} | ||
return clientId; | ||
}; | ||
//# sourceMappingURL=math.js.map |
{ | ||
"name": "@alwatr/math", | ||
"version": "0.28.0", | ||
"version": "0.29.0", | ||
"description": "Simple useful Math library written in tiny TypeScript module.", | ||
@@ -16,3 +16,2 @@ "keywords": [ | ||
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>", | ||
"contributors": [], | ||
"license": "MIT", | ||
@@ -35,6 +34,6 @@ "files": [ | ||
"dependencies": { | ||
"@alwatr/logger": "^0.28.0", | ||
"tslib": "^2.4.1" | ||
"@alwatr/logger": "^0.29.0", | ||
"tslib": "^2.5.0" | ||
}, | ||
"gitHead": "f4f63e7db916fda3ceac8b1d448068fc46da6334" | ||
"gitHead": "801487f183f8afd8cba25e0fec5d508c0b4fe809" | ||
} |
@@ -69,5 +69,2 @@ declare const supportedLanguageList: { | ||
* | ||
* @param {Array<UnicodeLangKeys> | 'all' | 'common'} fromLanguages - The source language to be translated. | ||
* @param {UnicodeLangKeys} toLanguage - The destination language to be translated. | ||
* | ||
* Example: | ||
@@ -93,3 +90,3 @@ * | ||
*/ | ||
constructor(fromLanguages: Array<UnicodeLangKeys> | 'all' | 'common', toLanguage: UnicodeLangKeys); | ||
constructor(toLanguage: UnicodeLangKeys, fromLanguages?: Array<UnicodeLangKeys> | 'all'); | ||
/** | ||
@@ -96,0 +93,0 @@ * Convert the String of number of the source language to the destination language. |
@@ -61,2 +61,3 @@ const supportedLanguageList = { | ||
}; | ||
const commonLangList = ['en', 'fa', 'ar']; | ||
export class UnicodeDigits { | ||
@@ -69,5 +70,2 @@ _replacer(_, ...args) { | ||
* | ||
* @param {Array<UnicodeLangKeys> | 'all' | 'common'} fromLanguages - The source language to be translated. | ||
* @param {UnicodeLangKeys} toLanguage - The destination language to be translated. | ||
* | ||
* Example: | ||
@@ -93,9 +91,9 @@ * | ||
*/ | ||
constructor(fromLanguages, toLanguage) { | ||
constructor(toLanguage, fromLanguages = [...commonLangList]) { | ||
if (fromLanguages === 'all') { | ||
fromLanguages = Object.keys(supportedLanguageList); | ||
} | ||
else if (fromLanguages === 'common') { | ||
fromLanguages = ['en', 'fa', 'ar', 'hi']; | ||
} | ||
const removeSelf = fromLanguages.indexOf(toLanguage); | ||
if (removeSelf !== -1) | ||
fromLanguages.splice(removeSelf, 1); | ||
this._toLangZeroCode = supportedLanguageList[toLanguage]; | ||
@@ -134,5 +132,5 @@ const regParts = []; | ||
translate(str) { | ||
return str.replace(this._searchRegExt, this._replacer); | ||
return str.trim() === '' ? str : str.replace(this._searchRegExt, this._replacer); | ||
} | ||
} | ||
//# sourceMappingURL=unicode-digits.js.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
54535
647
+ Added@alwatr/logger@0.29.0(transitive)
+ Added@alwatr/type@0.29.0(transitive)
- Removed@alwatr/logger@0.28.0(transitive)
- Removed@alwatr/type@0.28.0(transitive)
Updated@alwatr/logger@^0.29.0
Updatedtslib@^2.5.0