@nestjs/core
Version:
The core of NestJS, a TypeScript framework for Node.js backends: modules, dependency injection and the request pipeline, on Express, Fastify or node:http.
30 lines (29 loc) • 974 B
JavaScript
export class DeterministicUuidRegistry {
static registry = new Map();
/**
* The next suffix to try for a given string. The ids of the smaller ones
* are all taken, and stay taken until `clear()`, so they need not be
* probed again on every call.
*/
static nextIncrements = new Map();
static get(str, inc = 0) {
inc = Math.max(inc, this.nextIncrements.get(str) ?? 0);
let id = inc ? this.hashCode(`${str}_${inc}`) : this.hashCode(str);
while (this.registry.has(id)) {
id = this.hashCode(`${str}_${++inc}`);
}
this.registry.set(id, true);
this.nextIncrements.set(str, inc + 1);
return id;
}
static clear() {
this.registry.clear();
this.nextIncrements.clear();
}
static hashCode(s) {
let h = 0;
for (let i = 0; i < s.length; i++)
h = (Math.imul(31, h) + s.charCodeAt(i)) | 0;
return h.toString();
}
}