| name | mongez-reinforcements-strings |
| description | String utilities from @mongez/reinforcements — case conversion, trimming, slugify, truncate, mask, template interpolation, HTML helpers, and Arabic detection.
|
Strings
Casing, trimming, replacement, padding, slugify, truncate, mask, template, HTML helpers, Arabic detection. Import from @mongez/reinforcements.
Casing — powered by words()
All casing functions share a single tokenizer that handles acronyms correctly.
words
words(input: string): string[]
words("XMLHttpRequest");
words("AIAgent");
words("hello-world");
Casing family
| Function | Signature | Example |
|---|
toCamelCase | (str) => string | toCamelCase("XMLHttpRequest") → "xmlHttpRequest" |
toStudlyCase | (str) => string | toStudlyCase("hello-world") → "HelloWorld" |
toPascalCase | (str) => string | Alias of toStudlyCase |
toSnakeCase | (str, separator?, lowerAll?) => string | toSnakeCase("AIAgent") → "ai_agent" |
toKebabCase | (str, lowerAll?) => string | toKebabCase("getUserID") → "get-user-id" |
toConstantCase | (str) => string | toConstantCase("apiBaseUrl") → "API_BASE_URL" |
toDotCase | (str, lowerAll?) => string | toDotCase("helloWorld") → "hello.world" |
toPathCase | (str, lowerAll?) => string | toPathCase("helloWorld") → "hello/world" |
toTitleCase | (str, options?: { stopWords? }) => string | toTitleCase("the lord of rings") → "The Lord of Rings" |
Letter case primitives
ucfirst(str: string): string
capitalize(str: string): string
ucfirst("hello");
capitalize("hello world");
Trimming
| Function | Signature | Notes |
|---|
trim | (str, needle?: string) => string | Trims both ends (default whitespace) |
ltrim | (str, needle?: string) => string | Start only |
rtrim | (str, needle?: string) => string | End only |
trim(" hi ");
trim("---hi---", "-");
ltrim("//path", "/");
rtrim("file.tmp", ".tmp");
Replacement family
replaceAll(str, search, replacement): string
replaceFirst(str, search, replacement): string
replaceLast(str, search, replacement): string
removeFirst(str, needle): string
removeLast(str, needle): string
search/needle are literal strings — they're regex-escaped internally.
replaceAll("a-b-c", "-", "_");
replaceFirst("foo foo foo", "foo", "bar");
replaceLast("foo bar foo", "foo", "baz");
repeatsOf
repeatsOf(str: string, needle: string, caseSensitive?: boolean): number
repeatsOf("abcabc", "a");
repeatsOf("AbcAbc", "a", false);
Padding
pad(str, length, char?): string
padStart(str, length, char?): string
padEnd(str, length, char?): string
pad("hi", 6);
pad("hi", 7, "*");
padStart("7", 3, "0");
padEnd("7", 3, "0");
URL slugs & truncation
slugify
slugify(str, options?: {
separator?: string;
lower?: boolean;
strict?: boolean;
}): string
slugify("Hello, World!");
slugify("café crème");
slugify("Hello World", { separator: "_" });
truncate
truncate(str, length, options?: {
suffix?: string;
byWord?: boolean;
position?: "end" | "middle";
}): string
truncate("hello world", 8);
truncate("hello world there", 14, { byWord: true });
truncate("abcdefghij", 7, { position: "middle" });
readMoreChars / readMoreWords
readMoreChars(str, length, suffix?: string): string
readMoreWords(str, wordCount, suffix?: string): string
readMoreChars("hello world", 5);
readMoreWords("a b c d e", 3);
HTML & masking
escapeHtml / unescapeHtml
escapeHtml(str: string): string
unescapeHtml(str: string): string
escapeHtml('<a href="x">');
stripHtmlTags
stripHtmlTags(str, options?: {
replacement?: string;
stripScriptsAndStyles?: boolean;
stripComments?: boolean;
}): string
Not a sanitizer — for untrusted HTML, use DOMPurify.
stripHtmlTags("<p>Hello <b>world</b></p>");
stripHtmlTags("<script>alert(1)</script>safe");
stripHtmlTags("<p>hi</p>", { replacement: " " });
mask
mask(str, options?: {
start?: number;
end?: number;
char?: string;
}): string
mask("4242424242424242", { start: 0, end: 4 });
mask("hassan@gmail.com", { start: 2, end: 4 });
Templates
template
template(str: string, vars: Record<string, any>): string
{path} interpolation with dot-notation paths into vars. Missing paths render as "".
template("Hello {user.name}!", { user: { name: "Ada" } });
template("{count} items", { count: 3 });
Counting & reversal
wordCount(str: string): number
charCount(str: string, options?: { unicode?: boolean }): number
reverse(str: string): string
wordCount("hello world");
charCount("hello");
charCount("👨👩", { unicode: true });
reverse("hello");
Misc
initials
initials(name: string, separator?: string): string
initials("Ada Lovelace");
initials("Ada Lovelace", ".");
extension
extension(filename: string): string
Returns the part after the last dot, or "" if absent.
extension("foo.txt");
extension("archive.tar.gz");
extension("README");
toInputName
toInputName(path: string): string
"a.b.c" → "a[b][c]" for HTML form name attributes.
toInputName("user.address.city");
toInputName("user.tags[]");
Arabic
startsWithArabic(str: string, trimmed?: boolean): boolean
containsArabic(str: string): boolean
ARABIC_REGEX: RegExp
ARABIC_PATTERN: RegExp
startsWithArabic("مرحبا");
startsWithArabic(" مرحبا");
containsArabic("hello مرحبا");