Introducing strfi: The Ultimate String Utility Library for JavaScript & TypeScript
Source: Dev.to
Introduction
Hey dev.to community! 👋
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.
If you’re 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 powerful string toolkit.
- 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: ~22 KB
- Works everywhere: Node.js 16+, modern browsers, Deno, Bun, React Native, Cloudflare Workers
Installation
npm install strfi
Or with Yarn / pnpm:
yarn add strfi
# or
pnpm add strfi
Usage Example
import {
slugify,
escapeHtml,
capitalize,
levenshteinDistance,
formatBytes,
camelCase,
truncate,
isEmail,
} from 'strfi';
console.log(slugify('Hello World! 🚀')); // "hello-world"
console.log(escapeHtml('alert("xss")')); // "<script>alert("xss")</script>"
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
All Features – Grouped by Category
Case Transformations
toUpperCase,toLowerCase,capitalize,uncapitalize,titleCase,camelCase,pascalCase,snakeCase,kebabCase,constantCase,dotCase,pathCase,sentenceCase,swapCaseisUpperCase,isLowerCase,isTitleCase
Whitespace Operations
trim,trimStart,trimEnd,trimChars,trimCharsStart,trimCharsEndcollapseWhitespace,removeWhitespace,padStart,padEnd,center,repeat,insert,removeAt,indent,dedent
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
String Validation
isBlank,isNotBlank,isEmpty,isNotEmpty,isAlpha,isAlphanumeric,isNumeric,isInteger,isDecimal,isAscii,isPrintableAscii,isHexadecimal,isUUID,isEmail,isURL,isJSON,isIPv4,isIPv6,isCreditCard,isSlugstartsWith,endsWith,contains,containsAll,containsAny,matches,compare,equals,equalsIgnoreCase,isPalindrome,isWhitespace,hasUpperCase,hasLowerCase,hasDigits,hasSpecialChars
Encoding & Escaping
escapeHtml,unescapeHtml,escapeXml,unescapeXml,escapeRegex,escapeSqlLike,escapeShelltoBase64,fromBase64,toBase64Url,fromBase64Url,encodeUrl,decodeUrl,toHex,fromHex,toAsciiCodes,fromAsciiCodes,rot13
Search & Analysis
countOccurrences,countWords,countLines,countSentences,countParagraphscharFrequency,wordFrequency,indicesOf,nthIndexOfextractNumbers,extractWords,extractEmails,extractUrls,extractHashtags,extractMentionslevenshteinDistance,similarity,longestCommonSubstring,longestCommonPrefix,longestCommonSuffix
Unicode & i18n
normalize,removeDiacritics,toCodePoints,fromCodePoints,graphemeLength,toGraphemes,toWordsisLetter,isDigit,isSpace,isPunctuation,isRTL,isLTR,getDirectionformatNumber,formatDate,formatCurrency,formatRelativeTime,pluralize,collategetLocaleDisplayName,getRegionDisplayName
Formatting & Utilities
template,format,formatNamed,sprintf,zeroPad,formatThousands,formatBytes,formatDuration,ordinal,loremIpsum,randomString,uuid,hashCode
What’s Next?
- npm: (link to package)
- GitHub: (repository link)
If you give strfi a try, let me know in the comments – what’s your favorite utility? Any missing feature you’d love to see?
Stars, feedback, and shares are hugely appreciated. Thank you! 🚀
Happy coding!