Skip to content

Repository files navigation

A string is a sequence of characters. In JavaScript, strings are not mutable. Any transfomation of a string, such as slice or concat generates a new string. The JavaScript runtime however may optimize this behavior by mutating strings behind the scenes, when it can be guarenteed that the previous string is not accessible to the programmer. The runtime may also avoid copying slices of a string, or even concatenation of slices ofstrings, by implementing it as a series of lookups into existing strings. Food for thought.

This package provides functions for generating spaces, querying about a string, comparing strings, getting parts of a string, searching a string, transforming a string or its case, finding ngrams in strings, finding similarity/distance between strings, and array-like functions. All built-in string functions are also included. Finally, constants for ASCII characters, and minimum/maximum code point are included.

▌ 📦 JSR, 📰 Docs.


import*asxstringfrom"jsr:@nodef/extra-string";xstring.longestCommonInfix('dismiss','mississipi');// → 'miss'xstring.longestUncommonInfixes('chocolatier','engineer');// → ['chocolati', 'engine']xstring.toKebabCase('Malwa Plateau');// → 'malwa-plateau''6.626 x 10'+xstring.toSuperscript('-34');// → '6.626 x 10⁻³⁴' (Planck's constant)xstring.tverskyDistance('pikachu','raichu',3,0.2,0.4);// → 0.6666666666666667


Index

NameDescription
toKebabCaseConvert a string to kebab-case.
toSnakeCaseConvert a string to snake-case.
toCamelCaseConvert a string to camel-case.
toPascalCaseConvert a string to pascal-case.
toSlugCaseConvert a string to slug-case (URL-friendly kebab-case).
toWordsSplit a string into words, after de-casing it.
toBaselineConvert a string to baseline characters (limited support).
toSuperscriptConvert a string to superscript characters (limited support).
toSubscriptConvert a string to superscript characters (limited support).
ngramsGet n-grams of a string.
uniqueNgramsFind unique n-grams of a string.
countNgramsCount the total number of n-grams of a string.
countUniqueNgramsCount the total number of unique n-grams of a string.
countEachNgramCount each n-gram of a string.
matchingNgramsGet matching n-grams between strings.
uniqueMatchingNgramsGet unique matching n-grams between strings.
countMatchingNgramsCount the total number of matching n-grams between strings.
countEachMatchingNgramCount each matching n-gram between strings.
countUniqueMatchingNgramsCount the total number of unique matching n-grams between strings.
euclideanDistanceGet euclidean distance between strings.
hammingDistanceGet hamming distance between strings.
jaccardIndexGet jaccard index between strings.
jaccardDistanceGet jaccard distance between strings.
sorensenDiceIndexGet Sørensen-Dice index between strings.
sorensenDiceDistanceGet Sørensen-Dice distance between strings.
tverskyIndexGet Tversky index between strings.
tverskyDistanceGet Tversky distance between strings.
jaroSimilarityGet Jaro similarity between strings.
jaroDistanceGet Jaro distance between strings.
jaroWinklerSimilarityGet Jaro-Winkler similarity between strings.
jaroWinklerDistanceGet Jaro-Winkler distance between strings.
levenshteinDistanceGet Levenshtein distance between strings.
damerauLevenshteinDistanceGet Damerau–Levenshtein distance between strings.
longestCommonInfixGet the longest common infix between strings.
longestCommonPrefixGet the longest common prefix of strings.
longestCommonSuffixGet the longest common suffix of strings.
longestUncommonInfixesGet the longest uncommon infixes of strings.
getGet character at a given index in string.
getAllGet characters at indices.
setWrite a substring at specified index in string.
beginGet leftmost part of string.
middleGet a portion of string from middle.
endGet rightmost part of string.
fromCharCodeGet characters whose UTF-16 code units are given.
fromCodePointGet characters whose unicode code points are given.
concatCombine multiple strings into one.
repeatRepeat string given number of times.
valueOfGet primitive value of string object.
lengthGet length of string.
charAtGet character at given index in string.
charCodeAtGet UTF-16 code unit of a character in string.
codePointAtGet unicode code point of a character in string.
localeCompareCompare two strings in the current or given locale.
includesCheck if string has a given infix.
startsWithCheck if string has a given prefix.
endsWithCheck if string has a given suffix.
indexOfGet first index of a given infix in string.
lastIndexOfGet last index of a given infix in string.
searchGet first index of regular expression match in string.
matchGet results of matching string with regular expression.
matchAllGet detailed results of matching string with regular expression.
toStringGet string representation of string.
sliceExtract section of string.
substringExtract section of string.
splitSplit string by a given separator into substrings.
cycleGet characters that cycle through string.
rotateRotate characters in string.
trimStartRemove whitespace from begining of string.
trimEndRemove whitespace from end of string.
trimRemove whitespace from begining and end of string.
padStartPad start of string to fit a desired length.
padEndPad end of string to fit a desired length.
toUpperCaseConvert string to upper case.
toLocaleUpperCaseConvert string to upper case, as per locale-specific case mappings.
toLowerCaseConvert string to lower case.
toLocaleLowerCaseConvert string to lower case, as per locale-specific case mappings.
replaceReplace first match of given pattern by replacement.
normalizeNormalize string by given form, as per Unicode Standard Annex #15.
ofCreate string from arguments, like Array.of().
fromCreate string from iterable, like Array.from().
spliceRemove/replace characters in a string.
reverseReverse a string.
sortArrange characters in an order.
filterFilter characters which pass a test.
spacesGet a string of spaces.
isCheck if value is a string.
isEmptyCheck if string is empty.
isCharacterCheck if string is a character.
indexGet non-negative index within string.
indexRangeGet non-negative index range within string.
codePointRangeGet unicode code point range of string.
compareCompare two strings.
isEqualCheck if two strings are equal.
DIGITSDecimal digits 0-9.
OCT_DIGITSOctal digits 0-7.
HEX_DIGITSHexadecimal digits 0-9, A-F, a-f.
UPPERCASEEnglish letters A-Z.
LOWERCASEEnglish letters a-z.
LETTERSCombination of uppercase, lowercase english letters.
PUNCTUATIONPunctuation symbols (ASCII).
WHITESPACEThe string "\t\n\x0b\x0c\r ".
PRINTABLECombination of digits, letters, punctuation, and whitespace (ASCII).
MIN_CODE_POINTMinimum unicode code point.
MAX_CODE_POINTMaximum unicode code point.


References



License

As of 26 June 2025, this project is licensed under AGPL-3.0. Previous versions remain under MIT.




ORG

Used by

Contributors

Languages