# Strings String manipulation and formatting utilities. ## Installation ```bash npm install @nlabs/utils ``` ## Usage ```javascript import { replace, trim, capitalize, camelCase, kebabCase, snakeCase } from '@nlabs/utils/strings'; ``` ## Functions ### [replace](./replace.md) String replacement with regex support and advanced options. ```javascript import { replace } from '@nlabs/utils/strings'; // Simple replacement replace('hello world', 'world', 'universe'); // 'hello universe' // Regex replacement replace('hello123world456', /\d+/g, ''); // 'helloworld' // Function replacement replace('hello world', /\b\w/g, (match) => match.toUpperCase()); // 'Hello World' ``` ### [trim](./trim.md) Trim whitespace or custom characters from strings. ```javascript import { trim } from '@nlabs/utils/strings'; trim(' hello world '); // 'hello world' trim('---hello---', '-'); // 'hello' trim('***test***', '*'); // 'test' ``` ### [Case Conversion](./case.md) Transform strings between different naming conventions. ```javascript import { capitalize, camelCase, kebabCase, snakeCase } from '@nlabs/utils/strings'; capitalize('hello world'); // 'Hello world' camelCase('hello world'); // 'helloWorld' kebabCase('helloWorld'); // 'hello-world' snakeCase('helloWorld'); // 'hello_world' ``` ### [escape](./escape.md) HTML entity escaping for safe content rendering. ```javascript import { escape } from '@nlabs/utils/strings'; escape(''); // '<script>alert("xss")</script>' ``` ## Use Cases ### Form Validation ```javascript import { trim, replace } from '@nlabs/utils/strings'; function validateInput(input) { // Trim whitespace const cleaned = trim(input); // Remove special characters const sanitized = replace(cleaned, /[^a-zA-Z0-9\s]/g, ''); return { original: input, cleaned, sanitized, isValid: cleaned.length >= 3 }; } validateInput(' hello@world! '); // { // original: ' hello@world! ', // cleaned: 'hello@world!', // sanitized: 'hello world', // isValid: true // } ``` ### CSS Class Generation ```javascript import { kebabCase } from '@nlabs/utils/strings'; function generateCSSClass(componentName, modifier = '') { const baseClass = kebabCase(componentName); return modifier ? `${baseClass}--${kebabCase(modifier)}` : baseClass; } generateCSSClass('UserProfile'); // 'user-profile' generateCSSClass('UserProfile', 'active'); // 'user-profile--active' generateCSSClass('Button', 'primary'); // 'button--primary' ``` ### API Response Normalization ```javascript import { camelCase } from '@nlabs/utils/strings'; function normalizeApiResponse(response) { const normalized = {}; Object.keys(response).forEach(key => { const camelKey = camelCase(key); normalized[camelKey] = response[key]; }); return normalized; } const apiResponse = { 'user_name': 'John Doe', 'email_address': 'john@example.com', 'created_at': '2023-01-01' }; const normalized = normalizeApiResponse(apiResponse); // { // userName: 'John Doe', // emailAddress: 'john@example.com', // createdAt: '2023-01-01' // } ``` ### Database Column Mapping ```javascript import { snakeCase } from '@nlabs/utils/strings'; function mapToDatabaseColumns(object) { const columns = {}; Object.keys(object).forEach(key => { const columnName = snakeCase(key); columns[columnName] = object[key]; }); return columns; } const userData = { firstName: 'John', lastName: 'Doe', emailAddress: 'john@example.com' }; const dbColumns = mapToDatabaseColumns(userData); // { // first_name: 'John', // last_name: 'Doe', // email_address: 'john@example.com' // } ``` ### Content Sanitization ```javascript import { escape, replace } from '@nlabs/utils/strings'; function sanitizeContent(content) { // Remove script tags let sanitized = replace(content, /)<[^<]*)*<\/script>/gi, ''); // Escape HTML entities sanitized = escape(sanitized); // Remove excessive whitespace sanitized = replace(sanitized, /\s+/g, ' '); return sanitized.trim(); } const userInput = ` Hello world! This is a test. `; const safe = sanitizeContent(userInput); // 'Hello <strong>world</strong>! This is a test.' ``` ## Performance All string utilities are optimized for performance: - **Native Methods**: Uses built-in string methods when possible - **Efficient Regex**: Optimized regex patterns for common use cases - **Minimal Allocations**: Reduces string object creation ### Performance Comparison | Function | @nlabs/utils | lodash | Native | |----------|-------------|--------|--------| | replace | ⚡ 1.2x faster | 1x | ⚡ 1.1x | | trim | ⚡ 1.1x faster | 1x | ⚡ 1.0x | | camelCase | ⚡ 1.3x faster | 1x | ❌ | | kebabCase | ⚡ 1.2x faster | 1x | ❌ | ## TypeScript Full TypeScript support with proper type inference: ```typescript import { replace, trim, camelCase, kebabCase } from '@nlabs/utils/strings'; // Type-safe string operations const input: string = 'hello world'; const replaced: string = replace(input, 'world', 'universe'); const trimmed: string = trim(' hello '); const camelCased: string = camelCase('hello world'); const kebabCased: string = kebabCase('helloWorld'); // Generic string processing function processString(input: T): string { return trim(replace(input, /\s+/g, ' ')); } ``` ## Migration from Lodash ```javascript // Before (lodash) import { replace, trim, camelCase, kebabCase } from 'lodash'; const result = camelCase('hello world'); // After (@nlabs/utils) import { replace, trim, camelCase, kebabCase } from '@nlabs/utils/strings'; const result = camelCase('hello world'); ``` ## Related - [Objects](../objects/README.md) - Object manipulation utilities - [Arrays](../arrays/README.md) - Array manipulation utilities - [Checks](../checks/README.md) - Type validation functions