Skip to content

Commit e1361d9

Browse files
committedSep 14, 2020
Migrate to TypeScript
1 parent bff4bb3 commit e1361d9

8 files changed

+70
-68
lines changed
 

‎.eslintrc.js

-27
This file was deleted.

‎.eslintrc.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"parserOptions": {
3+
"ecmaVersion": 2020
4+
},
5+
"extends": [
6+
"eslint:all",
7+
"prettier",
8+
"plugin:node/recommended",
9+
"plugin:@typescript-eslint/eslint-recommended",
10+
"plugin:@typescript-eslint/recommended"
11+
],
12+
"parser": "@typescript-eslint/parser",
13+
"plugins": ["@typescript-eslint"],
14+
"rules": {
15+
"capitalized-comments": "off",
16+
"camelcase": "off",
17+
"curly": ["error", "all"],
18+
"id-length": "off",
19+
"max-lines-per-function": "off",
20+
"max-statements": "off",
21+
"multiline-comment-style": "off",
22+
"no-bitwise": "off",
23+
"no-magic-numbers": "off",
24+
"no-param-reassign": "off",
25+
"no-plusplus": "off",
26+
"node/no-unsupported-features/es-syntax": "off",
27+
"one-var": "off",
28+
"padded-blocks": "off"
29+
}
30+
}

‎.npmignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ package-lock.json
55
js/
66
coverage/
77
data.json
8-
bench.js
8+
bench.ts
99
mod.ts
1010
tsconfig.json

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![Build Status](https://travis-ci.org/ka-weihe/node-levenshtein.svg?branch=master)](https://travis-ci.org/ka-weihe/node-levenshtein)
66
[![Coverage Status](https://coveralls.io/repos/github/ka-weihe/node-levenshtein/badge.svg?branch=master)](https://coveralls.io/github/ka-weihe/node-levenshtein?branch=master)
77
[![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/ka-weihe/fastest-levenshtein.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/ka-weihe/fastest-levenshtein/context:javascript)
8-
```
8+
```bash
99
$ npm i fastest-levenshtein
1010
```
1111

‎bench.js ‎bench.ts

+17-21
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
12
/* eslint-disable no-console */
2-
/* eslint-disable no-loop-func */
3-
/* eslint-disable import/no-extraneous-dependencies */
4-
/* eslint-disable node/no-unpublished-require */
5-
const fs = require("fs");
6-
const leven = require("leven");
73
const Benchmark = require("benchmark");
8-
const fastLevenshtein = require("fast-levenshtein").get;
9-
const jslevenshtein = require("js-levenshtein");
10-
const levenshteinEditDistance = require("levenshtein-edit-distance");
11-
const { distance } = require("./dist");
4+
import { distance } from "./dist";
5+
import { get as fastLevenshtein } from "fast-levenshtein";
6+
const fs = require("fs");
7+
import jslevenshtein from "js-levenshtein";
8+
import leven from "leven";
9+
import levenshteinEditDistance from "levenshtein-edit-distance";
1210

1311
const suite = new Benchmark.Suite();
1412

15-
function randomstring(length) {
13+
const randomstring = (length) => {
1614
let result = "";
1715
const characters =
1816
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
@@ -21,16 +19,16 @@ function randomstring(length) {
2119
result += characters.charAt(Math.floor(Math.random() * charactersLength));
2220
}
2321
return result;
24-
}
22+
};
2523

26-
function randomstringArr(stringSize, arraySize) {
24+
const randomstringArr = (stringSize, arraySize) => {
2725
let i = 0;
2826
const arr = [];
2927
for (i = 0; i < arraySize; i++) {
3028
arr.push(randomstring(stringSize));
3129
}
3230
return arr;
33-
}
31+
};
3432

3533
const arrSize = 1000;
3634
if (!fs.existsSync("data.json")) {
@@ -49,39 +47,38 @@ if (!fs.existsSync("data.json")) {
4947
fs.writeFileSync("data.json", JSON.stringify(data));
5048
}
5149

52-
const data = JSON.parse(fs.readFileSync("data.json"));
50+
const data = JSON.parse(fs.readFileSync("data.json", "utf8"));
5351

5452
// BENCHMARKS
55-
let b = 0;
5653
for (let i = 0; i < 9; i++) {
5754
const datapick = data[i];
5855

5956
if (process.argv[2] !== "no") {
6057
suite
6158
.add(`${i} - js-levenshtein`, () => {
6259
for (let j = 0; j < arrSize - 1; j += 2) {
63-
b += jslevenshtein(datapick[j], datapick[j + 1]);
60+
jslevenshtein(datapick[j], datapick[j + 1]);
6461
}
6562
})
6663
.add(`${i} - leven`, () => {
6764
for (let j = 0; j < arrSize - 1; j += 2) {
68-
b += leven(datapick[j], datapick[j + 1]);
65+
leven(datapick[j], datapick[j + 1]);
6966
}
7067
})
7168
.add(`${i} - fast-levenshtein`, () => {
7269
for (let j = 0; j < arrSize - 1; j += 2) {
73-
b += fastLevenshtein(datapick[j], datapick[j + 1]);
70+
fastLevenshtein(datapick[j], datapick[j + 1]);
7471
}
7572
})
7673
.add(`${i} - levenshtein-edit-distance`, () => {
7774
for (let j = 0; j < arrSize - 1; j += 2) {
78-
b += levenshteinEditDistance(datapick[j], datapick[j + 1]);
75+
levenshteinEditDistance(datapick[j], datapick[j + 1]);
7976
}
8077
});
8178
}
8279
suite.add(`${i} - fastest-levenshtein`, () => {
8380
for (let j = 0; j < arrSize - 1; j += 2) {
84-
b += distance(datapick[j], datapick[j + 1]);
81+
distance(datapick[j], datapick[j + 1]);
8582
}
8683
});
8784
}
@@ -98,7 +95,6 @@ suite
9895
})
9996
.on("complete", () => {
10097
console.log(results);
101-
console.log(b);
10298
})
10399
// run async
104100
.run({ async: true });

‎mod.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ const myers_x = (b: string, a: string) => {
111111
return score;
112112
};
113113

114-
115-
export function distance(a: string, b: string): number {
114+
const distance = (a: string, b: string): number => {
116115
if (a.length < b.length) {
117116
const tmp = b;
118117
b = a;
@@ -125,9 +124,9 @@ export function distance(a: string, b: string): number {
125124
return myers_32(a, b);
126125
}
127126
return myers_x(a, b);
128-
}
127+
};
129128

130-
export function closest(str: string, arr: string[]): string {
129+
const closest = (str: string, arr: string[]): string => {
131130
let min_distance = Infinity;
132131
let min_index = 0;
133132
for (let i = 0; i < arr.length; i++) {
@@ -138,4 +137,6 @@ export function closest(str: string, arr: string[]): string {
138137
}
139138
}
140139
return arr[min_index];
141-
}
140+
};
141+
142+
export { closest, distance };

‎package.json

+5-6
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,18 @@
4141
"test:coveralls": "npm run build && jest --coverage --coverageReporters=text-lcov | coveralls"
4242
},
4343
"devDependencies": {
44+
"@types/benchmark": "^1.0.33",
45+
"@types/jest": "^26.0.13",
46+
"@typescript-eslint/eslint-plugin": "^4.1.1",
47+
"@typescript-eslint/parser": "^4.1.1",
4448
"benchmark": "^2.1.4",
4549
"coveralls": "^3.1.0",
46-
"eslint": "^7.5.0",
47-
"eslint-config-airbnb": "^18.2.0",
48-
"eslint-config-airbnb-base": "^14.2.0",
50+
"eslint": "^7.9.0",
4951
"eslint-config-node": "^4.1.0",
5052
"eslint-config-prettier": "^6.11.0",
5153
"eslint-plugin-import": "^2.22.0",
52-
"eslint-plugin-jsx-a11y": "^6.3.1",
5354
"eslint-plugin-node": "^11.1.0",
5455
"eslint-plugin-prettier": "^3.1.4",
55-
"eslint-plugin-react": "^7.20.3",
56-
"eslint-plugin-react-hooks": "^4.0.0",
5756
"fast-levenshtein": "^2.0.6",
5857
"jest": "^26.1.0",
5958
"js-levenshtein": "^1.1.6",

‎test.js ‎test.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
const { distance, closest } = require("./dist");
1+
import { closest, distance } from "./dist";
22

33
const levenshtein = (a, b) => {
4-
if (a.length === 0) return b.length;
5-
if (b.length === 0) return a.length;
4+
if (a.length === 0) {
5+
return b.length;
6+
}
7+
if (b.length === 0) {
8+
return a.length;
9+
}
610

711
if (a.length > b.length) {
812
const tmp = a;
@@ -18,7 +22,7 @@ const levenshtein = (a, b) => {
1822
for (let i = 1; i <= b.length; i++) {
1923
let prev = i;
2024
for (let j = 1; j <= a.length; j++) {
21-
let val;
25+
let val = 0;
2226
if (b.charAt(i - 1) === a.charAt(j - 1)) {
2327
val = row[j - 1];
2428
} else {
@@ -33,7 +37,7 @@ const levenshtein = (a, b) => {
3337
return row[a.length];
3438
};
3539

36-
function makeid(length) {
40+
const makeid = (length) => {
3741
let result = "";
3842
const characters =
3943
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
@@ -42,10 +46,9 @@ function makeid(length) {
4246
result += characters.charAt(Math.floor(Math.random() * charactersLength));
4347
}
4448
return result;
45-
}
49+
};
4650

4751
test("test compare", () => {
48-
const errors = 0;
4952
for (let i = 0; i < 1000; i++) {
5053
const rnd_num1 = (Math.random() * 1000) | 0;
5154
const rnd_num2 = (Math.random() * 1000) | 0;

0 commit comments

Comments
 (0)
Please sign in to comment.