string

String

Methods

hamming

(static) hamming(str1, str2) → {number}

Computes the hamming distance between two strings of identical length. This distance is always >= 0.

Example
hamming( 'john', 'john' );
// ->  0
hamming( 'sam', 'sat' );
// -> 1
hamming( 'summer', 'samuel' );
// -> 3
hamming( 'saturn', 'urn' );
// -> throws error
Parameters
Name Type Description
str1 string

first string.

str2 string

second string.

Returns

hamming distance between str1 and str2.

Type
number

hammingNormalized

(static) hammingNormalized(str1, str2) → {number}

Computes the normalized hamming distance between two strings. These strings may be of different lengths. Normalized distance is always between 0 and 1.

Example
hammingNormalized( 'john', 'johny' );
// ->  0.2
hammingNormalized( 'sam', 'sam' );
// -> 0
hammingNormalized( 'sam', 'samuel' );
// -> 0.5
hammingNormalized( 'saturn', 'urn' );
// -> 1
Parameters
Name Type Description
str1 string

first string.

str2 string

second string.

Returns

normalized hamming distance between str1 and str2.

Type
number

jaro

(static) jaro(str1, str2) → {number}

Computes the jaro distance between two strings. This distance is always between 0 and 1.

Example
jaro( 'father', 'farther' );
// ->  0.04761904761904756
jaro( 'abcdef', 'fedcba' );
// -> 0.6111111111111112
jaro( 'sat', 'urn' );
// -> 1
Parameters
Name Type Description
str1 string

first string.

str2 string

second string.

Returns

jaro distance between str1 and str2.

Type
number

jaroWinkler

(static) jaroWinkler(str1, str2, boostThresholdopt, scalingFactoropt) → {number}

Computes the jaro winkler distance between two strings. This distance, controlled by the scalingFactor, is always between 0 and 1.

Example
jaroWinkler( 'martha', 'marhta' );
// ->  0.03888888888888883
jaroWinkler( 'martha', 'marhta', 0.3, 0.2 );
// -> 0.022222222222222185
jaroWinkler( 'duane', 'dwayne' );
// -> .15999999999999992
Parameters
Name Type Attributes Default Description
str1 string

first string.

str2 string

second string.

boostThreshold number <optional>
0.3

beyond which scaling is applied: it is applied only if the jaro distance between the input strings is less than or equal to this value. Any value > 1, is capped at 1 automatically.

scalingFactor number <optional>
0.1

is used to scale the distance. Such scaling, if applied, is proportional to the number of shared consecutive characters from the first character of str1 and str2. Any value > 0.25, is capped at 0.25 automatically.

Returns

jaro winkler distance between str1 and str2.

Type
number

levenshtein

(static) levenshtein(str1, str2) → {number}

Computes the levenshtein distance between two strings. This distance is computed as the number of deletions, insertions, or substitutions required to transform a string to another. Levenshtein distance is always an integer with a value of 0 or more.

Example
levenshtein( 'example', 'sample' );
// ->  2
levenshtein( 'distance', 'difference' );
// -> 5
Parameters
Name Type Description
str1 string

first string.

str2 string

second string.

Returns

levenshtein distance between str1 and str2.

Type
number

soundex

(static) soundex(str1, str2) → {number}

Computes the soundex distance between two strings. This distance is either 1 indicating phonetic similarity or 0 indicating no similarity.

Example
soundex( 'Burroughs', 'Burrows' );
// ->  0
soundex( 'Ekzampul', 'example' );
// -> 0
soundex( 'sat', 'urn' );
// -> 1
Parameters
Name Type Description
str1 string

first string.

str2 string

second string.

Returns

soundex distance between str1 and str2.

Type
number