Skip to content

Function: makeIdentifier()

ts
function makeIdentifier(
  prefix: string,
  namespace: string,
  unprefixedLength: number,
  glue: string,
): string;

Generates unique HTML element identifiers using high-quality encoding for collision resistance. This function creates valid HTML IDs by combining a prefix with an encoded, sanitized suffix derived from namespace, timestamp, and random data.

The function ensures HTML ID validity by automatically fixing invalid prefixes and uses a robust encoding system to generate high-quality pseudo-random suffixes with excellent collision resistance properties.

Parameters

ParameterTypeDefault valueDescription
prefixstringundefinedThe prefix for the identifier. Will be prepended with 'p' if it doesn't start with a letter
namespacestringundefinedA namespace string to include in the uniqueness calculation
unprefixedLengthnumber8Length of the encoded suffix (default: 8)
gluestring'_'Character(s) used to join prefix and suffix (default: '_')

Returns

string

A valid HTML element ID guaranteed to start with a letter

Example

typescript
makeIdentifier("btn", "header"); // 'btn_a8f9c2d1'
makeIdentifier("123nav", "menu"); // 'p123nav_b7e4f8a9' (prefix auto-fixed)
makeIdentifier("form", "login", 12, "-"); // 'form-c9d8e7f6a5b4'
makeIdentifier("", "fallback"); // 'p_d6c5b4a3'