| 12345678910111213141516171819202122 |
- const CHARSET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
- export function createRandomId(length = 32, prefixWithU = true, radix = null) {
- const base = radix || CHARSET.length
- const chars = []
- if (length) {
- for (let i = 0; i < length; i++) {
- chars.push(CHARSET[Math.floor(Math.random() * base)])
- }
- } else {
- chars[8] = chars[13] = chars[18] = chars[23] = '-'
- chars[14] = '4'
- for (let i = 0; i < 36; i++) {
- if (!chars[i]) {
- const r = Math.floor(Math.random() * 16)
- chars[i] = CHARSET[i === 19 ? (r & 0x3) | 0x8 : r]
- }
- }
- }
- const joined = prefixWithU && length ? 'u' + chars.join('') : chars.join('')
- return joined
- }
|