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"); // ["XML", "Http", "Request"]words("AIAgent"); // ["AI", "Agent"]words("hello-world"); // ["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 // uppercase first charcapitalize(str: string): string // ucfirst on every whitespace-separated worducfirst("hello"); // "Hello"capitalize("hello world"); // "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 "); // "hi"trim("---hi---", "-"); // "hi"ltrim("//path", "/"); // "path"rtrim("file.tmp", ".tmp"); // "file"Replacement family
replaceAll(str, search, replacement): stringreplaceFirst(str, search, replacement): stringreplaceLast(str, search, replacement): stringremoveFirst(str, needle): string // = replaceFirst(str, needle, "")removeLast(str, needle): string // = replaceLast(str, needle, "")search/needle are literal strings — they’re regex-escaped internally.
replaceAll("a-b-c", "-", "_"); // "a_b_c"replaceFirst("foo foo foo", "foo", "bar"); // "bar foo foo"replaceLast("foo bar foo", "foo", "baz"); // "foo bar baz"repeatsOf
repeatsOf(str: string, needle: string, caseSensitive?: boolean): number // default truerepeatsOf("abcabc", "a"); // 2repeatsOf("AbcAbc", "a", false); // 2Padding
pad(str, length, char?): string // pad both sides; extra char goes to endpadStart(str, length, char?): string // typed wrapper around String#padStartpadEnd(str, length, char?): string // typed wrapper around String#padEndpad("hi", 6); // " hi "pad("hi", 7, "*"); // "**hi***"padStart("7", 3, "0"); // "007"padEnd("7", 3, "0"); // "700"URL slugs & truncation
slugify
slugify(str, options?: { separator?: string; // default "-" lower?: boolean; // default true strict?: boolean; // default true — strip non-alphanumeric per token}): stringslugify("Hello, World!"); // "hello-world"slugify("café crème"); // "cafe-creme"slugify("Hello World", { separator: "_" }); // "hello_world"truncate
truncate(str, length, options?: { suffix?: string; // default "..." byWord?: boolean; // default false — cut at word boundary position?: "end" | "middle"; // default "end"}): stringtruncate("hello world", 8); // "hello..."truncate("hello world there", 14, { byWord: true }); // "hello world..."truncate("abcdefghij", 7, { position: "middle" }); // "ab...ij"readMoreChars / readMoreWords
readMoreChars(str, length, suffix?: string): string // default suffix "..."readMoreWords(str, wordCount, suffix?: string): stringreadMoreChars("hello world", 5); // "hello..."readMoreWords("a b c d e", 3); // "a b c..."HTML & masking
escapeHtml / unescapeHtml
escapeHtml(str: string): string // & < > " ' → entitiesunescapeHtml(str: string): string // reverseescapeHtml('<a href="x">'); // "<a href="x">"stripHtmlTags
stripHtmlTags(str, options?: { replacement?: string; // default "" stripScriptsAndStyles?: boolean; // default true — drops content too stripComments?: boolean; // default true}): stringNot a sanitizer — for untrusted HTML, use DOMPurify.
stripHtmlTags("<p>Hello <b>world</b></p>"); // "Hello world"stripHtmlTags("<script>alert(1)</script>safe"); // "safe"stripHtmlTags("<p>hi</p>", { replacement: " " }); // " hi "mask
mask(str, options?: { start?: number; // visible chars from start, default 0 end?: number; // visible chars from end, default 0 char?: string; // mask char, default "*"}): stringmask("4242424242424242", { start: 0, end: 4 }); // "************4242"mask("hassan@gmail.com", { start: 2, end: 4 }); // "ha**********.com"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" } });// "Hello Ada!"
template("{count} items", { count: 3 });// "3 items"Counting & reversal
wordCount(str: string): numbercharCount(str: string, options?: { unicode?: boolean }): number // grapheme-aware when truereverse(str: string): string // unicode-safe (handles surrogate pairs)wordCount("hello world"); // 2charCount("hello"); // 5charCount("👨👩", { unicode: true }); // 1 (single grapheme)reverse("hello"); // "olleh"Misc
initials
initials(name: string, separator?: string): string // default separator ""initials("Ada Lovelace"); // "AL"initials("Ada Lovelace", "."); // "A.L"extension
extension(filename: string): stringReturns the part after the last dot, or "" if absent.
extension("foo.txt"); // "txt"extension("archive.tar.gz"); // "gz"extension("README"); // ""toInputName
toInputName(path: string): string"a.b.c" → "a[b][c]" for HTML form name attributes.
toInputName("user.address.city"); // "user[address][city]"toInputName("user.tags[]"); // "user[tags][]"Arabic
startsWithArabic(str: string, trimmed?: boolean): boolean // default trimmed = truecontainsArabic(str: string): booleanARABIC_REGEX: RegExp // /[-ۿ]/ARABIC_PATTERN: RegExp // @deprecated — same as ARABIC_REGEXstartsWithArabic("مرحبا"); // truestartsWithArabic(" مرحبا"); // true (trimmed)containsArabic("hello مرحبا"); // true