Skip to content

Commit

Permalink
feat(util): parseJson, getLocalStorageItem, getClientId (#802)
Browse files Browse the repository at this point in the history
  • Loading branch information
AliMD authored Feb 11, 2023
2 parents 68ef633 + 8be5a4a commit 5e40185
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 20 deletions.
14 changes: 0 additions & 14 deletions core/math/src/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
4 changes: 3 additions & 1 deletion core/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
16 changes: 16 additions & 0 deletions core/util/src/client-id.ts
Original file line number Diff line number Diff line change
@@ -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;
};
3 changes: 3 additions & 0 deletions core/util/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from './delay.js';
export * from './json.js';
export * from './local-storage.js';
export * from './client-id.js';
11 changes: 11 additions & 0 deletions core/util/src/json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type {Stringifyable} from '@alwatr/type';

export const parseJson = <T extends Stringifyable>(str: string): T | null => {
try {
return JSON.parse(str) as T;
}
catch (err) {
console.error(err);
return null;
}
};
8 changes: 8 additions & 0 deletions core/util/src/local-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {parseJson} from './json.js';

import type {Stringifyable} from '@alwatr/type';

export const getLocalStorageItem = <T extends Stringifyable>(name: string, defaultValue: T): T => {
const item = localStorage.getItem(name);
return item == null ? defaultValue : parseJson(item) ?? defaultValue;
};
5 changes: 4 additions & 1 deletion core/util/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@

"include": ["src/**/*.ts"],
"exclude": [],
"references": [{"path": "../math"}]
"references": [
{"path": "../type"},
{"path": "../math"},
]
}
5 changes: 1 addition & 4 deletions demo/math/translate-unicode-digits.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {UnicodeDigits, getClientId} from '@alwatr/math';
import {UnicodeDigits} from '@alwatr/math';

const unicodeDigits = new UnicodeDigits('fa', 'all');

Expand Down Expand Up @@ -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());

0 comments on commit 5e40185

Please sign in to comment.