-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (32 loc) · 1.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'use strict'
const DEFAULT_SIZE = 16;
const DEFAULT_CHARSET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXZ';
function getChars(ch){
const num = '0123456789';
const alpha = 'abcdefghijklmnopqrstuvwxyz';
if(ch.toLowerCase() === 'num') return num;
if(ch === 'alpha') return alpha;
if(ch === 'Alpha') return alpha + alpha.toUpperCase();
if(ch === 'ALPHA') return alpha.toUpperCase();
if(ch === 'hex') return num + 'abcdef';
if(ch === 'HEX') return num + 'ABCDEF';
if(ch === 'alphanum') return num + alpha;
if(ch === 'ALPHANUM') return num + alpha.toUpperCase();
return ch;
}
function generateUrid(length, chars) {
let id = '';
for (let i = 0; i < length; i++) {
id += chars[Math.floor(Math.random() * chars.length)];
}
return id;
}
export default function urid(size = DEFAULT_SIZE, charset = DEFAULT_CHARSET) {
let length = size;
let chars = charset;
if(arguments.length === 1 && typeof size === 'string'){
length = DEFAULT_SIZE;
chars = size;
}
return generateUrid(length, getChars(chars));
}