feat: Implement Levenshtein with tests
This commit is contained in:
parent
91b6d2a955
commit
6e4e127518
3317
package-lock.json
generated
3317
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
23
package.json
23
package.json
@ -4,7 +4,7 @@
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"test": "jest",
|
||||
"start": "parcel src/index.html --public-url '/app/'"
|
||||
},
|
||||
"author": "",
|
||||
@ -17,10 +17,27 @@
|
||||
"react-router-dom": "^4.3.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^23.3.2",
|
||||
"html-webpack-plugin": "3.2.0",
|
||||
"jest": "^23.6.0",
|
||||
"parcel-bundler": "^1.9.7",
|
||||
"ts-jest": "^23.1.4",
|
||||
"typescript": "^2.9.2",
|
||||
"webpack": "4.17.1",
|
||||
"webpack-cli": "3.1.0",
|
||||
"parcel-bundler": "^1.9.7"
|
||||
"webpack-cli": "3.1.0"
|
||||
},
|
||||
"jest": {
|
||||
"transform": {
|
||||
"^.+\\.tsx?$": "ts-jest"
|
||||
},
|
||||
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
|
||||
"moduleFileExtensions": [
|
||||
"ts",
|
||||
"tsx",
|
||||
"js",
|
||||
"jsx",
|
||||
"json",
|
||||
"node"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
16
src/algorithms/levenshtein/__tests__/levenshtein.test.ts
Normal file
16
src/algorithms/levenshtein/__tests__/levenshtein.test.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { lev, levW } from "../";
|
||||
|
||||
test("lev(abc, abd) == 1", () => {
|
||||
expect(lev("abc", "abd", 3, 3)).toBe(1);
|
||||
expect(levW("abc", "abd")).toBe(1);
|
||||
});
|
||||
|
||||
test("lev(abc, abcd) == 1", () => {
|
||||
expect(lev("abc", "abcd", 3, 4)).toBe(1);
|
||||
expect(levW("abc", "abcd")).toBe(1);
|
||||
});
|
||||
|
||||
test("lev(abcd, bcd) == 1", () => {
|
||||
expect(lev("abcd", "bcd", 4, 3)).toBe(1);
|
||||
expect(levW("abcd", "bcd")).toBe(1);
|
||||
});
|
30
src/algorithms/levenshtein/index.ts
Normal file
30
src/algorithms/levenshtein/index.ts
Normal file
@ -0,0 +1,30 @@
|
||||
export function one(a: string, b: string, i: number, j: number) {
|
||||
if (a[i] === b[j]) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Computes the difference between the strings a and b.
|
||||
// @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) {
|
||||
if (Math.min(i, j) === 0) {
|
||||
return Math.max(i, j);
|
||||
} else {
|
||||
return Math.min(
|
||||
lev(a, b, i - 1, j) + 1,
|
||||
lev(a, b, i, j - 1) + 1,
|
||||
lev(a, b, i - 1, j - 1) + one(a, b, i, j)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
// 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) {
|
||||
return lev(a, b, a.length, b.length);
|
||||
}
|
@ -1,29 +1,29 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"outDir": "build/dist",
|
||||
"module": "commonjs",
|
||||
"target": "es6",
|
||||
"lib": ["es6", "dom"],
|
||||
"sourceMap": true,
|
||||
"allowJs": true,
|
||||
"jsx": "react",
|
||||
"moduleResolution": "node",
|
||||
"rootDir": "src",
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"noImplicitReturns": true,
|
||||
"noImplicitThis": true,
|
||||
"noImplicitAny": true,
|
||||
"strictNullChecks": true,
|
||||
"suppressImplicitAnyIndexErrors": true,
|
||||
"noUnusedLocals": true
|
||||
},
|
||||
"exclude": [
|
||||
"node_modules",
|
||||
"build",
|
||||
"scripts",
|
||||
"acceptance-tests",
|
||||
"webpack",
|
||||
"**/__test__/",
|
||||
"src/setupTests.ts"
|
||||
]
|
||||
"compilerOptions": {
|
||||
"outDir": "build/dist",
|
||||
"module": "commonjs",
|
||||
"target": "es6",
|
||||
"lib": ["es6", "dom"],
|
||||
"sourceMap": true,
|
||||
"allowJs": true,
|
||||
"jsx": "react",
|
||||
"moduleResolution": "node",
|
||||
"rootDir": "src",
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"noImplicitReturns": true,
|
||||
"noImplicitThis": true,
|
||||
"noImplicitAny": true,
|
||||
"strictNullChecks": true,
|
||||
"suppressImplicitAnyIndexErrors": true,
|
||||
"noUnusedLocals": true,
|
||||
},
|
||||
"exclude": [
|
||||
"node_modules",
|
||||
"build",
|
||||
"scripts",
|
||||
"acceptance-tests",
|
||||
"webpack",
|
||||
"**/__test__/",
|
||||
"src/setupTests.ts"
|
||||
]
|
||||
}
|
||||
|
Reference in New Issue
Block a user