diff options
author | Dessalines <tyhou13@gmx.com> | 2020-06-23 12:52:07 -0400 |
---|---|---|
committer | Dessalines <tyhou13@gmx.com> | 2020-06-23 12:52:07 -0400 |
commit | 4c582cf1b6d9d9a348dd6efc6c337a0b1754bb8e (patch) | |
tree | 32e6159a2c96767d104f026399ca1043ee1b76a5 /ui/src/utils.ts | |
parent | 43905a041b348d3f7ae5256b84ae07447b61579b (diff) |
Making sure new comments don't clear out your current textarea.
- Making a better random string generator.
- Doing better incoming comment checking.
- Fixes #769
Diffstat (limited to 'ui/src/utils.ts')
-rw-r--r-- | ui/src/utils.ts | 25 |
1 files changed, 20 insertions, 5 deletions
diff --git a/ui/src/utils.ts b/ui/src/utils.ts index af54d77a..bdb9afbd 100644 --- a/ui/src/utils.ts +++ b/ui/src/utils.ts @@ -114,11 +114,26 @@ export const emojiPicker = new EmojiButton({ // TODO i18n }); -export function randomStr() { - return Math.random() - .toString(36) - .replace(/[^a-z]+/g, '') - .substr(2, 10); +const DEFAULT_ALPHABET = + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + +function getRandomCharFromAlphabet(alphabet: string): string { + return alphabet.charAt(Math.floor(Math.random() * alphabet.length)); +} + +export function randomStr( + idDesiredLength: number = 20, + alphabet = DEFAULT_ALPHABET +): string { + /** + * Create n-long array and map it to random chars from given alphabet. + * Then join individual chars as string + */ + return Array.from({ length: idDesiredLength }) + .map(() => { + return getRandomCharFromAlphabet(alphabet); + }) + .join(''); } export function wsJsonToRes(msg: WebSocketJsonResponse): WebSocketResponse { |