stringloom
A lightweight, zero-dependency collection of string manipulation utilities for JavaScript and TypeScript. Think of it as a focused alternative to lodash's string methods.
Install
npm install stringloom
Usage
import { slugify, maskEmail, toCamelCase } from "stringloom";
slugify("Hello World!");
maskEmail("john.doe@example.com");
toCamelCase("background-color");
Works with both ESM (import) and CommonJS (require). Full TypeScript types included.
API
Slugify
slugify("Hello World!")
slugify("Héllo Wörld")
slugify("foo bar", { separator: "_" })
slugify("Hello World", { lowercase: false })
separator | string | "-" | Character to replace spaces with |
lowercase | boolean | true | Convert to lowercase |
Truncate
truncate("The quick brown fox jumps over the lazy dog", { length: 20 })
truncate("abcdefghij", { length: 7, ellipsis: "...", wordBoundary: false })
length | number | 30 | Max length including ellipsis |
ellipsis | string | "…" | String appended when truncated |
wordBoundary | boolean | true | Truncate at nearest word boundary |
Capitalize
capitalize("hello world")
capitalizeWords("hello world")
Case Conversion
toCamelCase("hello-world")
toPascalCase("hello-world")
toKebabCase("helloWorld")
toSnakeCase("helloWorld")
camelToKebab("backgroundColor")
camelToSnake("backgroundColor")
Handles camelCase, PascalCase, kebab-case, snake_case, and whitespace-separated input.
Masking
maskEmail("john.doe@example.com")
mask("4242424242424242")
mask("4242424242424242", { keepStart: 0, keepEnd: 4 })
maskPhone("+1 (555) 123-4567")
char | string | "*" | Character used for masking |
keepStart | number | 4 | Visible characters at the start |
keepEnd | number | 0 | Visible characters at the end |
Reverse
reverse("hello")
reverse("🚀🌍")
Unicode-safe string reversal.
Counting
countWords("Hello, World!")
countOccurrences("banana", "an")
Padding
padStart("42", 5, "0")
padEnd("42", 5, "0")
Template
template("Hello, {{name}}!", { name: "World" })
template("{{a}} + {{b}} = {{c}}", { a: "1", b: "2", c: "3" })
Simple {{key}} placeholder interpolation. Unknown placeholders are left as-is.
Strip / Collapse
stripHtml("<p>Hello <b>World</b></p>")
stripWhitespace(" h e l l o ")
collapseWhitespace(" hello world ")
Checks
isPalindrome("Racecar")
isBlank(" ")
isNumeric("12345")
isAlpha("hello")
isAlphanumeric("hello123")
Initials
initials("John Doe")
initials("alice bob charlie")
Excerpt
excerpt("The quick brown fox jumps over the lazy dog", "fox", { radius: 5 })
radius | number | 30 | Characters to include around the phrase |
omission | string | "…" | Indicator for omitted text |
All Exports
slugify | Convert string to URL-friendly slug |
truncate | Truncate with ellipsis & word boundary support |
capitalize | Capitalize first letter |
capitalizeWords | Capitalize first letter of every word |
toCamelCase | Convert to camelCase |
toPascalCase | Convert to PascalCase |
toKebabCase | Convert to kebab-case |
toSnakeCase | Convert to snake_case |
camelToKebab | camelCase/PascalCase to kebab-case |
camelToSnake | camelCase/PascalCase to snake_case |
maskEmail | Mask an email address |
mask | Mask a string with configurable visibility |
maskPhone | Mask a phone number (keeps last 4 digits) |
reverse | Unicode-safe string reversal |
countWords | Count words in a string |
countOccurrences | Count substring occurrences |
padStart | Left-pad a string |
padEnd | Right-pad a string |
template | {{key}} placeholder interpolation |
stripHtml | Remove HTML tags |
stripWhitespace | Remove all whitespace |
collapseWhitespace | Collapse whitespace to single spaces |
isPalindrome | Check if string is a palindrome |
isBlank | Check if string is empty/whitespace |
isNumeric | Check if string is all digits |
isAlpha | Check if string is all letters |
isAlphanumeric | Check if string is letters + digits |
initials | Extract initials from a name |
excerpt | Extract snippet around a search phrase |
License
MIT