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
parseFloat
andparseInt
, 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/[email protected]/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_WORD
const RE_EMBED
function isBlank
function isAscii
function isAsciiPrint
function isNarrow
function isUni
function isEveryCharCode
function isCodeAscii
function isCodeAsciiPrint
function lenStr
function lenUni
function ell
function trunc
function trim
function words
class Words
function lower
function upper
function title
function strMap
class StrMap
function regTest
function regEsc
function boolOpt
function bool
function finOpt
function fin
function intOpt
function int
function natOpt
function nat
function inter
function maybeInter
function stripPre
function stripPreAll
function stripSuf
function stripSufAll
function optPre
function optSuf
function maybePre
function maybeSuf
function split
function splitMap
function lines
function trimLines
function joinBy
function joinOptBy
function join
function joinLax
function joinOpt
function joinOptLax
function joinLines
function joinLinesLax
function joinLinesOpt
function joinLinesOptLax
function spaced
function dashed
function isSubpath
function rndHex
function arrHex
function uuid
function uuidArr
function draftParse
function draftRender
function draftRenderAsync
class Draft
function isRen
class Embed
function str
function strLax
function strConcat
function strConcatLax
function san
function sanLax
function interpolate
class Str
function replaceAll
function commonPrefixLen
function commonPrefix