Skip to content

Commit

Permalink
feat: tuid first version
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Cipriano committed Mar 8, 2023
1 parent 16635fc commit 1afd091
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 40 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-suns-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"timestamp-uid": minor
---

first version
19 changes: 0 additions & 19 deletions CHANGELOG.md

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fuid",
"version": "0.2.2",
"name": "timestamp-uid",
"version": "0.0.1",
"license": "MIT",
"main": "dist/index.js",
"module": "dist/index.mjs",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { fuid, decode, encode } from './fuid'
export { tuid, decode, encode } from './tuid'
12 changes: 12 additions & 0 deletions src/randomBase36.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import crypto from "crypto"

const randomInt = crypto?.randomInt || ((int) => Math.floor(Math.random() * int));

export function randomBase36(length: number) {
let entropy = ""

while (entropy.length < length)
entropy = entropy + randomInt(36).toString(36)

return entropy
}
26 changes: 8 additions & 18 deletions src/fuid.ts → src/tuid.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
// FUID - human-Friendly Unique ID
// tuid - human-readable Timestamp Unique ID

import crypto from "crypto"
import { randomBase36 } from "./randomBase36"

const randomInt = crypto?.randomInt || ((int) => Math.floor(Math.random() * int));

function randomBase36(length: number) {
let entropy = ""

while (entropy.length < length)
entropy = entropy + randomInt(36).toString(36)

return entropy
}

export function fuid(randLength = 4) {
export function tuid(randLength = 4) {
return encode({ randLength })
}

Expand All @@ -36,16 +26,16 @@ export function encode(options = {}) {

const entropy = randStr || (randValue && randValue.toString(36)) || randomBase36(randLength || 4) // 36^4 = 1/1,679,616

const _fuid = `${year}${month}${day}-${milisecondsBase36}-${entropy}`
const _tuid = `${year}${month}${day}-${milisecondsBase36}-${entropy}`

return _fuid
return _tuid
}

export function decode(_fuid: string) {
const [date, milisecondsAtDay, randStr] = _fuid.split('-')
export function decode(_tuid: string) {
const [date, milisecondsAtDay, randStr] = _tuid.split('-')

if (!date || !milisecondsAtDay || !randStr)
throw new Error("string provided is not a fuid")
throw new Error("string provided is not a tuid")

const isodate = `20${date.substring(0, 2)}-${date.substring(2, 4)}-${date.substring(4)}`

Expand Down

0 comments on commit 1afd091

Please sign in to comment.