Skip to content

nodef/extra-string

Repository files navigation

A collection of common string functions.
📦 Node.js, 🌐 Web, 📜 Files, 📰 Docs.

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 of strings, 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 such as is, isEmpty, isCharacter, index, indexRange, codePointRange; comparing strings such as compare, isEqual; getting parts of a string such as get, getAll, set, begin, middle, end; searching a string such as longestCommonInfix, longestCommonPrefix, longestCommonSuffix, longestUncommonInfixes; transforming a string such as toBaseline, toSuperscript, toSubscript; transforming case of a string such as toKebabCase, toSnakeCase, toCamelCase, toPascalCase; finding ngrams in strings such as ngrams, uniqueNgrams, countNgrams, countUniqueNgrams, countEachNgram, matchingNgrams, uniqueMatchingNgrams, countMatchingNgrams, countEachMatchingNgram, countUniqueMatchingNgrams; and finding similarity/distance between strings such as euclideanDistance, hammingDistance, jaccardIndex, jaccardDistance, sorensenDiceIndex, sorensenDiceDistance, tverskyIndex, tverskyDistance, jaroSimilarity, jaroDistance, jaroWinklerSimilarity, jaroWinklerDistance, levenshteinDistance, damerauLevenshteinDistance.

This package also provides Array-like functions for strings. These includes functions for generating a string such as of, from; transforming a string such as splice, reverse, sort; and functional behavior such as filter. All built-in string functions are also included. Finally, constants for ASCII characters, and minimum/maximum code point are included.

This package is available in both Node.js and Web formats. The web format is exposed as extra_string standalone variable and can be loaded from jsDelivr CDN.

Stability: Experimental.


const string = require('extra-string');
// import * as string from "extra-string";
// import * as string from "https://unpkg.com/extra-string/index.mjs"; (deno)

string.longestCommonInfix('dismiss', 'mississipi');
// → 'miss'

string.longestUncommonInfixes('chocolatier', 'engineer');
// → ['chocolati', 'engine']

string.toKebabCase('Malwa Plateau');
// → 'malwa-plateau'

'6.626 x 10' + string.toSuperscript('-34');
// → '6.626 x 10⁻³⁴' (Planck's constant)

string.tverskyDistance('pikachu', 'raichu', 3, 0.2, 0.4);
// → 0.6666666666666667


Index

Property Description
DIGITS Decimal digits 0-9.
OCT_DIGITS Octal digits 0-7.
HEX_DIGITS Hexadecimal digits 0-9, A-F, a-f.
UPPERCASE English letters A-Z.
LOWERCASE English letters a-z.
LETTERS Combination of uppercase, lowercase english letters.
PUNCTUATION Punctuation symbols (ASCII).
WHITESPACE The string "\t\n\x0b\x0c\r ".
PRINTABLE Combination of digits, letters, punctuation, and whitespace (ASCII).
MIN_CODE_POINT Minimum unicode code point.
MAX_CODE_POINT Maximum unicode code point.
fromCharCode Get characters whose UTF-16 code units are given.
fromCodePoint Get characters whose unicode code points are given.
concat Combine multiple strings into one.
repeat Repeat string given number of times.
valueOf Get primitive value of string object.
length Get length of string.
charAt Get character at given index in string.
charCodeAt Get UTF-16 code unit of a character in string.
codePointAt Get unicode code point of a character in string.
localeCompare Compare two strings in the current or given locale.
includes Check if string has a given infix.
startsWith Check if string has a given prefix.
endsWith Check if string has a given suffix.
indexOf Get first index of a given infix in string.
lastIndexOf Get last index of a given infix in string.
search Get first index of regular expression match in string.
match Get results of matching string with regular expression.
matchAll Get detailed results of matching string with regular expression.
toString Get string representation of string.
slice Extract section of string.
substring Extract section of string.
split Split string by a given separator into substrings.
trimStart Remove whitespace from begining of string.
trimEnd Remove whitespace from end of string.
trim Remove whitespace from begining and end of string.
padStart Pad start of string to fit a desired length.
padEnd Pad end of string to fit a desired length.
toUpperCase Convert string to upper case.
toLocaleUpperCase Convert string to upper case, as per locale-specific case mappings.
toLowerCase Convert string to lower case.
toLocaleLowerCase Convert string to lower case, as per locale-specific case mappings.
replace Replace first match of given pattern by replacement.
normalize Normalize string by given form, as per Unicode Standard Annex #15.
of Create string from arguments, like Array.of().
from Create string from iterable, like Array.from().
splice Remove/replace characters in a string.
reverse Reverse a string.
sort Arrange characters in an order.
filter Filter characters which pass a test.
spaces Get a string of spaces.
is Check if value is a string.
isEmpty Check if string is empty.
isCharacter Check if string is a character.
index Get non-negative index within string.
indexRange Get non-negative index range within string.
codePointRange Get unicode code point range of string.
compare Compare two strings.
isEqual Check if two strings are equal.
get Get character at a given index in string.
getAll Get characters at indices.
set Write a substring at specified index in string.
begin Get leftmost part of string.
middle Get a portion of string from middle.
end Get rightmost part of string.
longestCommonInfix Get the longest common infix between strings.
longestCommonPrefix Get the longest common prefix of strings.
longestCommonSuffix Get the longest common suffix of strings.
longestUncommonInfixes Get the longest uncommon infixes of strings.
toBaseline Convert a string to baseline characters (limited support).
toSuperscript Convert a string to superscript characters (limited support).
toSubscript Convert a string to superscript characters (limited support).
toKebabCase Convert a string to kebab-case.
toSnakeCase Convert a string to snake-case.
toCamelCase Convert a string to camel-case.
toPascalCase Convert a string to pascal-case.
ngrams Get n-grams of a string.
uniqueNgrams Find unique n-grams of a string.
countNgrams Count the total number of n-grams of a string.
countUniqueNgrams Count the total number of unique n-grams of a string.
countEachNgram Count each n-gram of a string.
matchingNgrams Get matching n-grams between strings.
uniqueMatchingNgrams Get unique matching n-grams between strings.
countMatchingNgrams Count the total number of matching n-grams between strings.
countEachMatchingNgram Count each matching n-gram between strings.
countUniqueMatchingNgrams Count the total number of unique matching n-grams between strings.
euclideanDistance Get euclidean distance between strings.
hammingDistance Get hamming distance between strings.
jaccardIndex Get jaccard index between strings.
jaccardDistance Get jaccard distance between strings.
sorensenDiceIndex Get Sørensen-Dice index between strings.
sorensenDiceDistance Get Sørensen-Dice distance between strings.
tverskyIndex Get Tversky index between strings.
tverskyDistance Get Tversky distance between strings.
jaroSimilarity Get Jaro similarity between strings.
jaroDistance Get Jaro distance between strings.
jaroWinklerSimilarity Get Jaro-Winkler similarity between strings.
jaroWinklerDistance Get Jaro-Winkler distance between strings.
levenshteinDistance Get Levenshtein distance between strings.
damerauLevenshteinDistance Get Damerau–Levenshtein distance between strings.


References




ORG DOI Coverage Status Test Coverage Maintainability