Skip to content

Chaisser/string-wizard

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧩 @chaisser/string-wizard

Advanced string manipulation with TypeScript type safety


✨ Features

  • 🎯 Type-safe - Full TypeScript support with no any types
  • πŸ” 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

πŸ“¦ Installation

npm install @chaisser/string-wizard
# or
yarn add @chaisser/string-wizard
# or
pnpm add @chaisser/string-wizard

πŸš€ Quick Start

import {
  // 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 **** **** 1111

πŸ“– What It Does

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


🎯 How It Works

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)

🎨 What It's Useful For

  • 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

πŸ’‘ Usage Examples

Basic Operations

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

Validation

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

Masking Sensitive Data

// 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 Generation

// 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 types

String Statistics

const 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, !)

Search & Replace

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

Regular Expressions

// 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' }

Encoding/Decoding

// 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%20world

πŸ”— Related Packages

Explore our other utility packages in the @chaisser namespace:


πŸ”’ License

MIT - Free to use in personal and commercial projects


πŸ‘¨ Developed by

Doruk Karaboncuk doruk.karaboncuk@interaktifis.com


πŸ“„ Repository


🀝 Contributing

Contributions are welcome! Feel free to:

  • Report bugs
  • Suggest new features
  • Submit pull requests
  • Improve documentation

πŸ“ž Support

For issues, questions, or suggestions, please reach out through:


Made with ❀️ by @chaisser

npm license downloads typescript

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors