1 | import { customAlphabet } from 'nanoid'
|
2 | import { customAlphabet as customAlphabetAsync } from 'nanoid/async'
|
3 | import { customAlphabet as customAlphabetNonSecure } from 'nanoid/non-secure'
|
4 |
|
5 | export const ALPHABET_NUMBER = '0123456789'
|
6 | export const ALPHABET_LOWERCASE = 'abcdefghijklmnopqrstuvwxyz'
|
7 | export const ALPHABET_UPPERCASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
8 | export const ALPHABET_ALPHANUMERIC_LOWERCASE = [ALPHABET_NUMBER, ALPHABET_LOWERCASE].join('')
|
9 | export const ALPHABET_ALPHANUMERIC_UPPERCASE = [ALPHABET_NUMBER, ALPHABET_UPPERCASE].join('')
|
10 | export const ALPHABET_ALPHANUMERIC = [ALPHABET_NUMBER, ALPHABET_LOWERCASE, ALPHABET_UPPERCASE].join(
|
11 | '',
|
12 | )
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | export function stringId(length = 16, alphabet = ALPHABET_ALPHANUMERIC_LOWERCASE): string {
|
19 | return customAlphabet(alphabet, length)()
|
20 | }
|
21 |
|
22 | export async function stringIdAsync(
|
23 | length = 16,
|
24 | alphabet = ALPHABET_ALPHANUMERIC_LOWERCASE,
|
25 | ): Promise<string> {
|
26 | return await customAlphabetAsync(alphabet, length)()
|
27 | }
|
28 |
|
29 | export function stringIdUnsafe(length = 16, alphabet = ALPHABET_ALPHANUMERIC_LOWERCASE): string {
|
30 | return customAlphabetNonSecure(alphabet, length)()
|
31 | }
|