Utilities
import {
words, wordCount, join, template,
random, randomAlpha, randomNumeric, randomAlphanumeric,
similarity, transliterate,
} from '@ppabari/strio/str';
words / wordCount
words(str: string): string[]
wordCount(str: string): number
Split into words (runs of letters/digits), discarding punctuation. Unlike case conversion, this does not break camelCase boundaries.
words('Hello, world! 42'); // → ['Hello', 'world', '42']
wordCount('the quick brown fox'); // → 4
join
join(values: Array<string | number | null | undefined>, separator?: string): string
Join a list, skipping null, undefined, and empty strings — so you never get doubled or trailing separators.
join(['a', '', 'b', null, 'c']); // → 'a b c'
join(['2024', '01', '05'], '-'); // → '2024-01-05'
template
template(str: string, data: Record<string, unknown>, options?: { fallback?: string }): string
Interpolate `` placeholders. Whitespace inside braces is ignored. Missing keys are left untouched unless a fallback is given.
template('Hi !', { name: 'Ada' }); // → 'Hi Ada!'
template('/', { a: 1, b: 2 }); // → '1/2'
template('Hi ', {}, { fallback: '?' }); // → 'Hi ?'
Secure random strings
random(length: number, charset?: string): string
randomAlpha(length: number): string
randomNumeric(length: number): string
randomAlphanumeric(length: number): string
Cryptographically secure random strings, backed by the same bias-free Web-Crypto engine as the core library. charset accepts a named alias (e.g. 'hex', 'base58') or a raw character string; it defaults to 'alphanumeric'.
random(16); // → 16 alphanumeric chars
random(8, 'hex'); // → e.g. '9f2a4c0b'
randomNumeric(6); // → e.g. '048213'
randomAlpha(10); // → e.g. 'QwErTyUiOp'
For richer generation (prefixes, patterns, presets, entropy estimates) use generateRandomString from the main export.
similarity
similarity(a: string, b: string): number
Normalized Levenshtein similarity ratio in [0, 1] (1 = identical).
similarity('kitten', 'sitting'); // → ~0.571
similarity('abc', 'abc'); // → 1
transliterate
transliterate(str: string): string
Transliterate accented and special Latin characters to their closest ASCII equivalents. Non-Latin scripts are left unchanged.
transliterate('Héllo Wörld'); // → 'Hello World'
transliterate('Straße'); // → 'Strasse'
transliterate('naïve café'); // → 'naive cafe'