npm package: @js-utility/string
Description: A lightweight and powerful collection of string utility functions for Node.js - trimming, casing, formatting, and more.
Author: Pradip Sabhadiya
Latest version: 1.0.1

# 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 ☕](https://buymeacoffee.com/pradip_sabhadiya)

## 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

```bash
npm install @js-utility/string
```

## API & Use Cases

### Trimming & Whitespace

#### `trim(str)`
Removes whitespace from both ends.

```javascript
import { trim } from '@js-utility/string';
trim('   Hello World!   '); // 'Hello World!'
```

#### `removeWhitespace(str)`
Removes all whitespace.

```javascript
import { removeWhitespace } from '@js-utility/string';
removeWhitespace('  a b  c d '); // 'abcd'
```

#### `replaceMultipleSpaces(str)`
Replaces multiple spaces with a single space.

```javascript
import { replaceMultipleSpaces } from '@js-utility/string';
replaceMultipleSpaces('a   b    c'); // 'a b c'
```

### Casing

#### `upper(str)`
Converts to uppercase.

```javascript
import { upper } from '@js-utility/string';
upper('hello'); // 'HELLO'
```

#### `lower(str)`
Converts to lowercase.

```javascript
import { lower } from '@js-utility/string';
lower('HELLO'); // 'hello'
```

#### `capitalize(str)`
Capitalizes the first letter.

```javascript
import { capitalize } from '@js-utility/string';
capitalize('hello world'); // 'Hello world'
```

#### `capitalizeEachWord(str)`
Capitalizes the first letter of each word.

```javascript
import { capitalizeEachWord } from '@js-utility/string';
capitalizeEachWord('hello world'); // 'Hello World'
```

#### `camelCase(str)`
Converts to camelCase.

```javascript
import { camelCase } from '@js-utility/string';
camelCase('hello world example'); // 'helloWorldExample'
```

#### `kebabCase(str)`
Converts to kebab-case.

```javascript
import { kebabCase } from '@js-utility/string';
kebabCase('Hello World Example'); // 'hello-world-example'
```

#### `snakeCase(str)`
Converts to snake_case.

```javascript
import { snakeCase } from '@js-utility/string';
snakeCase('Hello World Example'); // 'hello_world_example'
```

#### `titleCase(str)`
Converts to Title Case.

```javascript
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.

```javascript
import { truncate } from '@js-utility/string';
truncate('Hello World', 5); // 'He...'
```

#### `padLeft(str, targetLength, padChar?)`
Pads string on the left.

```javascript
import { padLeft } from '@js-utility/string';
padLeft('42', 5, '0'); // '00042'
```

#### `padRight(str, targetLength, padChar?)`
Pads string on the right.

```javascript
import { padRight } from '@js-utility/string';
padRight('42', 5, '0'); // '42000'
```

#### `padBoth(str, targetLength, padChar?)`
Pads string on both sides.

```javascript
import { padBoth } from '@js-utility/string';
padBoth('42', 6, '*'); // '**42**'
```

#### `repeat(str, count)`
Repeats a string.

```javascript
import { repeat } from '@js-utility/string';
repeat('ab', 3); // 'ababab'
```

#### `replaceAll(str, search, replacement)`
Replaces all occurrences of a substring.

```javascript
import { replaceAll } from '@js-utility/string';
replaceAll('foo bar foo', 'foo', 'baz'); // 'baz bar baz'
```

#### `joinStrings(separator, ...parts)`
Joins multiple strings with a separator.

```javascript
import { joinStrings } from '@js-utility/string';
joinStrings('-', 'a', 'b', 'c'); // 'a-b-c'
```

### Validation

#### `isEmptyStr(str)`
Checks if a string is empty or whitespace.

```javascript
import { isEmptyStr } from '@js-utility/string';
isEmptyStr('   '); // true
```

#### `startsWith(str, prefix)`
Checks if string starts with prefix.

```javascript
import { startsWith } from '@js-utility/string';
startsWith('Hello', 'He'); // true
```

#### `endsWith(str, suffix)`
Checks if string ends with suffix.

```javascript
import { endsWith } from '@js-utility/string';
endsWith('Hello', 'lo'); // true
```

### Transformation

#### `reverse(str)`
Reverses a string.

```javascript
import { reverse } from '@js-utility/string';
reverse('abc'); // 'cba'
```

#### `split(str, delimiter)`
Splits a string by delimiter.

```javascript
import { split } from '@js-utility/string';
split('a,b,c', ','); // ['a', 'b', 'c']
```

#### `charAt(str, index)`
Gets character at index.

```javascript
import { charAt } from '@js-utility/string';
charAt('hello', 1); // 'e'
```

#### `toCharArray(str)`
Converts string to array of characters.

```javascript
import { toCharArray } from '@js-utility/string';
toCharArray('abc'); // ['a', 'b', 'c']
```

#### `removeDuplicateChars(str)`
Removes duplicate characters.

```javascript
import { removeDuplicateChars } from '@js-utility/string';
removeDuplicateChars('aabbcc'); // 'abc'
```

#### `removeDuplicateWords(str)`
Removes duplicate words.

```javascript
import { removeDuplicateWords } from '@js-utility/string';
removeDuplicateWords('foo bar foo baz'); // 'foo bar baz'
```

#### `removeConsecutiveDuplicates(str)`
Removes consecutive duplicate characters.

```javascript
import { removeConsecutiveDuplicates } from '@js-utility/string';
removeConsecutiveDuplicates('aaabbbcc'); // 'abc'
```

#### `removeConsecutiveDuplicateWords(str)`
Removes consecutive duplicate words.

```javascript
import { removeConsecutiveDuplicateWords } from '@js-utility/string';
removeConsecutiveDuplicateWords('foo foo bar bar bar baz'); // 'foo bar baz'
```

### HTML Utilities

#### `escapeHtml(str)`
Escapes special HTML characters.

```javascript
import { escapeHtml } from '@js-utility/string';
escapeHtml('<div>"Hello"</div>'); // '&lt;div&gt;&quot;Hello&quot;&lt;/div&gt;'
```

#### `unescapeHtml(str)`
Unescapes HTML entities.

```javascript
import { unescapeHtml } from '@js-utility/string';
unescapeHtml('&lt;div&gt;Hello&lt;/div&gt;'); // '<div>Hello</div>'
```

#### `stripHtmlTags(str)`
Removes HTML tags.

```javascript
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).

```javascript
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.

```javascript
import { slugify } from '@js-utility/string';
slugify('Hello World!'); // 'hello-world'
```

### Pluralization

#### `pluralize(str)`
Converts a singular noun to plural.

```javascript
import { pluralize } from '@js-utility/string';
pluralize('child'); // 'children'
```

#### `singularize(str)`
Converts a plural noun to singular.

```javascript
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 : 
```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 }
```

---
## 🧪 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 :

- [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.