@nats-io/nuid
Version:
NUID - A highly performant unique identifier generator.
59 lines (58 loc) • 1.67 kB
TypeScript
/**
* `NuidImpl` is the internal implementation backing the public `Nuid`
* interface exported from `mod.ts`. Generates 22-base-62-character ids:
* 12-char crypto-random prefix + 10-char base-62 sequence.
*
* Output format matches Go `nats-io/nuid` — same alphabet and length —
* so nuids generated here look the same as nuids generated by Go.
*/
export declare class NuidImpl {
buf: Uint8Array;
private cbuf;
seqHi: number;
seqLo: number;
inc: number;
inited: boolean;
constructor();
/**
* Initializes a nuid with a crypto random prefix,
* and pseudo-random sequence and increment. This function
* is only called if any api on a nuid is called.
*
* @ignore
*/
private init;
/**
* Initializes the pseudo random sequence number and the increment range.
* @ignore
*/
private initSeqAndInc;
/**
* Sets the prefix from crypto random bytes. Converts them to base62.
*
* @ignore
*/
private setPre;
/**
* Fills the sequence portion of the buffer as base62 from
* the split-int seq (seqHi, seqLo). Performs long division
* by 62 over the 64-bit value using doubles only — no bigint.
*
* @ignore
*/
private fillSeq;
/**
* Returns the next nuid.
*/
next(): string;
/**
* Resets the prefix and counter for the nuid. This is typically
* called automatically from within next() if the current sequence
* exceeds the resolution of the nuid.
*/
reset(): void;
}
/**
* A nuid instance you can use by simply calling `next()` on it.
*/
export declare const nuid: NuidImpl;