str.mjs provides tools for string manipulation. Stuff missing from JS built-ins. Some of the features:
- Unicode-aware word splitting and case conversion.
- Unicode-aware truncation and ellipsis.
- Sanity-checked parsing of booleans and numbers.
- Unlike built-in
parseFloatandparseInt, this library requires an entire string to be a valid input, without truncating the rest.
- Unlike built-in
Written carefully and with benchmarks, but doesn't claim to be optimal at what it does. When in doubt, measure and compare.
Example case conversion:
import * as s from 'https://cdn.jsdelivr.net/npm/@mitranim/js@0.1.85/str.mjs'
s.words(`oneTwoThree`).title().snake() === `One_Two_Three`
s.words(`ΕΝΑ_ΔΥΟ_ΤΡΙΑ`).lower().kebab() === `ενα-δυο-τρια`Nil tolerance rules:
- Funs that take and return strings allow nil input.
- Funs that answer questions about strings require strings.
The following APIs are exported but undocumented. Check str.mjs.
const RE_WORDconst RE_EMBEDfunction isBlankfunction isAsciifunction isAsciiPrintfunction isNarrowfunction isUnifunction isEveryCharCodefunction isCodeAsciifunction isCodeAsciiPrintfunction lenStrfunction lenUnifunction ellfunction truncfunction trimfunction wordsclass Wordsfunction lowerfunction upperfunction titlefunction strMapclass StrMapfunction regTestfunction regEscfunction boolOptfunction boolfunction finOptfunction finfunction intOptfunction intfunction natOptfunction natfunction interfunction maybeInterfunction stripPrefunction stripPreAllfunction stripSuffunction stripSufAllfunction optPrefunction optSuffunction maybePrefunction maybeSuffunction splitfunction splitMapfunction linesfunction trimLinesfunction joinByfunction joinOptByfunction joinfunction joinLaxfunction joinOptfunction joinOptLaxfunction joinLinesfunction joinLinesLaxfunction joinLinesOptfunction joinLinesOptLaxfunction spacedfunction dashedfunction toByteArrfunction rndHexfunction byteArrHexfunction uuidfunction uuidByteArrfunction draftParsefunction draftRenderfunction draftRenderAsyncclass Draftfunction isRenclass Embedfunction strfunction strLaxfunction strConcatfunction strConcatLaxfunction sanfunction sanLaxfunction interpolateclass Strfunction replaceAllfunction commonPrefixLenfunction commonPrefix