node-string-similarity
Version:
A TypeScript library for string similarity comparison
72 lines (71 loc) • 3.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("./index");
const visualization_1 = require("./visualization");
describe("compareStrings", () => {
it("should return 1.0 for identical strings", () => {
expect((0, index_1.compareStrings)("hello", "hello")).toBe(1.0);
});
it("should calculate Jaro-Winkler similarity", () => {
expect((0, index_1.jaroWinklerDistance)("hello", "hell")).toBeGreaterThan(0.8);
expect((0, index_1.jaroWinklerDistance)("hello", "world")).toBeLessThan(0.5);
});
it("should calculate Cosine similarity", () => {
expect((0, index_1.cosineSimilarity)("hello", "world")).toBeLessThan(0.9);
expect((0, index_1.cosineSimilarity)("hello", "world")).toBeLessThan(0.9);
});
it("should calculate Dice's Coefficient", () => {
expect((0, index_1.diceCoefficient)("hello", "hell")).toBeGreaterThan(0.8);
expect((0, index_1.diceCoefficient)("hello", "world")).toBeLessThan(0.5);
});
it("should perform batch comparison", () => {
var _a;
const results = (0, index_1.batchCompareStrings)("hello", [
"hi",
"hey",
"hello",
"world",
]);
expect(results).toHaveLength(4);
expect((_a = results.find((r) => r.candidate === "hello")) === null || _a === void 0 ? void 0 : _a.similarity).toBe(1.0);
});
it("should find matches above a threshold", () => {
const matches = (0, index_1.findMatchesAboveThreshold)("hello", ["hi", "hey", "hello", "world"], 0.8);
expect(matches).toContain("hello");
expect(matches).not.toContain("world");
});
it("should visualize string differences", () => {
const visualization = (0, visualization_1.visualizeStringDifferences)("hello", "hell");
console.log("Visualization Output:", visualization);
//eslint-disable-next-line
const strippedVisualization = visualization.replace(/\x1B\[[0-9;]*m/g, ""); // Remove ANSI escape codes
expect(strippedVisualization).toBe("hello");
});
it("should return 0.0 for completely different strings", () => {
expect((0, index_1.compareStrings)("hello", "world")).toBeLessThan(0.5);
});
it("should handle empty strings correctly", () => {
try {
expect((0, index_1.compareStrings)("", "")).toBe(1.0);
}
catch (err) {
if (err instanceof Error) {
expect(err.message).toBe("Both strings must be non-empty");
}
}
try {
(0, index_1.compareStrings)("hello", "");
}
catch (error) {
if (error instanceof Error) {
console.log("Caught error:", error.message);
expect(error.message).toBe("Both strings must be non-empty");
}
}
});
it("should return a similarity score between 0 and 1 for partially similar strings", () => {
const score = (0, index_1.compareStrings)("hello", "hell");
expect(score).toBeGreaterThan(0.7);
expect(score).toBeLessThan(1.0);
});
});