Last active
November 21, 2018 13:28
-
-
Save zmmbreeze/3af8142176ee4a56c1759913831fe66d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function random(): number { | |
let random; | |
// https://zh.wikipedia.org/wiki/2147483647 | |
// 2147483647 === 1111111111111111111111111111111 | |
try { | |
const arr = new Uint32Array(1); | |
// https://developer.mozilla.org/en-US/docs/Web/API/RandomSource/getRandomValues | |
window.crypto.getRandomValues(arr); | |
random = arr[0] & 2147483647; | |
} catch (b) { | |
random = Math.floor(Math.random() * 2147483648); | |
} | |
return random; | |
} | |
export function uuid(join: string = '.'): string { | |
return random().toString(36) | |
+ join + random().toString(36) | |
+ join + random().toString(36) | |
+ join + (+new Date()).toString(36); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment