strfi
v1.0.0
Published
High-performance string utilities for Node.js, browsers, and CLI applications
Downloads
82
Maintainers
Readme
strfi
High-performance string utilities for JavaScript/TypeScript. Works everywhere: Node.js, browsers, Deno, Bun, and CLI tools.
Author
Sepehr Mohseni
- GitHub: @sepehr-mohseni
- LinkedIn: sepehr-mohseni
- npm: sepehr-mohseni
Installation
npm install strfiFeatures
- Zero runtime dependencies
- Tree-shakeable (import only what you need)
- Full TypeScript support
- Works in Node.js, browsers, and edge runtimes
- Unicode-aware operations
- No
eval()or dynamic code execution - Minified bundle: ~22KB
Case Transformations
| Function | Description | Args |
|----------|-------------|------|
| toUpperCase | Converts to uppercase | str, options?: { locale } |
| toLowerCase | Converts to lowercase | str, options?: { locale } |
| capitalize | Capitalizes first character | str, options?: { locale } |
| uncapitalize | Lowercases first character | str, options?: { locale } |
| titleCase | Capitalizes first letter of each word | str, options?: { locale } |
| camelCase | Converts to camelCase | str |
| pascalCase | Converts to PascalCase | str |
| snakeCase | Converts to snake_case | str |
| kebabCase | Converts to kebab-case | str |
| constantCase | Converts to CONSTANT_CASE | str |
| dotCase | Converts to dot.case | str |
| pathCase | Converts to path/case | str |
| sentenceCase | Converts to Sentence case | str, options?: { locale } |
| swapCase | Swaps uppercase/lowercase | str |
| isUpperCase | Checks if all uppercase | str |
| isLowerCase | Checks if all lowercase | str |
| isTitleCase | Checks if title case | str |
Whitespace Operations
| Function | Description | Args |
|----------|-------------|------|
| trim | Removes whitespace from both ends | str |
| trimStart | Removes whitespace from start | str |
| trimEnd | Removes whitespace from end | str |
| trimChars | Removes specific chars from both ends | str, chars |
| trimCharsStart | Removes specific chars from start | str, chars |
| trimCharsEnd | Removes specific chars from end | str, chars |
| collapseWhitespace | Collapses multiple spaces to one | str |
| removeWhitespace | Removes all whitespace | str |
| padStart | Pads string at start | str, length, char? |
| padEnd | Pads string at end | str, length, char? |
| center | Centers string with padding | str, length, char? |
| repeat | Repeats string n times | str, count |
| insert | Inserts string at position | str, index, value |
| removeAt | Removes chars at position | str, index, count? |
| indent | Adds indentation to each line | str, spaces?, char? |
| dedent | Removes common leading whitespace | str |
String Manipulation
| Function | Description | Args |
|----------|-------------|------|
| reverse | Reverses string (Unicode-safe) | str |
| truncate | Truncates with ellipsis | str, options: { length, omission?, separator? } |
| truncateMiddle | Truncates middle with ellipsis | str, options: { length, omission? } |
| wordWrap | Wraps text at width | str, options: { width, indent?, cut? } |
| slugify | Creates URL slug | str, options?: { separator?, lowercase?, strict? } |
| between | Extracts string between delimiters | str, start, end |
| betweenAll | Extracts all strings between delimiters | str, start, end |
| remove | Removes all occurrences | str, search |
| replaceFirst | Replaces first occurrence | str, search, replacement |
| replaceLast | Replaces last occurrence | str, search, replacement |
| replaceAll | Replaces all occurrences | str, search, replacement |
| mask | Masks part of string | str, start, length?, char? |
| shuffle | Randomly shuffles characters | str |
| chunk | Splits into fixed-size chunks | str, size |
| splitBy | Splits by multiple delimiters | str, delimiters |
| join | Joins array with separator | arr, separator, lastSeparator? |
| stripTags | Removes HTML/XML tags | str |
| normalizeLineEndings | Normalizes line endings | str, ending? |
| surround | Wraps with prefix/suffix | str, wrapper |
| ensurePrefix | Adds prefix if missing | str, prefix |
| ensureSuffix | Adds suffix if missing | str, suffix |
| removePrefix | Removes prefix if present | str, prefix |
| removeSuffix | Removes suffix if present | str, suffix |
| lines | Splits into lines array | str |
| first | Gets first n characters | str, n? |
| last | Gets last n characters | str, n? |
String Validation
| Function | Description | Args |
|----------|-------------|------|
| isBlank | Checks if empty or whitespace only | str |
| isNotBlank | Checks if has non-whitespace content | str |
| isEmpty | Checks if string is empty | str |
| isNotEmpty | Checks if string is not empty | str |
| isAlpha | Checks if alphabetic only | str |
| isAlphanumeric | Checks if alphanumeric only | str |
| isNumeric | Checks if numeric only | str |
| isInteger | Checks if valid integer string | str |
| isDecimal | Checks if valid decimal string | str |
| isAscii | Checks if ASCII characters only | str |
| isPrintableAscii | Checks if printable ASCII only | str |
| isHexadecimal | Checks if valid hex string | str |
| isUUID | Checks if valid UUID format | str |
| isEmail | Checks if valid email format | str |
| isURL | Checks if valid URL | str |
| isJSON | Checks if valid JSON string | str |
| isIPv4 | Checks if valid IPv4 address | str |
| isIPv6 | Checks if valid IPv6 address | str |
| isCreditCard | Validates credit card (Luhn) | str |
| isSlug | Checks if valid URL slug | str |
| startsWith | Checks if starts with substring | str, search, position? |
| endsWith | Checks if ends with substring | str, search, position? |
| contains | Checks if contains substring | str, search, position? |
| containsAll | Checks if contains all substrings | str, searches |
| containsAny | Checks if contains any substring | str, searches |
| matches | Checks if matches regex | str, pattern |
| compare | Compares two strings | str1, str2, options?: { locale } |
| equals | Checks strict equality | str1, str2 |
| equalsIgnoreCase | Checks equality ignoring case | str1, str2 |
| isPalindrome | Checks if palindrome | str |
| isWhitespace | Checks if whitespace only | str |
| hasUpperCase | Checks if has uppercase | str |
| hasLowerCase | Checks if has lowercase | str |
| hasDigits | Checks if has digits | str |
| hasSpecialChars | Checks if has special characters | str |
Encoding & Escaping
| Function | Description | Args |
|----------|-------------|------|
| escapeHtml | Escapes HTML entities | str |
| unescapeHtml | Unescapes HTML entities | str |
| escapeXml | Escapes XML entities | str |
| unescapeXml | Unescapes XML entities | str |
| escapeRegex | Escapes regex special chars | str |
| escapeSqlLike | Escapes SQL LIKE wildcards | str, escapeChar? |
| escapeShell | Escapes for shell commands | str |
| toBase64 | Encodes to Base64 | str |
| fromBase64 | Decodes from Base64 | str |
| toBase64Url | Encodes to URL-safe Base64 | str |
| fromBase64Url | Decodes URL-safe Base64 | str |
| encodeUrl | URL encodes string | str |
| decodeUrl | URL decodes string | str |
| toHex | Converts to hex string | str |
| fromHex | Converts from hex string | str |
| toAsciiCodes | Gets ASCII codes array | str |
| fromAsciiCodes | Creates string from ASCII codes | codes |
| rot13 | Applies ROT13 cipher | str |
Search & Analysis
| Function | Description | Args |
|----------|-------------|------|
| countOccurrences | Counts substring occurrences | str, search, overlap? |
| countWords | Counts words | str |
| countLines | Counts lines | str |
| countSentences | Counts sentences | str |
| countParagraphs | Counts paragraphs | str |
| charFrequency | Gets character frequency map | str |
| wordFrequency | Gets word frequency map | str |
| indicesOf | Gets all indices of substring | str, search |
| nthIndexOf | Gets nth index of substring | str, search, n |
| extractNumbers | Extracts all numbers | str |
| extractWords | Extracts all words | str |
| extractEmails | Extracts email addresses | str |
| extractUrls | Extracts URLs | str |
| extractHashtags | Extracts hashtags | str |
| extractMentions | Extracts @mentions | str |
| levenshteinDistance | Calculates edit distance | str1, str2 |
| similarity | Calculates similarity (0-1) | str1, str2 |
| longestCommonSubstring | Finds longest common substring | str1, str2 |
| longestCommonPrefix | Finds common prefix | strings[] |
| longestCommonSuffix | Finds common suffix | strings[] |
Unicode & i18n
| Function | Description | Args |
|----------|-------------|------|
| normalize | Normalizes Unicode form | str, form? |
| removeDiacritics | Removes accents/diacritics | str |
| toCodePoints | Gets Unicode code points | str |
| fromCodePoints | Creates string from code points | codePoints |
| graphemeLength | Gets grapheme cluster count | str |
| toGraphemes | Splits into grapheme clusters | str |
| toWords | Splits into words (locale-aware) | str, locale? |
| isLetter | Checks if Unicode letter | char, locale? |
| isDigit | Checks if Unicode digit | char |
| isSpace | Checks if Unicode space | char |
| isPunctuation | Checks if Unicode punctuation | char |
| isRTL | Checks if right-to-left text | str |
| isLTR | Checks if left-to-right text | str |
| getDirection | Gets text direction (ltr/rtl/neutral) | str |
| formatNumber | Formats number for locale | num, locale?, options? |
| formatDate | Formats date for locale | date, locale?, options? |
| formatCurrency | Formats currency for locale | amount, currency, locale? |
| formatRelativeTime | Formats relative time | value, unit, locale? |
| pluralize | Selects plural form | count, forms, locale? |
| collate | Sorts strings (locale-aware) | strings, locale?, options? |
| getLocaleDisplayName | Gets locale display name | locale, displayLocale? |
| getRegionDisplayName | Gets region display name | region, displayLocale? |
Formatting
| Function | Description | Args |
|----------|-------------|------|
| template | Interpolates template string | str, values, options? |
| format | Formats with positional args | str, ...args |
| formatNamed | Formats with named args | str, values |
| sprintf | C-style sprintf | format, ...args |
| zeroPad | Pads number with zeros | num, length |
| formatThousands | Adds thousand separators | num, separator? |
| formatBytes | Formats bytes (KB, MB, etc.) | bytes, decimals? |
| formatDuration | Formats duration in seconds | seconds |
| ordinal | Creates ordinal (1st, 2nd, etc.) | num |
| loremIpsum | Generates lorem ipsum text | words |
| randomString | Generates random string | length, options? |
| uuid | Generates UUID v4 | none |
| hashCode | Generates hash code | str |
Platform Support
| Platform | Supported | |----------|-----------| | Node.js 16+ | ✅ | | Chrome/Edge | ✅ | | Firefox | ✅ | | Safari | ✅ | | Deno | ✅ | | Bun | ✅ | | React Native | ✅ | | Cloudflare Workers | ✅ |
Usage
ESM (Browser/Node.js)
import { slugify, escapeHtml } from 'strfi';CommonJS (Node.js)
const { slugify, escapeHtml } = require('strfi');CLI Scripts
#!/usr/bin/env node
import { slugify } from 'strfi';
console.log(slugify(process.argv[2]));License
MIT © Sepehr Mohseni
