Last active
June 17, 2024 14:01
-
-
Save alexsad/3ca670e0634a26975fc5fb6d624335d5 to your computer and use it in GitHub Desktop.
gen uid
This file contains 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
import MD5 from "crypto-js/md5"; | |
const appendToString = (text: string, appendText: string, positionIndex: number) => { | |
return [text.slice(0, positionIndex), appendText, text.slice(positionIndex)].join(''); | |
}; | |
const genUUID = () => { | |
if (globalThis?.crypto && 'randomUUID' in globalThis.crypto) { | |
return `${globalThis.crypto.randomUUID()}`.replaceAll("-", "_"); | |
} | |
const stringRandom = (new Date()).getTime().toString(36) + Math.random().toString(36).slice(2); | |
return appendToString(stringRandom, '_', 5); | |
}; | |
const genHashFromString = (data: string) => { | |
return MD5(data).toString(); | |
}; | |
export { genHashFromString, genUUID }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment