import { Injectable } from '@nestjs/common'; @Injectable() export class ClockIDGenService { private _counter = 0n; private timestampBits = 42; private timestampMask = BigInt( Math.floor(Math.pow(2, this.timestampBits)) - 1, ); private counterBits = 10; private counterMask = BigInt(Math.floor(Math.pow(2, this.counterBits)) - 1); idGenerator(prefix?: string): string { const timestamp = BigInt(Date.now()) & this.timestampMask; const counter = BigInt(this._counter++) & this.counterMask; let now = timestamp << BigInt(this.counterBits); now = now | counter; const id = now.toString(36).toUpperCase(); return prefix ? prefix + id : id; } }