A lightweight, zero-dependency NPM Package for elegant string manipulations. It provides a comprehensive range of text utilities for JavaScript and Node.js applications including transformations, validations, analysis, and formatting.
✨ Features
- 💪 Powerful - Transform, validate, analyze, and format strings with minimal code
- 🪶 Lightweight - Zero dependencies, tiny footprint
- 🧩 Modular - Import only what you need with organized namespaces
- 🚀 Fast - Optimized for performance
- ✅ Tested - Reliable and robust
- 🎯 Comprehensive - 4 specialized modules for all string needs
📦 Installation
# Using npm
npm install stringzy
# Using yarn
yarn add stringzy
# Using pnpm
pnpm add stringzy
🚀 Quick Start
// Import the entire library
import stringzy from 'stringzy';
// Or import specific functions
import { toUpperCase, isEmail, wordCount, formatPhone } from 'stringzy';
// Or import by namespace
import { transform, validate, analyze, format } from 'stringzy';
// Transform your strings
const slug = stringzy.toSlug('Hello World!'); // 'hello-world'
const isValid = stringzy.validate.isEmail('user@example.com'); // true
const count = stringzy.analyze.wordCount('Hello world'); // 2
📋 Table of Contents
Transformations
- truncateText - Truncates text to a specified maximum length
- toSlug - Converts a string to a URL-friendly slug
- capitalizeWords - Capitalizes the first letter of each word
- removeSpecialChars - Removes special characters from a string
- removeWords - Removes specified words from a string
- removeDuplicates - Removes specified words from a string
- initials - Extracts initials from a text string
- camelCase - Converts the given string to Camel Case
- pascaslCase - Converts the given string to Pascal Case
- snakeCase - Converts the given string to Snake Case
- kebabCase - Converts the given string to Kebab Case
- titleCase - Converts the given string to Title Case
- constantCase - Converts the given string to Constant Case
Validations
- isURL - Checks if a string is a valid URL
- isEmail - Checks if a string is a valid email address
- isDate - Checks if a string is a valid date
- isEmpty - Checks if a string is empty or contains only whitespace
Analysis
- wordCount - Counts the number of words in a string
- characterCount - Counts the number of characters in a string
- characterFrequency - Analyzes character frequency in a string
Formatting
- capitalize - Capitalizes the first letter of each word
- formatNumber - Formats a number string with thousand separators
- formatPhone - Formats a phone number string to standard format
📋 API Reference
🔄 Transformations
Functions for transforming and manipulating strings.
truncateText(text, maxLength, suffix = '...')
Truncates text to a specified maximum length, adding a suffix if truncated.
import { truncateText } from 'stringzy';
truncateText('This is a long sentence that needs truncating', 10);
// Returns: 'This is a...'
truncateText('This is a long sentence', 10, ' →');
// Returns: 'This is a →'
truncateText('Short', 10);
// Returns: 'Short' (no truncation needed)
Parameter | Type | Default | Description |
---|---|---|---|
text | string | required | The input string to truncate |
maxLength | number | required | Maximum length of the output string (excluding suffix) |
suffix | string | '...' | String to append if truncation occurs |
toSlug(text)
Converts a string to a URL-friendly slug.
import { toSlug } from 'stringzy';
toSlug('Hello World!');
// Returns: 'hello-world'
toSlug('This is a TEST string 123');
// Returns: 'this-is-a-test-string-123'
toSlug('Special $#@! characters');
// Returns: 'special-characters'
Parameter | Type | Default | Description |
---|---|---|---|
text | string | required | The input string to convert to a slug |
capitalizeWords(text)
Capitalizes the first letter of each word in a string.
import { capitalizeWords } from 'stringzy';
capitalizeWords('hello world');
// Returns: 'Hello World'
capitalizeWords('javascript string manipulation');
// Returns: 'Javascript String Manipulation'
capitalizeWords('already Capitalized');
// Returns: 'Already Capitalized'
Parameter | Type | Default | Description |
---|---|---|---|
text | string | required | The input string to capitalize |
removeSpecialChars(text, replacement = '')
Removes special characters from a string, optionally replacing them.
import { removeSpecialChars } from 'stringzy';
removeSpecialChars('Hello, world!');
// Returns: 'Hello world'
removeSpecialChars('email@example.com');
// Returns: 'emailexamplecom'
removeSpecialChars('Phone: (123) 456-7890', '-');
// Returns: 'Phone-123-456-7890'
Parameter | Type | Default | Description |
---|---|---|---|
text | string | required | The input string to process |
replacement | string | '' | String to replace special characters with |
removeWords(text, wordsToRemove)
Removes specified words from a string
import { removeWords } from 'stringzy';
removeWords('Hello world this is a test', ['this', 'is']);
// Returns: 'Hello world a test'
removeWords('Remove The Quick BROWN fox', ['the', 'brown']);
// Returns: 'Remove Quick fox'
removeWords('JavaScript is awesome and JavaScript rocks', ['JavaScript']);
// Returns: 'is awesome and rocks'
Parameter | Type | Default | Description |
---|---|---|---|
text | string | required | The input string to process |
wordsToRemove | string[] | required | Array of words to remove from the string |
removeDuplicates(text)
Removes duplicate case-sensitive words from a given text.
import { removeDuplicates } from 'stringzy';
removeDuplicates('Hello world this is a is a test');
// Returns: 'Hello world this is a test'
removeDuplicates('Remove me me me me or Me');
// Returns: 'Remove me or Me''
removeDuplicates('JavaScript is not bad and not awesome');
// Returns: 'JavaScript is not bad and awesome'
Parameter | Type | Default | Description |
---|---|---|---|
text | string | required | The input string to process |
initials(text, limit)
Extracts initials from a text string.
import { initials } from 'stringzy';
initials('John Doe');
// Returns: 'JD'
initials('Alice Bob Charlie', 2);
// Returns: 'AB'
initials('Hello World Test Case');
// Returns: 'HWTC'
initials('single');
// Returns: 's'
initials(' Multiple Spaces Between ');
// Returns: 'MSB'
Parameter | Type | Default | Description |
---|---|---|---|
text | string | required | The input string to extract initials from |
limit | number | undefined | Maximum number of initials to return (optional) |
camelCase(text)
Converts the given string to Camel Case.
import { camelCase } from 'stringzy';
camelCase('hello world'); // 'helloWorld'
camelCase('this is a test'); // 'thisIsATest'
Parameter | Type | Default | Description |
---|---|---|---|
text | string | required | The input string to convert to Camel Case |
pascalCase(text)
Converts the given string to Pascal Case.
import { pascalCase } from 'stringzy';
pascalCase('hello world'); // 'HelloWorld'
pascalCase('this is a test'); // 'ThisIsATest'
Parameter | Type | Default | Description |
---|---|---|---|
text | string | required | The input string to convert to Pascal Case |
snakeCase(text)
Converts the given string to Snake Case.
import { snakeCase } from 'stringzy';
snakeCase('hello world'); // 'hello_world'
snakeCase('this is a test'); // 'this_is_a_test'
Parameter | Type | Default | Description |
---|---|---|---|
text | string | required | The input string to convert to Snake Case |
kebabCase(text)
Converts the given string to Kebab Case.
import { kebabCase } from 'stringzy';
kebabCase('hello world'); // 'hello-world'
kebabCase('this is a test'); // 'this-is-a-test'
Parameter | Type | Default | Description |
---|---|---|---|
text | string | required | The input string to convert to Kebab Case |
titleCase(text)
Converts the given string to Title Case.
import { titleCase } from 'stringzy';
titleCase('hello world'); // 'Hello World'
titleCase('this is a test'); // 'This Is A Test'
Parameter | Type | Default | Description |
---|---|---|---|
text | string | required | The input string to convert to Title Case |
constantCase(text)
Converts the given string to Constant Case.
import { constantCase } from 'stringzy';
constantCase('hello world'); // 'HELLO_WORLD'
constantCase('this is a test'); // 'THIS_IS_A_TEST'
Parameter | Type | Default | Description |
---|---|---|---|
text | string | required | The input string to convert to Constant Case |
✅ Validations
Functions for validating string formats and content.
isURL(text)
Checks if a string is a valid URL.
isURL('https://example.com'); // true
isURL('not-a-url'); // false
Parameter | Type | Default | Description |
---|---|---|---|
text | string | required | The input string to validate as URL |
isEmail(text)
Checks if a string is a valid email address.
isEmail('user@example.com'); // true
isEmail('invalid-email'); // false
Parameter | Type | Default | Description |
---|---|---|---|
text | string | required | The input string to validate as email |
isDate(text)
Checks if a string is a valid date.
import { isDate } from 'stringzy';
isDate('2023-12-25'); // true
isDate('12/25/2023'); // true
isDate('invalid-date'); // false
isDate('2023-13-45'); // false
Parameter | Type | Default | Description |
---|---|---|---|
text | string | required | The input string to validate as date |
isEmpty(text)
Checks if a string is empty or contains only whitespace.
isEmpty(' '); // true
isEmpty('hello'); // false
Parameter | Type | Default | Description |
---|---|---|---|
text | string | required | The input string to check for emptiness |
📊 Analysis
Functions for analyzing string content and structure.
wordCount(text)
Counts the number of words in a string.
wordCount('Hello world'); // 2
wordCount(''); // 0
Parameter | Type | Default | Description |
---|---|---|---|
text | string | required | The input string to count words in |
characterCount(text)
Counts the number of characters in a string.
characterCount('Hello'); // 5
Parameter | Type | Default | Description |
---|---|---|---|
text | string | required | The input string to count characters in |
characterFrequency(text)
Analyzes character frequency in a string (excluding spaces).
characterFrequency('hello'); // { h: 1, e: 1, l: 2, o: 1 }
Parameter | Type | Default | Description |
---|---|---|---|
text | string | required | The input string to analyze character frequency |
🎨 Formatting
Functions for formatting strings into specific patterns.
capitalize(text)
Capitalizes the first letter of each word.
capitalize('hello world'); // 'Hello World'
capitalize('javaScript programming'); // 'Javascript Programming'
Parameter | Type | Default | Description |
---|---|---|---|
text | string | required | The input string to capitalize |
formatNumber(number, separator = ',')
Formats a number string with thousand separators.
formatNumber('1234567'); // '1,234,567'
formatNumber('1234567', '.'); // '1.234.567'
Parameter | Type | Default | Description |
---|---|---|---|
number | string|number | required | The number to format |
separator | string | ',' | The separator to use for thousands |
formatPhone(phone, format = 'us')
Formats a phone number string to standard format.
formatPhone('1234567890'); // '(123) 456-7890'
formatPhone('11234567890', 'international'); // '+1 (123) 456-7890'
Parameter | Type | Default | Description |
---|---|---|---|
phone | string | required | The phone number string to format |
format | string | 'us' | Format type: 'us' or 'international' |
🔧 Usage Patterns
Individual Function Imports
import { isEmail, wordCount, capitalize } from 'stringzy';
const email = 'user@example.com';
if (isEmail(email)) {
console.log('Valid email!');
}
Namespace Imports
import { validate, analyze, format } from 'stringzy';
// Organized by functionality
const emailValid = validate.isEmail('test@example.com');
const words = analyze.wordCount('Hello world');
const formatted = format.capitalize('hello world');
Default Import (All Functions)
import stringzy from 'stringzy';
// Access any function
stringzy.toUpperCase('hello');
stringzy.validate.isEmail('test@example.com');
stringzy.analyze.wordCount('Hello world');
stringzy.format.capitalize('hello world');
🛠️ Usage Examples
In a React component
import React from 'react';
import { truncate, capitalize, wordCount, isEmpty } from 'stringzy';
function ArticlePreview({ title, content }) {
const displayTitle = isEmpty(title) ? 'Untitled' : capitalize(title);
const previewText = truncate(content, 150);
const readingTime = Math.ceil(wordCount(content) / 200);
return (
<div className="article-preview">
<h2>{displayTitle}</h2>
<p>{previewText}</p>
<small>{readingTime} min read</small>
</div>
);
}
Form Validation
import { validate } from 'stringzy';
function validateForm(formData) {
const errors = {};
if (!validate.isEmail(formData.email)) {
errors.email = 'Please enter a valid email address';
}
if (!validate.isURL(formData.website)) {
errors.website = 'Please enter a valid URL';
}
if (validate.isEmpty(formData.name)) {
errors.name = 'Name is required';
}
return errors;
}
Content Analysis Dashboard
import { analyze } from 'stringzy';
function getContentStats(text) {
return {
words: analyze.wordCount(text),
characters: analyze.characterCount(text),
frequency: analyze.characterFrequency(text),
readingTime: Math.ceil(analyze.wordCount(text) / 200)
};
}
Data Formatting
import { format } from 'stringzy';
function formatUserData(userData) {
return {
name: format.capitalize(userData.name),
phone: format.formatPhone(userData.phone),
revenue: format.formatNumber(userData.revenue)
};
}
🔄 TypeScript Support
The package includes TypeScript type definitions for all functions.
import { validate, analyze, format } from 'stringzy';
// TypeScript will provide proper type checking
const isValid: boolean = validate.isEmail('test@example.com');
const count: number = analyze.wordCount('Hello world');
const formatted: string = format.capitalize('hello world');
🏗️ Architecture
stringzy is organized into four specialized modules:
transformations.js
- Core string transformationsvalidations.js
- String validation utilitiesanalysis.js
- String analysis and metricsformatting.js
- String formatting functions
Each module can be imported individually or accessed through the main entry point.
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Contributors
Samarth Ruia |
John Cervantes |
Hardik Srivastav |
Ahmed Semih Erkan |
Michael van der Bend |
📝 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- Thank you to all contributors and users of this package!
- Inspired by the need for comprehensive yet simple string manipulation utilities.
If you have contributed to this project and your image is not here, please let us know, and we'll be happy to add it!
Made with ❤️ by Samarth Ruia