GeneratorService
katanakit-js / GeneratorService
Class: GeneratorService
Section titled “Class: GeneratorService”Defined in: core/services/generator.service.ts:27
GeneratorService — Singleton facade for numeric ids, UUIDs, slugs, tokens and PBKDF2 hashing.
Applies the Singleton pattern (one shared instance via
GeneratorService.getInstance), the Facade pattern (one call per
generation concern, hiding node:crypto and globalThis.crypto details)
and the Strategy pattern: the exported LazyNodeCryptoStrategy
and NativeUuidStrategy are thin adapters that satisfy
ICryptoStrategy / IUuidStrategy while the implementation
stays in this class.
Every public method uses the use* prefix per CONTRIBUTING.md; the module
level use* functions below are thin wrappers kept for call-site ergonomics.
The auto-incrementing id counter is instance state, never a static.
Example
Section titled “Example”import { useUuid } from "katanakit-js";
const id = useUuid();// => "3b241101-e2bb-4d7a-8615-..."Methods
Section titled “Methods”useHash()
Section titled “useHash()”useHash(plainText, salt?): Promise<string>;Defined in: core/services/generator.service.ts:181
Derives a PBKDF2-SHA512 hash of plainText.
Returns "salt:hashHex". Prefer a unique salt per secret.
Implemented with a dynamic import("node:crypto") so it works in both
CJS and pure ESM environments without a top-level require.
NOTE: this is a one-way hash, not encryption. Do not rely on the fixed default salt for real password hashing; prefer scrypt/argon2id instead.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
plainText |
string |
The text to hash. |
salt? |
string |
Optional hex salt. When omitted a random 16-byte salt is generated. |
Returns
Section titled “Returns”Promise<string>
"salt:hashHex" — the salt and the 128-char hex digest joined by a colon.
Examples
Section titled “Examples”import { useHash } from "katanakit-js";
const digest = await useHash("secret", "optional-salt");// => "optional-salt:<128 hex chars>"import { LazyNodeCryptoStrategy } from "katanakit-js";
const digest = await LazyNodeCryptoStrategy.useHash("secret");// => "<32-char salt>:<128-char hex>"useNumericId()
Section titled “useNumericId()”useNumericId(): number;Defined in: core/services/generator.service.ts:57
Returns the next auto-incrementing numeric id.
Returns
Section titled “Returns”number
A monotonically increasing integer starting from 1.
Example
Section titled “Example”import { useNumericId } from "katanakit-js";
useNumericId(); // 1useNumericId(); // 2usePbkdf2Hash()
Section titled “usePbkdf2Hash()”usePbkdf2Hash(plainText, salt?): Promise<string>;Defined in: core/services/generator.service.ts:216
Alias for GeneratorService.useHash.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
plainText |
string |
The text to hash. |
salt? |
string |
Optional hex salt. When omitted a random 16-byte salt is generated. |
Returns
Section titled “Returns”Promise<string>
"salt:hashHex".
Example
Section titled “Example”import { usePbkdf2Hash } from "katanakit-js";
const digest = await usePbkdf2Hash("secret");// => "<32-char salt>:<128-char hex>"useSlugify()
Section titled “useSlugify()”useSlugify(text): string;Defined in: core/services/generator.service.ts:114
Converts a text string into a URL-friendly slug.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
text |
string |
The text to slugify. Must not be empty. |
Returns
Section titled “Returns”string
A lower-case, hyphen-separated slug with diacritics removed.
Throws
Section titled “Throws”If text is empty or falsy.
Example
Section titled “Example”import { useSlugify } from "katanakit-js";
useSlugify("Hello World!"); // "hello-world"useSlugify(" Café au Lait "); // "cafe-au-lait"useToken()
Section titled “useToken()”useToken(): number;Defined in: core/services/generator.service.ts:142
Returns a cryptographically strong 6-digit numeric token (100000–999999).
Returns
Section titled “Returns”number
A random 6-digit integer.
Example
Section titled “Example”import { useToken } from "katanakit-js";
const code = useToken(); // e.g. 482916useUuid()
Section titled “useUuid()”useUuid(): string;Defined in: core/services/generator.service.ts:85
Generates a RFC 4122 v4 UUID string.
Uses globalThis.crypto.randomUUID when available and falls back to a
Math.random-based v4 template otherwise.
Returns
Section titled “Returns”string
A RFC 4122 v4 UUID string.
Examples
Section titled “Examples”import { useUuid } from "katanakit-js";
const id = useUuid();// => "3b241101-e2bb-4d7a-8615-..."import { NativeUuidStrategy } from "katanakit-js";
const id = NativeUuidStrategy.useGenerate();// => "3b241101-e2bb-4d7a-8615-..."getInstance()
Section titled “getInstance()”static getInstance(): GeneratorService;Defined in: core/services/generator.service.ts:37
Returns the shared instance (Singleton).
Returns
Section titled “Returns”GeneratorService