1234567891011121314151617181920212223242526272829303132333435363738394041 |
- function guid(len = 32, firstU = true, radix = null) {
- let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
- let uuid = [];
- radix = radix || chars.length;
- if (len) {
-
- for (let i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
- } else {
- let r;
-
- uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
- uuid[14] = '4';
- for (let i = 0; i < 36; i++) {
- if (!uuid[i]) {
- r = 0 | Math.random() * 16;
- uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
- }
- }
- }
-
- if (firstU) {
- uuid.shift();
- return 'u' + uuid.join('');
- } else {
- return uuid.join('');
- }
- }
- export default guid;
|