diff --git a/core/math/src/math.ts b/core/math/src/math.ts index e8cb7cd00..9d4ab94b7 100644 --- a/core/math/src/math.ts +++ b/core/math/src/math.ts @@ -252,17 +252,3 @@ export const hex = (bytes: Uint8Array): string => { } return str; }; - -let clientId: string | null = null; -export const getClientId = (): string => { - if (clientId != null) { - return clientId; - } - // else - clientId = localStorage.getItem('client-id'); - if (clientId == null) { - clientId = random.uuid; - localStorage.setItem('client-id', clientId); - } - return clientId; -}; diff --git a/core/util/package.json b/core/util/package.json index ea868dfc7..11566edf7 100644 --- a/core/util/package.json +++ b/core/util/package.json @@ -32,6 +32,8 @@ "url": "https://github.com/AliMD/alwatr/issues" }, "dependencies": { - "tslib": "^2.5.0" + "tslib": "^2.5.0", + "@alwatr/math": "^0.29.0", + "@alwatr/type": "^0.29.0" } } diff --git a/core/util/src/client-id.ts b/core/util/src/client-id.ts new file mode 100644 index 000000000..9b280ada6 --- /dev/null +++ b/core/util/src/client-id.ts @@ -0,0 +1,16 @@ +import {random} from '@alwatr/math'; + +let clientId: string | null = null; + +export const getClientId = (): string => { + if (clientId != null) { + return clientId; + } + // else + clientId = localStorage.getItem('client-id'); + if (clientId == null) { + clientId = random.uuid; + localStorage.setItem('client-id', clientId); + } + return clientId; +}; diff --git a/core/util/src/index.ts b/core/util/src/index.ts index e36815a83..bc5a45ebe 100644 --- a/core/util/src/index.ts +++ b/core/util/src/index.ts @@ -1 +1,4 @@ export * from './delay.js'; +export * from './json.js'; +export * from './local-storage.js'; +export * from './client-id.js'; diff --git a/core/util/src/json.ts b/core/util/src/json.ts new file mode 100644 index 000000000..6a2d441e4 --- /dev/null +++ b/core/util/src/json.ts @@ -0,0 +1,11 @@ +import type {Stringifyable} from '@alwatr/type'; + +export const parseJson = (str: string): T | null => { + try { + return JSON.parse(str) as T; + } + catch (err) { + console.error(err); + return null; + } +}; diff --git a/core/util/src/local-storage.ts b/core/util/src/local-storage.ts new file mode 100644 index 000000000..c7d73872a --- /dev/null +++ b/core/util/src/local-storage.ts @@ -0,0 +1,8 @@ +import {parseJson} from './json.js'; + +import type {Stringifyable} from '@alwatr/type'; + +export const getLocalStorageItem = (name: string, defaultValue: T): T => { + const item = localStorage.getItem(name); + return item == null ? defaultValue : parseJson(item) ?? defaultValue; +}; diff --git a/core/util/tsconfig.json b/core/util/tsconfig.json index d26028499..f3c2df152 100644 --- a/core/util/tsconfig.json +++ b/core/util/tsconfig.json @@ -9,5 +9,8 @@ "include": ["src/**/*.ts"], "exclude": [], - "references": [{"path": "../math"}] + "references": [ + {"path": "../type"}, + {"path": "../math"}, + ] } diff --git a/demo/math/translate-unicode-digits.ts b/demo/math/translate-unicode-digits.ts index cc4f2719f..59f3547b1 100644 --- a/demo/math/translate-unicode-digits.ts +++ b/demo/math/translate-unicode-digits.ts @@ -1,4 +1,4 @@ -import {UnicodeDigits, getClientId} from '@alwatr/math'; +import {UnicodeDigits} from '@alwatr/math'; const unicodeDigits = new UnicodeDigits('fa', 'all'); @@ -27,6 +27,3 @@ for (let i = count; i > 0; i--) { } console.log((count / (Date.now() - start)) * 1000); - -console.log('client-id', getClientId()); -console.log('client-id', getClientId());