Skip to content

Instantly share code, notes, and snippets.

@HallexCosta
Created January 24, 2021 02:35
Show Gist options
  • Save HallexCosta/6c2a14d2be1a888fb9ff963702b96f3b to your computer and use it in GitHub Desktop.
Save HallexCosta/6c2a14d2be1a888fb9ff963702b96f3b to your computer and use it in GitHub Desktop.
Generate Unique Id (ObjectId)
export function guid(): string {
const date = new Date()
const timestamp = ((date.getTime() / 1000) | 0).toString(16)
const context = `${timestamp}xxxxxxxxxxxxxxxx`
const hash = context
.replace(/[x]/g, () => ((Math.random() * 16) | 0).toString(16))
.toLowerCase()
return hash
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment