Package Exports
- @js-utility/string
Readme
String Utils JS
A collection of utility functions for string manipulation, including trimming, casing, formatting, validation, and transformation. These utilities are designed to simplify common string operations and are used across the System Designer Core module.
Features
- Trimming: Remove whitespace from strings.
- Casing: Convert strings to uppercase, lowercase, camel case, kebab case, snake case, and title case.
- Formatting: Truncate strings, pad strings, and replace substrings.
- Validation: Check if a string is empty or starts/ends with specific characters.
- Transformation: Reverse strings, capitalize words, and remove duplicates.
- HTML Utilities: Escape and unescape HTML characters.
- Random String Generation: Generate random strings with customizable character sets.
- Slugify: Convert strings into URL-friendly slugs.
Installation
To use this library in your project, install it via npm:
npm install @js-utility/stringUse Cases
Here are some common use cases for the String Utils JS library:
1. Trimming Strings
Remove leading and trailing whitespace from a string.
import { trim } from '@js-utility/string';
const result = trim(' Hello World! ');
console.log(result); // Output: 'Hello World!'2. Changing String Case
Convert a string to camel case.
import { toCamelCase } from '@js-utility/string';
const result = toCamelCase('hello world');
console.log(result); // Output: 'helloWorld'3. Validating Strings
Check if a string starts with a specific character.
import { startsWith } from '@js-utility/string';
const result = startsWith('Hello World', 'H');
console.log(result); // Output: true4. Generating Random Strings
Generate a random alphanumeric string of length 10.
import { generateRandomString } from '@js-utility/string';
const result = generateRandomString(10);
console.log(result); // Output: 'a1b2c3d4e5' (example output)5. Slugifying Strings
Convert a string into a URL-friendly slug.
import { slugify } from '@js-utility/string';
const result = slugify('Hello World!');
console.log(result); // Output: 'hello-world'6. Escaping HTML
Escape HTML characters in a string.
import { escapeHTML } from '@js-utility/string';
const result = escapeHTML('<div>Hello</div>');
console.log(result); // Output: '<div>Hello</div>'