@js-utility/string
Version:
A lightweight and powerful collection of string utility functions for Node.js - trimming, casing, formatting, and more.
380 lines (275 loc) β’ 9.79 kB
Markdown
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.
[ ](https://buymeacoffee.com/pradip_sabhadiya)
- **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.
```bash
npm install @js-utility/string
```
Removes whitespace from both ends.
```javascript
import { trim } from '@js-utility/string';
trim(' Hello World! '); // 'Hello World!'
```
Removes all whitespace.
```javascript
import { removeWhitespace } from '@js-utility/string';
removeWhitespace(' a b c d '); // 'abcd'
```
Replaces multiple spaces with a single space.
```javascript
import { replaceMultipleSpaces } from '@js-utility/string';
replaceMultipleSpaces('a b c'); // 'a b c'
```
Converts to uppercase.
```javascript
import { upper } from '@js-utility/string';
upper('hello'); // 'HELLO'
```
Converts to lowercase.
```javascript
import { lower } from '@js-utility/string';
lower('HELLO'); // 'hello'
```
Capitalizes the first letter.
```javascript
import { capitalize } from '@js-utility/string';
capitalize('hello world'); // 'Hello world'
```
Capitalizes the first letter of each word.
```javascript
import { capitalizeEachWord } from '@js-utility/string';
capitalizeEachWord('hello world'); // 'Hello World'
```
Converts to camelCase.
```javascript
import { camelCase } from '@js-utility/string';
camelCase('hello world example'); // 'helloWorldExample'
```
Converts to kebab-case.
```javascript
import { kebabCase } from '@js-utility/string';
kebabCase('Hello World Example'); // 'hello-world-example'
```
Converts to snake_case.
```javascript
import { snakeCase } from '@js-utility/string';
snakeCase('Hello World Example'); // 'hello_world_example'
```
Converts to Title Case.
```javascript
import { titleCase } from '@js-utility/string';
titleCase('hello world example'); // 'Hello World Example'
```
Truncates a string and adds ellipsis if needed.
```javascript
import { truncate } from '@js-utility/string';
truncate('Hello World', 5); // 'He...'
```
Pads string on the left.
```javascript
import { padLeft } from '@js-utility/string';
padLeft('42', 5, '0'); // '00042'
```
Pads string on the right.
```javascript
import { padRight } from '@js-utility/string';
padRight('42', 5, '0'); // '42000'
```
Pads string on both sides.
```javascript
import { padBoth } from '@js-utility/string';
padBoth('42', 6, '*'); // '**42**'
```
Repeats a string.
```javascript
import { repeat } from '@js-utility/string';
repeat('ab', 3); // 'ababab'
```
Replaces all occurrences of a substring.
```javascript
import { replaceAll } from '@js-utility/string';
replaceAll('foo bar foo', 'foo', 'baz'); // 'baz bar baz'
```
Joins multiple strings with a separator.
```javascript
import { joinStrings } from '@js-utility/string';
joinStrings('-', 'a', 'b', 'c'); // 'a-b-c'
```
Checks if a string is empty or whitespace.
```javascript
import { isEmptyStr } from '@js-utility/string';
isEmptyStr(' '); // true
```
Checks if string starts with prefix.
```javascript
import { startsWith } from '@js-utility/string';
startsWith('Hello', 'He'); // true
```
Checks if string ends with suffix.
```javascript
import { endsWith } from '@js-utility/string';
endsWith('Hello', 'lo'); // true
```
Reverses a string.
```javascript
import { reverse } from '@js-utility/string';
reverse('abc'); // 'cba'
```
Splits a string by delimiter.
```javascript
import { split } from '@js-utility/string';
split('a,b,c', ','); // ['a', 'b', 'c']
```
Gets character at index.
```javascript
import { charAt } from '@js-utility/string';
charAt('hello', 1); // 'e'
```
Converts string to array of characters.
```javascript
import { toCharArray } from '@js-utility/string';
toCharArray('abc'); // ['a', 'b', 'c']
```
Removes duplicate characters.
```javascript
import { removeDuplicateChars } from '@js-utility/string';
removeDuplicateChars('aabbcc'); // 'abc'
```
Removes duplicate words.
```javascript
import { removeDuplicateWords } from '@js-utility/string';
removeDuplicateWords('foo bar foo baz'); // 'foo bar baz'
```
Removes consecutive duplicate characters.
```javascript
import { removeConsecutiveDuplicates } from '@js-utility/string';
removeConsecutiveDuplicates('aaabbbcc'); // 'abc'
```
Removes consecutive duplicate words.
```javascript
import { removeConsecutiveDuplicateWords } from '@js-utility/string';
removeConsecutiveDuplicateWords('foo foo bar bar bar baz'); // 'foo bar baz'
```
Escapes special HTML characters.
```javascript
import { escapeHtml } from '@js-utility/string';
escapeHtml('<div>"Hello"</div>'); // '<div>"Hello"</div>'
```
Unescapes HTML entities.
```javascript
import { unescapeHtml } from '@js-utility/string';
unescapeHtml('<div>Hello</div>'); // '<div>Hello</div>'
```
Removes HTML tags.
```javascript
import { stripHtmlTags } from '@js-utility/string';
stripHtmlTags('<b>Hello</b> World'); // 'Hello World'
```
Generates a random string.
Types: `"upper"`, `"lower"`, `"alpha"`, `"number"`, `"alphanumeric"`, `"special"`, `"mix"` (default).
```javascript
import { random } from '@js-utility/string';
random(8, 'alpha'); // e.g. 'aBcDeFgH'
random(10, 'number'); // e.g. '4839201745'
```
Converts to a URL-friendly slug.
```javascript
import { slugify } from '@js-utility/string';
slugify('Hello World!'); // 'hello-world'
```
Converts a singular noun to plural.
```javascript
import { pluralize } from '@js-utility/string';
pluralize('child'); // 'children'
```
Converts a plural noun to singular.
```javascript
import { singularize } from '@js-utility/string';
singularize('children'); // 'child'
```
---
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 :
```typescript
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 }
```
---
This package is thoroughly tested using Jest, with a focus on correctness, edge cases, and null-safety.
---
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 :
- [Buy Me a Coffee β](https://buymeacoffee.com/pradip_sabhadiya)
- [GitHub](https://github.com/sponsors/AIWebBuilder)
---
## π¬ 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.
A comprehensive collection of utility functions