String Utils JS
A comprehensive collection of utility functions for string manipulation, including trimming, casing, formatting, validation, transformation, HTML escaping, random string generation, and more. These utilities are designed to simplify common string operations and are used across the System Designer Core module.
If this package has been helpful to you, your support goes a long way in helping maintain it, improve its features, and build more open-source tools like it. Buy Me a Coffee ☕
Features
- Trimming & Whitespace: Trim, remove, or replace whitespace.
- Casing: Convert to upper, lower, camel, kebab, snake, and title case.
- Formatting: Truncate, pad, repeat, join, and replace substrings.
- Validation: Check for empty strings, prefixes, and suffixes.
- Transformation: Reverse, capitalize, remove duplicates, and split strings.
- HTML Utilities: Escape and unescape HTML characters, strip HTML tags.
- Random String Generation: Generate random strings with customizable character sets.
- Slugify: Convert strings into URL-friendly slugs.
- Pluralization: Pluralize and singularize English nouns.
Installation
npm install @js-utility/string
API & Use Cases
Trimming & Whitespace
trim(str)
Removes whitespace from both ends.
import { trim } from '@js-utility/string';
trim(' Hello World! '); // 'Hello World!'
removeWhitespace(str)
Removes all whitespace.
import { removeWhitespace } from '@js-utility/string';
removeWhitespace(' a b c d '); // 'abcd'
replaceMultipleSpaces(str)
Replaces multiple spaces with a single space.
import { replaceMultipleSpaces } from '@js-utility/string';
replaceMultipleSpaces('a b c'); // 'a b c'
Casing
upper(str)
Converts to uppercase.
import { upper } from '@js-utility/string';
upper('hello'); // 'HELLO'
lower(str)
Converts to lowercase.
import { lower } from '@js-utility/string';
lower('HELLO'); // 'hello'
capitalize(str)
Capitalizes the first letter.
import { capitalize } from '@js-utility/string';
capitalize('hello world'); // 'Hello world'
capitalizeEachWord(str)
Capitalizes the first letter of each word.
import { capitalizeEachWord } from '@js-utility/string';
capitalizeEachWord('hello world'); // 'Hello World'
camelCase(str)
Converts to camelCase.
import { camelCase } from '@js-utility/string';
camelCase('hello world example'); // 'helloWorldExample'
kebabCase(str)
Converts to kebab-case.
import { kebabCase } from '@js-utility/string';
kebabCase('Hello World Example'); // 'hello-world-example'
snakeCase(str)
Converts to snake_case.
import { snakeCase } from '@js-utility/string';
snakeCase('Hello World Example'); // 'hello_world_example'
titleCase(str)
Converts to Title Case.
import { titleCase } from '@js-utility/string';
titleCase('hello world example'); // 'Hello World Example'
Formatting
truncate(str, maxLength)
Truncates a string and adds ellipsis if needed.
import { truncate } from '@js-utility/string';
truncate('Hello World', 5); // 'He...'
padLeft(str, targetLength, padChar?)
Pads string on the left.
import { padLeft } from '@js-utility/string';
padLeft('42', 5, '0'); // '00042'
padRight(str, targetLength, padChar?)
Pads string on the right.
import { padRight } from '@js-utility/string';
padRight('42', 5, '0'); // '42000'
padBoth(str, targetLength, padChar?)
Pads string on both sides.
import { padBoth } from '@js-utility/string';
padBoth('42', 6, '*'); // '**42**'
repeat(str, count)
Repeats a string.
import { repeat } from '@js-utility/string';
repeat('ab', 3); // 'ababab'
replaceAll(str, search, replacement)
Replaces all occurrences of a substring.
import { replaceAll } from '@js-utility/string';
replaceAll('foo bar foo', 'foo', 'baz'); // 'baz bar baz'
joinStrings(separator, ...parts)
Joins multiple strings with a separator.
import { joinStrings } from '@js-utility/string';
joinStrings('-', 'a', 'b', 'c'); // 'a-b-c'
Validation
isEmptyStr(str)
Checks if a string is empty or whitespace.
import { isEmptyStr } from '@js-utility/string';
isEmptyStr(' '); // true
startsWith(str, prefix)
Checks if string starts with prefix.
import { startsWith } from '@js-utility/string';
startsWith('Hello', 'He'); // true
endsWith(str, suffix)
Checks if string ends with suffix.
import { endsWith } from '@js-utility/string';
endsWith('Hello', 'lo'); // true
Transformation
reverse(str)
Reverses a string.
import { reverse } from '@js-utility/string';
reverse('abc'); // 'cba'
split(str, delimiter)
Splits a string by delimiter.
import { split } from '@js-utility/string';
split('a,b,c', ','); // ['a', 'b', 'c']
charAt(str, index)
Gets character at index.
import { charAt } from '@js-utility/string';
charAt('hello', 1); // 'e'
toCharArray(str)
Converts string to array of characters.
import { toCharArray } from '@js-utility/string';
toCharArray('abc'); // ['a', 'b', 'c']
removeDuplicateChars(str)
Removes duplicate characters.
import { removeDuplicateChars } from '@js-utility/string';
removeDuplicateChars('aabbcc'); // 'abc'
removeDuplicateWords(str)
Removes duplicate words.
import { removeDuplicateWords } from '@js-utility/string';
removeDuplicateWords('foo bar foo baz'); // 'foo bar baz'
removeConsecutiveDuplicates(str)
Removes consecutive duplicate characters.
import { removeConsecutiveDuplicates } from '@js-utility/string';
removeConsecutiveDuplicates('aaabbbcc'); // 'abc'
removeConsecutiveDuplicateWords(str)
Removes consecutive duplicate words.
import { removeConsecutiveDuplicateWords } from '@js-utility/string';
removeConsecutiveDuplicateWords('foo foo bar bar bar baz'); // 'foo bar baz'
HTML Utilities
escapeHtml(str)
Escapes special HTML characters.
import { escapeHtml } from '@js-utility/string';
escapeHtml('<div>"Hello"</div>'); // '<div>"Hello"</div>'
unescapeHtml(str)
Unescapes HTML entities.
import { unescapeHtml } from '@js-utility/string';
unescapeHtml('<div>Hello</div>'); // '<div>Hello</div>'
stripHtmlTags(str)
Removes HTML tags.
import { stripHtmlTags } from '@js-utility/string';
stripHtmlTags('<b>Hello</b> World'); // 'Hello World'
Random String Generation
random(length?, type?)
Generates a random string.
Types: "upper"
, "lower"
, "alpha"
, "number"
, "alphanumeric"
, "special"
, "mix"
(default).
import { random } from '@js-utility/string';
random(8, 'alpha'); // e.g. 'aBcDeFgH'
random(10, 'number'); // e.g. '4839201745'
Slugify
slugify(str)
Converts to a URL-friendly slug.
import { slugify } from '@js-utility/string';
slugify('Hello World!'); // 'hello-world'
Pluralization
pluralize(str)
Converts a singular noun to plural.
import { pluralize } from '@js-utility/string';
pluralize('child'); // 'children'
singularize(str)
Converts a plural noun to singular.
import { singularize } from '@js-utility/string';
singularize('children'); // 'child'
📘 TypeScript Support
This package is built with full TypeScript support. All functions are type-safe, and type definitions are bundled, so you get autocomplete, inline documentation, and compile-time safety out of the box, no need to install @types
.
Examples :
import { deepMerge } from '@js-utility/object';
const result = deepMerge<{ a: number }, { b: string }>({ a: 1 }, { b: 'hello' });
// result is inferred as: { a: number; b: string }
🧪 Testing
This package is thoroughly tested using Jest, with a focus on correctness, edge cases, and null-safety.
🤝 Contributing
This project is maintained privately. While direct contributions (e.g., pull requests or code changes) are not open to the public, feedback, suggestions, and issue reports are always welcome.
If you notice any bugs, edge cases, or have ideas for improvement, feel free to reach out or open an issue (if access is available). Your input helps make the package more robust and useful for everyone!
💖 Support / Donate
If you find this package useful, consider supporting its development. Your support helps maintain the project, improve documentation, and add new features.
Support as through :
💬 Support & Feedback
Have ideas, suggestions, or found a bug? I'd love to hear from you!
- Feedback: Whether it’s a feature request or an edge case you'd like handled, your input helps improve the package.
- Issues: If you run into a bug or unexpected behavior, feel free to open an issue (if the repo is accessible).
- Reach Out: You can also reach out directly for feedback or discussion via email or the contact details in the repository.
Your feedback helps shape better tools for everyone using this package.