Skip to content

GeneratorService

katanakit-js / 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.

import { useUuid } from "katanakit-js";
const id = useUuid();
// => "3b241101-e2bb-4d7a-8615-..."
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.

Parameter Type Description
plainText string The text to hash.
salt? string Optional hex salt. When omitted a random 16-byte salt is generated.

Promise<string>

"salt:hashHex" — the salt and the 128-char hex digest joined by a colon.

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(): number;

Defined in: core/services/generator.service.ts:57

Returns the next auto-incrementing numeric id.

number

A monotonically increasing integer starting from 1.

import { useNumericId } from "katanakit-js";
useNumericId(); // 1
useNumericId(); // 2

usePbkdf2Hash(plainText, salt?): Promise<string>;

Defined in: core/services/generator.service.ts:216

Alias for GeneratorService.useHash.

Parameter Type Description
plainText string The text to hash.
salt? string Optional hex salt. When omitted a random 16-byte salt is generated.

Promise<string>

"salt:hashHex".

import { usePbkdf2Hash } from "katanakit-js";
const digest = await usePbkdf2Hash("secret");
// => "<32-char salt>:<128-char hex>"

useSlugify(text): string;

Defined in: core/services/generator.service.ts:114

Converts a text string into a URL-friendly slug.

Parameter Type Description
text string The text to slugify. Must not be empty.

string

A lower-case, hyphen-separated slug with diacritics removed.

If text is empty or falsy.

import { useSlugify } from "katanakit-js";
useSlugify("Hello World!"); // "hello-world"
useSlugify(" Café au Lait "); // "cafe-au-lait"

useToken(): number;

Defined in: core/services/generator.service.ts:142

Returns a cryptographically strong 6-digit numeric token (100000–999999).

number

A random 6-digit integer.

import { useToken } from "katanakit-js";
const code = useToken(); // e.g. 482916

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.

string

A RFC 4122 v4 UUID string.

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-..."

static getInstance(): GeneratorService;

Defined in: core/services/generator.service.ts:37

Returns the shared instance (Singleton).

GeneratorService