A string is a sequence of characters.
▌ 📦 JSR, 📦 NPM, 🌐 Web, 📰 Docs.
This package is a single flat module that includes every built-in
String method as a free function (slice, concat, toUpperCase, ...)
and adds a number of utility functions on top. In JavaScript, strings are
immutable: any transformation such as slice or concat produces a new
string. The runtime may optimize this behind the scenes (copy-on-write,
sliced-string views), but from the programmer's perspective strings never
change.
The additions are:
- Case conversion: toKebabCase, toSnakeCase, toCamelCase, toPascalCase, toSlugCase, toTitleCase, toWords.
- Script conversion: toBaseline, toSuperscript, toSubscript.
- N-grams: ngrams, uniqueNgrams, countNgrams, countUniqueNgrams, countEachNgram and the matching variants.
- Similarity / distance: levenshteinDistance, damerauLevenshteinDistance, jaroSimilarity, jaroWinklerSimilarity, jaccardIndex, sorensenDiceIndex, tverskyIndex, euclideanDistance, hammingDistance.
- Longest common substrings: longestCommonPrefix, longestCommonSuffix, longestCommonInfix, longestUncommonInfixes.
- Character access: get, getAll, set, begin, middle, end.
- Predicates and helpers: is, isEmpty, isCharacter, index, indexRange, codePointRange, compare, isEqual.
- Array-like transforms: of, from, splice, reverse, sort, filter, cycle, rotate.
- ASCII constants: DIGITS, LETTERS, PUNCTUATION, WHITESPACE, PRINTABLE, and code-point bounds.
import * as xstring from "jsr:@nodef/extra-string";
// 1. Convert a string to kebab-case.
xstring.toKebabCase('Malwa Plateau');
// → 'malwa-plateau'
// 2. Get the longest common infix.
xstring.longestCommonInfix('dismiss', 'mississipi');
// → 'miss'
// 3. Get the longest uncommon infixes.
xstring.longestUncommonInfixes('chocolatier', 'engineer');
// → ['chocolati', 'engine']
// 4. Convert digits to superscript characters.
'6.626 x 10' + xstring.toSuperscript('-34');
// → '6.626 x 10⁻³⁴' (Planck's constant)
// 5. Get the Tversky distance between strings.
xstring.tverskyDistance('pikachu', 'raichu', 3, 0.2, 0.4);
// → 0.6666666666666667| Property | Description |
|---|---|
| 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. |
| toSlugCase | Convert a string to slug-case (URL-friendly kebab-case). |
| toTitleCase | Convert a string to title-case. |
| toWords | Split a string into words, after de-casing it. |
| toBaseline | Convert a string to baseline characters (limited support). |
| toSuperscript | Convert a string to superscript characters (limited support). |
| toSubscript | Convert a string to subscript characters (limited support). |
| 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. |
| 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. |
| 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. |
| cycle | Get characters that cycle through string. |
| rotate | Rotate characters in string. |
| 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. |
| 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. |
| Property | Description |
|---|---|
| fromCharCode | Get characters whose UTF-16 code units are given. |
| fromCodePoint | Get characters whose unicode code points are given. |
| concat | Combine multiple values into a string. |
| 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. |
function toKebabCase(x: string, re?: RegExp | null, sep?: string): string;
function toSnakeCase(x: string, re?: RegExp | null): string;
function toCamelCase(x: string, re?: RegExp | null, upper?: boolean): string;
function toPascalCase(x: string, re?: RegExp | null): string;
function toSlugCase(x: string, re?: RegExp | null, sep?: string): string;
function toTitleCase(x: string, re?: RegExp | null, sep?: string): string;Convert a string to a particular case. re is the word separator pattern
(defaults to /[^0-9A-Za-z]+/g). toSlugCase() additionally normalizes
accents and strips non-ASCII characters, making the result URL-friendly.
function toWords(x: string, re?: RegExp | null): string[];Split a string into words, de-casing it along the way. parseURLToJSON
becomes ['parse', 'URL', 'To', 'JSON'].
function toBaseline(x: string, fsup?: MapFunction | null, fsub?: MapFunction | null): string;
function toSuperscript(x: string): string;
function toSubscript(x: string): string;Convert characters to baseline, superscript, or subscript form. Coverage is limited to a documented subset of Unicode.
function ngrams(x: string, n: number): string[];
function uniqueNgrams(x: string, n: number): Set<string>;
function countNgrams(x: string, n: number): number;
function countUniqueNgrams(x: string, n: number): number;
function countEachNgram(x: string, n: number): Map<string, number>;Generate or count n-grams of a string.
matchingNgrams / uniqueMatchingNgrams / countMatchingNgrams / countEachMatchingNgram / countUniqueMatchingNgrams
function matchingNgrams(x: string, y: string, n: number): string[];
function uniqueMatchingNgrams(x: string, y: string, n: number): Set<string>;
function countMatchingNgrams(x: string, y: string, n: number): number;
function countEachMatchingNgram(x: string, y: string, n: number): Map<string, number>;
function countUniqueMatchingNgrams(x: string, y: string, n: number): number;Generate or count matching n-grams between two strings. Matching accounts
for multiplicities: each n-gram in x can be matched at most once.
function levenshteinDistance(x: string, y: string, ins?: number, del?: number, sub?: number): number;
function damerauLevenshteinDistance(x: string, y: string, ins?: number, del?: number, sub?: number, tra?: number): number;Levenshtein (edit) distance and Damerau–Levenshtein distance with configurable insertion, deletion, substitution, and (for Damerau) transposition costs.
function jaroSimilarity(x: string, y: string): number;
function jaroDistance(x: string, y: string): number;
function jaroWinklerSimilarity(x: string, y: string, p?: number): number;
function jaroWinklerDistance(x: string, y: string, p?: number): number;Jaro and Jaro–Winkler string similarity and distance. p is the scaling
factor for the common prefix (0.1–0.25, default 0.1).
jaccardIndex / jaccardDistance / sorensenDiceIndex / sorensenDiceDistance / tverskyIndex / tverskyDistance
function jaccardIndex(x: string, y: string, n: number): number;
function jaccardDistance(x: string, y: string, n: number): number;
function sorensenDiceIndex(x: string, y: string, n: number): number;
function sorensenDiceDistance(x: string, y: string, n: number): number;
function tverskyIndex(x: string, y: string, n: number, a?: number, b?: number): number;
function tverskyDistance(x: string, y: string, n: number, a?: number, b?: number): number;Set-similarity indices and distances based on n-grams. The Tversky index
generalizes both Jaccard (a = b = 1) and Sørensen–Dice (a = b = 0.5).
function euclideanDistance(x: string, y: string): number;
function hammingDistance(x: string, y: string): number;Euclidean distance over code points, and Hamming distance over aligned characters.
function longestCommonPrefix(x: string, y: string): string;
function longestCommonSuffix(x: string, y: string): string;
function longestCommonInfix(x: string, y: string): string;
function longestUncommonInfixes(x: string, y: string): [string, string];Longest common prefix, suffix, or infix; or the longest uncommon middle portions after stripping the common prefix and suffix.
function get(x: string, at: number): string;
function getAll(x: string, ats: Iterable<number>): string;
function set(x: string, at: number, write: string): string;
function begin(x: string, count?: number): string;
function middle(x: string, start: number, count?: number): string;
function end(x: string, count?: number): string;Character accessors. Indices may be negative (counted from the right) for
get(), getAll(), and set().
function is(v: unknown): boolean;
function isEmpty(x: string): boolean;
function isCharacter(x: string): boolean;
function index(x: string, at: number): number;
function indexRange(x: string, start: number, end: number): [number, number];
function codePointRange(x: string): [number, number];
function compare(x: string, y: string): number;
function isEqual(x: string, y: string): boolean;Predicates and small helpers.
function spaces(n: number): string;Get a string of n spaces.
function cycle(x: string, start: number, count?: number): string;
function rotate(x: string, n: number): string;Cycle or rotate characters in a string.
function of(...args: string[]): string;
function from(xs: Iterable<string>, fm?: MapFunction, ths?: unknown): string;Create a string from arguments or an iterable, like Array.of() and
Array.from().
function splice(x: string, start: number, remove?: number, add?: string): string;
function reverse(x: string): string;
function sort(x: string, fc?: CompareFunction): string;
function filter(x: string, ft: TestFunction, ths?: unknown): string;Array-like transformations on strings.
- MDN Web docs
- Lodash documentation
- natural package by Chris Umbel and contributors
- esrever package by Mathias Bynens and contributors
- js-string-compression package by Chen Caishun
- @stdlib/string-base-trim package by @stdlib-js
- How do you reverse a string in-place in JavaScript?
