Advanced string manipulation with TypeScript type safety
- π― Type-safe - Full TypeScript support with no
anytypes - π Pattern matching - Built-in regex patterns for common string operations
- π§½ Case conversion - Multiple case styles (camel, kebab, snake, pascal, etc.)
- βοΈ Validation - Email, URL, phone, UUID validators
- π€ Encoding/Decoding - Base64, URL encode/decode
- π Masking - Sensitive data masking (emails, phones, credit cards)
- π§½ Truncation - Smart string truncation with ellipsis
- π Statistics - Word count, character count, reading time
- π§ Search & Replace - Multiple search strategies (includes, startsWith, endsWith, indexOf)
- πͺ Normalization - Trim, collapse whitespace, remove diacritics
- π Templates - String interpolation and template literals
- π Transformations - Reverse, shuffle, rotate, slice operations
- π§° Generation - Random strings, UUIDs, tokens, unique strings
- π§Ύ Comparisons - Levenshtein distance, similarity, diff
- π¬ Regular Expressions - Build, test, and manipulate regex patterns
- π¨ Color Operations - Parse, validate, manipulate hex colors
npm install @chaisser/string-wizard
# or
yarn add @chaisser/string-wizard
# or
pnpm add @chaisser/string-wizardimport {
// Basic operations
capitalize,
lowerCase,
upperCase,
trim,
isEmpty,
// Case conversion
camelCase,
kebabCase,
snakeCase,
pascalCase,
// Validation
isEmail,
isURL,
isPhone,
isUUID,
// Masking
maskEmail,
maskPhone,
maskCreditCard,
// Generation
randomString,
randomUUID,
// Statistics
wordCount,
characterCount,
readingTime
} from '@chaisser/string-wizard';
// Basic operations
const str = 'hello world';
console.log(capitalize(str)); // Hello World
console.log(trim(str)); // hello world
// Validation
console.log(isEmail('test@example.com')); // true
console.log(isUUID('550e8400-e29b-41d4-a716-446655440144')); // true
// Masking
console.log(maskEmail('user@example.com')); // u***@example.com
console.log(maskCreditCard('4111111111111111', { showLastFour: true })); // 4111 **** **** 1111This package provides a comprehensive toolkit for string manipulation in JavaScript and TypeScript. It handles everything from basic string operations to advanced regex-based pattern matching, validation, masking, and generation.
The package is organized into modules, each handling a specific aspect of string manipulation:
- Validation - Check if strings match expected patterns (emails, URLs, phones, UUIDs)
- Case Conversion - Convert between different case formats (camel, kebab, snake, pascal)
- Masking - Hide sensitive information while preserving format
- Generation - Create random strings, tokens, and UUIDs
- Statistics - Analyze strings (word count, character count, reading time)
- Search & Replace - Find and replace content in strings
- Encoding/Decoding - Encode/decode Base64 and URLs
- Regular Expressions - Build, test, and manipulate regex patterns
- Transformations - Manipulate strings (reverse, shuffle, rotate, slice operations)
- Form Validation - Validate user inputs (emails, phone numbers, postal codes)
- Data Privacy - Mask sensitive data (credit cards, social security numbers)
- Text Processing - Case conversion, trimming, normalization
- API Development - Slug generation, key creation, parameter naming
- Security - Password strength checking, secure random token generation
- UI/UX - Display formatted names, mask sensitive information
- Data Analysis - Analyze text content (word count, reading time)
- Testing - Generate test data (random strings, UUIDs)
- Internationalization - Case conversion for different languages
// Case conversion
const text = 'hello world';
console.log(camelCase(text)); // helloWorld
console.log(kebabCase(text)); // hello-world
console.log(snakeCase(text)); // hello_world
console.log(pascalCase(text)); // HelloWorld
console.log(upperCase(text)); // HELLO WORLD
console.log(lowerCase(text)); // hello world
console.log(capitalize(text)); // Hello world
// Trimming and cleaning
const messy = ' hello world ';
console.log(trim(messy)); // hello world
console.log(collapseWhitespace(messy)); // hello world
console.log(normalizeAccents('CafΓ©')); // Cafe// Email validation
console.log(isEmail('test@example.com')); // true
console.log(isEmail('invalid')); // false
console.log(isEmail('user.name@domain')); // true (with TLD)
// Phone validation
console.log(isPhone('+1-555-123-4567')); // true
console.log(isPhone('123456')); // true (basic format)
console.log(isPhone('abc')); // false
// UUID validation
console.log(isUUID('550e8400-e29b-41d4-a716-446655440144')); // true (v4)
console.log(isUUID('00000000-0000-0000-000000000000')); // true (nil)
console.log(isUUID('not-a-uuid')); // false
// URL validation
console.log(isURL('https://example.com')); // true
console.log(isURL('example.com')); // false
console.log(isURL('ftp://example.com')); // true
// Postal code validation
console.log(isPostalCode('12345')); // true
console.log(isPostalCode('1234')); // true// Email masking
console.log(maskEmail('user@example.com')); // u***@example.com
console.log(maskEmail('user.name@domain')); // u*****@domain.com
console.log(maskEmail('long.email@company.com')); // l***@company.com
// Phone masking
console.log(maskPhone('+1-555-123-4567')); // +1-555-***-****
console.log(maskPhone('+1-800-555-1234')); // +1-800-***-***
// Credit card masking
console.log(maskCreditCard('4111111111111111', { showLastFour: true })); // 4111 **** **** 1111
console.log(maskCreditCard('4111111111111111')); // **** **** **** ****
// SSN masking
console.log(maskSSN('123-45-6789')); // ***-**-****// Random string
console.log(randomString(10)); // Random 10-character string
console.log(randomString(10, { lowercase: true, numbers: true })); // Random 10-char with lower+nums
// UUID generation
console.log(randomUUID()); // Random UUID v4
console.log(randomUUID('v7')); // Random UUID v7
// Custom alphabet
console.log(randomString(10, { alphabet: 'abcdef' })); // Random from custom charset
console.log(randomString(10, { lowercase: true, uppercase: true, numbers: true, symbols: true })); // Mix all typesconst text = 'Hello World! This is a test string.';
// Character count
console.log(characterCount(text)); // { length: 34, letters: 24, digits: 2, spaces: 7, symbols: 1 }
// Word count
console.log(wordCount(text)); // 8
console.log(wordCount('The quick brown fox jumps over the lazy dog')); // 9
// Letter frequency
console.log(letterFrequency(text)); // { H: 1, e: 3, l: 3, o: 4, r: 1, d: 1, t: 2, W: 1, '!': 2, T: 1, s: 3, i: 2, a: 1, h: 1 }
// Unique characters
console.log(uniqueCharacters(text)); // 8 (H, e, l, o, r, d, t, W, !)const text = 'hello world hello world';
// Find operations
console.log(contains(text, 'hello')); // true
console.log(startsWith(text, 'hello')); // true
console.log(endsWith(text, 'world')); // true
console.log(includes(text, 'world')); // true
// Index operations
console.log(indexOf(text, 'world')); // 6 (first occurrence)
console.log(lastIndexOf(text, 'world')); // 18 (last occurrence)
// Replace operations
console.log(replaceAll(text, 'hello', 'hi')); // hi world hi world
console.log(replaceAt(text, 0, 'HI')); // HIello world hello world
console.log(replaceRange(text, 0, 5, 'HEY')); // HEY world hello world// Test regex patterns
console.log(testEmailRegex('test@example.com')); // true
console.log(testURLRegex('https://example.com')); // true
console.log(testPhoneRegex('+1-555-123-4567')); // true
// Build regex from components
const pattern = buildRegex('hello', { anyChar: true, digits: true });
console.log(testRegex(pattern, 'h3llo777')); // true (h = any, 3 = digit, etc.)
// Extract data from regex
const email = 'contact@example.com';
const { match, groups } = extractEmailRegex(email);
console.log(match); // contact@example.com
console.log(groups); // { domain: 'example.com', local: 'contact' }// Base64
const original = 'hello world';
const encoded = toBase64(original);
console.log(encoded); // aGVsbG8gd29yb3Js=
const decoded = fromBase64(encoded);
console.log(decoded); // hello world
// URL encode/decode
const url = 'https://example.com/search?q=hello world';
const encodedURL = encodeURL(url);
console.log(encodedURL); // https://example.com/search?q=hello%20world
const decodedURL = decodeURL(encodedURL);
console.log(decodedURL); // https://example.com/search?q=hello world
// Encode URI component
const encodedComponent = encodeURIComponent('hello world');
console.log(encodedComponent); // hello%20worldExplore our other utility packages in the @chaisser namespace:
- @chaisser/human-time - Human-readable time formatting
- @chaisser/obj-path - Object path traversal
- @chaisser/regex-humanizer - Regex pattern explanation
- @chaisser/debounce-throttle - Rate limiting utilities
- @chaisser/color-utils - Color conversion utilities
- @chaisser/deep-clone - Deep cloning functions
- @chaisser/array-group-by - Array grouping utilities
- @chaisser/uuid-v7 - UUID v7 generator
- @chaisser/wait-for - Promise-based wait utilities
- @chaisser/password-strength - Password strength checker
- @chaisser/merge-objects - Object merge utilities
- @chaisser/chunk-array - Array chunking functions
- @chaisser/string-wizard (this package) - Advanced string manipulation
MIT - Free to use in personal and commercial projects
Doruk Karaboncuk doruk.karaboncuk@interaktifis.com
- GitHub: @chaisser
- NPM: @chaisser/string-wizard
Contributions are welcome! Feel free to:
- Report bugs
- Suggest new features
- Submit pull requests
- Improve documentation
For issues, questions, or suggestions, please reach out through:
- Email: doruk.karaboncuk@interaktifis.com
- GitHub Issues: Create an issue
Made with β€οΈ by @chaisser