From e555b89f8f54cf1dbd205b5afb572dc8003b4cae Mon Sep 17 00:00:00 2001 From: Alexander Polynomdivision Date: Fri, 14 Sep 2018 16:55:58 +0200 Subject: [PATCH] fix: Add typings and a test case for equality --- src/algorithms/levenshtein/__tests__/levenshtein.test.ts | 5 +++++ src/algorithms/levenshtein/index.ts | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/algorithms/levenshtein/__tests__/levenshtein.test.ts b/src/algorithms/levenshtein/__tests__/levenshtein.test.ts index d0106de..fd098ce 100644 --- a/src/algorithms/levenshtein/__tests__/levenshtein.test.ts +++ b/src/algorithms/levenshtein/__tests__/levenshtein.test.ts @@ -1,5 +1,10 @@ import { lev, levW } from "../"; +test("lev(abc, abc) == 0", () => { + expect(lev("abc", "abc", 3, 3)).toBe(0); + expect(levW("abc", "abc")).toBe(0); +}); + test("lev(abc, abd) == 1", () => { expect(lev("abc", "abd", 3, 3)).toBe(1); expect(levW("abc", "abd")).toBe(1); diff --git a/src/algorithms/levenshtein/index.ts b/src/algorithms/levenshtein/index.ts index 0810ea1..1405d21 100644 --- a/src/algorithms/levenshtein/index.ts +++ b/src/algorithms/levenshtein/index.ts @@ -1,4 +1,4 @@ -export function one(a: string, b: string, i: number, j: number) { +function one(a: string, b: string, i: number, j: number): number { if (a[i] === b[j]) { return 0; } @@ -10,7 +10,7 @@ export function one(a: string, b: string, i: number, j: number) { // @a, b: The strings to compare // @i, j: From where to compare (a_i and b_j) // @ret : The distance in insertions, deletions and changes -export function lev(a: string, b: string, i: number, j: number) { +export function lev(a: string, b: string, i: number, j: number): number { if (Math.min(i, j) === 0) { return Math.max(i, j); } else { @@ -25,6 +25,6 @@ export function lev(a: string, b: string, i: number, j: number) { // Computes the difference between the strings a and b. // @a, b: The strings to compare // @ret : The distance in insertions, deletions and changes -export function levW(a: string, b: string) { +export function levW(a: string, b: string): number { return lev(a, b, a.length, b.length); }