DEV Community

Cover image for Introducing strfi: The Ultimate String Utility Library for JavaScript & TypeScript
Sepehr Mohseni
Sepehr Mohseni

Posted on • Edited on

Introducing strfi: The Ultimate String Utility Library for JavaScript & TypeScript

I'm thrilled to announce strfi – a comprehensive, zero-dependency, tree-shakeable collection of over 150 pure string utilities for modern JavaScript and TypeScript projects.

Tired of pulling in heavy libraries for simple string tasks like slugifying text, escaping HTML, calculating edit distance, or formatting bytes? strfi is here to be your lightweight, yet incredibly powerful string toolkit.

Key Highlights

  • Zero runtime dependencies
  • Fully tree-shakeable – import only the functions you need
  • Excellent TypeScript support with precise types
  • Unicode-aware and locale-sensitive where applicable
  • No unsafe practices (no eval(), etc.)
  • Tiny minified bundle: ~22KB
  • Works everywhere: Node.js 16+, modern browsers, Deno, Bun, React Native, Cloudflare Workers

Installation

npm install strfi
Enter fullscreen mode Exit fullscreen mode

Or with yarn/pnpm:

yarn add strfi
# or
pnpm add strfi
Enter fullscreen mode Exit fullscreen mode

Quick Usage Examples:

import { 
  slugify, 
  escapeHtml, 
  capitalize, 
  levenshteinDistance, 
  formatBytes,
  camelCase,
  truncate,
  isEmail
} from 'strfi';

console.log(slugify('Hello World! 🚀'));           // "hello-world"
console.log(escapeHtml('<script>alert("xss")</script>')); // "&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;"
console.log(capitalize('javascript'));             // "Javascript"
console.log(levenshteinDistance('kitten', 'sitting')); // 3
console.log(formatBytes(1500000));                 // "1.43 MB"
console.log(camelCase('foo-bar_baz'));             // "fooBarBaz"
console.log(truncate('Very long text here', { length: 10 })); // "Very lo..."
console.log(isEmail('test@example.com'));          // true

Enter fullscreen mode Exit fullscreen mode

All Features – Grouped by Category
strfi provides an extensive set of utilities, organized into clear categories:

Case Transformations

toUpperCase, toLowerCase, capitalize, uncapitalize, titleCase, camelCase, pascalCase, snakeCase, kebabCase, constantCase, dotCase, pathCase, sentenceCase, swapCase, isUpperCase, isLowerCase, isTitleCase
Enter fullscreen mode Exit fullscreen mode

Whitespace Operations

trim, trimStart, trimEnd, trimChars, trimCharsStart, trimCharsEnd, collapseWhitespace, removeWhitespace, padStart, padEnd, center, repeat, insert, removeAt, indent, dedent
Enter fullscreen mode Exit fullscreen mode

String Manipulation

reverse, truncate, truncateMiddle, wordWrap, slugify, between, betweenAll, remove, replaceFirst, replaceLast, replaceAll, mask, shuffle, chunk, splitBy, join, stripTags, normalizeLineEndings, surround, ensurePrefix, ensureSuffix, removePrefix, removeSuffix, lines, first, last
Enter fullscreen mode Exit fullscreen mode

String Validation

isBlank, isNotBlank, isEmpty, isNotEmpty, isAlpha, isAlphanumeric, isNumeric, isInteger, isDecimal, isAscii, isPrintableAscii, isHexadecimal, isUUID, isEmail, isURL, isJSON, isIPv4, isIPv6, isCreditCard, isSlug, startsWith, endsWith, contains, containsAll, containsAny, matches, compare, equals, equalsIgnoreCase, isPalindrome, isWhitespace, hasUpperCase, hasLowerCase, hasDigits, hasSpecialChars
Enter fullscreen mode Exit fullscreen mode

Encoding & Escaping

escapeHtml, unescapeHtml, escapeXml, unescapeXml, escapeRegex, escapeSqlLike, escapeShell, toBase64, fromBase64, toBase64Url, fromBase64Url, encodeUrl, decodeUrl, toHex, fromHex, toAsciiCodes, fromAsciiCodes, rot13
Enter fullscreen mode Exit fullscreen mode

Search & Analysis

countOccurrences, countWords, countLines, countSentences, countParagraphs, charFrequency, wordFrequency, indicesOf, nthIndexOf, extractNumbers, extractWords, extractEmails, extractUrls, extractHashtags, extractMentions, levenshteinDistance, similarity, longestCommonSubstring, longestCommonPrefix, longestCommonSuffix
Enter fullscreen mode Exit fullscreen mode

Unicode & i18n

normalize, removeDiacritics, toCodePoints, fromCodePoints, graphemeLength, toGraphemes, toWords, isLetter, isDigit, isSpace, isPunctuation, isRTL, isLTR, getDirection, formatNumber, formatDate, formatCurrency, formatRelativeTime, pluralize, collate, getLocaleDisplayName, getRegionDisplayName
Enter fullscreen mode Exit fullscreen mode

Formatting & Utilities

template, format, formatNamed, sprintf, zeroPad, formatThousands, formatBytes, formatDuration, ordinal, loremIpsum, randomString, uuid, hashCode
Enter fullscreen mode Exit fullscreen mode

What's Next?
This is v1.0.0 – packed with features from day one! I'm planning more i18n enhancements, performance optimizations, and community-driven utilities.
Contributions, bug reports, and feature requests are very welcome!
Links

npm: https://www.npmjs.com/package/strfi
GitHub Profile: https://github.com/sepehr-mohseni/strfi

If you give strfi a try, let me know in the comments – what's your favorite utility? Any missing feature you'd love?
Stars, feedback, and shares are hugely appreciated. Thank you! 🚀
Happy coding!

Top comments (0)