fix: Add typings and a test case for equality
This commit is contained in:
parent
6e4e127518
commit
e555b89f8f
@ -1,5 +1,10 @@
|
|||||||
import { lev, levW } from "../";
|
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", () => {
|
test("lev(abc, abd) == 1", () => {
|
||||||
expect(lev("abc", "abd", 3, 3)).toBe(1);
|
expect(lev("abc", "abd", 3, 3)).toBe(1);
|
||||||
expect(levW("abc", "abd")).toBe(1);
|
expect(levW("abc", "abd")).toBe(1);
|
||||||
|
@ -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]) {
|
if (a[i] === b[j]) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -10,7 +10,7 @@ export function one(a: string, b: string, i: number, j: number) {
|
|||||||
// @a, b: The strings to compare
|
// @a, b: The strings to compare
|
||||||
// @i, j: From where to compare (a_i and b_j)
|
// @i, j: From where to compare (a_i and b_j)
|
||||||
// @ret : The distance in insertions, deletions and changes
|
// @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) {
|
if (Math.min(i, j) === 0) {
|
||||||
return Math.max(i, j);
|
return Math.max(i, j);
|
||||||
} else {
|
} 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.
|
// Computes the difference between the strings a and b.
|
||||||
// @a, b: The strings to compare
|
// @a, b: The strings to compare
|
||||||
// @ret : The distance in insertions, deletions and changes
|
// @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);
|
return lev(a, b, a.length, b.length);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user