Jaro-Winkler for TypeScript

1.0.1 · active · verified Sun Apr 19

jaro-winkler-typescript provides a pure TypeScript implementation of the Jaro and Jaro-Winkler string similarity algorithms. These algorithms are widely used to measure the edit distance or similarity between two strings, returning a score typically ranging from 0 (completely dissimilar) to 1 (identical). The current stable version is 1.0.1. As a utility library for a well-defined algorithm, its release cadence is expected to be slow, with new versions primarily addressing bug fixes, performance improvements, or minor enhancements. A key differentiator is its explicit TypeScript implementation, offering strong type safety and an improved developer experience in modern TypeScript projects. It serves as a direct, unopinionated implementation focused solely on accurately calculating these specific string similarity scores.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of Jaro and Jaro-Winkler algorithms, including case-sensitive and case-insensitive comparisons, and interpretation of similarity scores.

import { jaro, jaroWinkler } from "jaro-winkler-typescript";

// Example 1: Basic comparison for Jaro similarity
const stringA = "MARTHA";
const stringB = "MARHTA";
const similarityJaro = jaro(stringA, stringB);
console.log(`Jaro similarity between "${stringA}" and "${stringB}": ${similarityJaro.toFixed(4)}`);
// Expected output: Jaro similarity between "MARTHA" and "MARHTA": 0.9444

// Example 2: Basic comparison for Jaro-Winkler similarity
const stringC = "DWAYNE";
const stringD = "DUANE";
const similarityJaroWinkler = jaroWinkler(stringC, stringD);
console.log(`Jaro-Winkler similarity between "${stringC}" and "${stringD}": ${similarityJaroWinkler.toFixed(4)}`);
// Expected output: Jaro-Winkler similarity between "DWAYNE" and "DUANE": 0.8400

// Example 3: Case-insensitive comparison
const name1 = "JavaScript";
const name2 = "javascript";
const insensitiveJaro = jaro(name1, name2, { caseSensitive: false });
console.log(`Case-insensitive Jaro similarity between "${name1}" and "${name2}": ${insensitiveJaro.toFixed(4)}`);
// Expected output: Case-insensitive Jaro similarity between "JavaScript" and "javascript": 1.0000

// Example 4: Comparing strings with significant differences
const word1 = "hello";
const word2 = "world";
const jaroScore = jaro(word1, word2);
const jaroWinklerScore = jaroWinkler(word1, word2);
console.log(`Jaro score for "${word1}" and "${word2}": ${jaroScore.toFixed(4)}`);
console.log(`Jaro-Winkler score for "${word1}" and "${word2}": ${jaroWinklerScore.toFixed(4)}`);
// Expected output: Jaro score for "hello" and "world": 0.4444, Jaro-Winkler score for "hello" and "world": 0.4444

view raw JSON →