MCPcopy
hub / github.com/ulid/javascript / monotonicFactory

Function monotonicFactory

source/ulid.ts:155–169  ·  view source on GitHub ↗
(prng?: PRNG)

Source from the content-addressed store, hash-verified

153 * ulid(); // "01HNZXD07M5CEN5XA66EMZSRZW"
154 */
155export function monotonicFactory(prng?: PRNG): ULIDFactory {
156 const currentPRNG = prng || detectPRNG();
157 let lastTime: number = 0,
158 lastRandom: string;
159 return function _ulid(seedTime?: number): ULID {
160 const seed = !seedTime || isNaN(seedTime) ? Date.now() : seedTime;
161 if (seed <= lastTime) {
162 const incrementedRandom = (lastRandom = incrementBase32(lastRandom));
163 return encodeTime(lastTime, TIME_LEN) + incrementedRandom;
164 }
165 lastTime = seed;
166 const newRandom = (lastRandom = encodeRandom(RANDOM_LEN, currentPRNG));
167 return encodeTime(seed, TIME_LEN) + newRandom;
168 };
169}
170
171/**
172 * Generate a ULID

Callers 2

cli.tsFile · 0.85
ulid.spec.tsFile · 0.85

Calls 4

detectPRNGFunction · 0.85
incrementBase32Function · 0.85
encodeTimeFunction · 0.85
encodeRandomFunction · 0.85

Tested by

no test coverage detected