Skip to content

Instantly share code, notes, and snippets.

@alexsad
Last active June 17, 2024 14:01
Show Gist options
  • Save alexsad/3ca670e0634a26975fc5fb6d624335d5 to your computer and use it in GitHub Desktop.
Save alexsad/3ca670e0634a26975fc5fb6d624335d5 to your computer and use it in GitHub Desktop.
gen uid
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