New upstream version 3.1.0+~cs1.1.1
Xavier Guimard
3 years ago
0 | ||
1 | The MIT License (MIT) | |
2 | ||
3 | Copyright (c) 2019 Tan Li Hau | |
4 | ||
5 | Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | of this software and associated documentation files (the "Software"), to deal | |
7 | in the Software without restriction, including without limitation the rights | |
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | copies of the Software, and to permit persons to whom the Software is | |
10 | furnished to do so, subject to the following conditions: | |
11 | ||
12 | The above copyright notice and this permission notice shall be included in all | |
13 | copies or substantial portions of the Software. | |
14 | ||
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
21 | SOFTWARE. |
0 | # levenary | |
1 | ||
2 | [![npm-version](https://img.shields.io/npm/v/levenary.svg)](https://www.npmjs.com/package/levenary) | |
3 | [![github-actions](https://github.com/tanhauhau/levenary/workflows/CI/badge.svg)](https://github.com/tanhauhau/levenary/actions) | |
4 | ||
5 | > Given a string, A and an array of strings XS, return the string X from XS whose Levenshtein distance from A is minimal. | |
6 | ||
7 | ||
8 | ## Install | |
9 | ||
10 | ``` | |
11 | $ npm install levenary | |
12 | ``` | |
13 | ||
14 | ||
15 | ## Usage | |
16 | ||
17 | ```js | |
18 | import levenary from 'levenary'; | |
19 | ||
20 | levenary('cat', ['cow', 'dog', 'pig']); | |
21 | //=> 'cow' | |
22 | ``` | |
23 | ||
24 | ## Why `levenary`? | |
25 | 1. Based on [leven](https://github.com/sindresorhus/leven), the fastest JS implementation of the [Levenshtein distance algorithm](https://en.wikipedia.org/wiki/Levenshtein_distance) | |
26 | 1. Only 1 API. Simple and clean. If you want more, please use [didyoumean2](https://www.npmjs.com/package/didyoumean2). | |
27 | 1. [Flow](http://flow.org/) and [TypeScript](http://typescriptlang.org/) support. | |
28 | ||
29 | ## Benchmark | |
30 | ||
31 | ``` | |
32 | $ npm run bench | |
33 | ``` | |
34 | ||
35 | ``` | |
36 | 311,915 op/s » levenary | |
37 | 74,030 op/s » didyoumean | |
38 | 141,423 op/s » didyoumean2 | |
39 | ``` | |
40 |
0 | declare module "levenary" { | |
1 | /** | |
2 | Return the string within `array`, whose Levenshtein distance from `str` is minimal. | |
3 | ||
4 | @example | |
5 | ``` | |
6 | import levenary from 'levenary'; | |
7 | ||
8 | levenary('cat', ['cow', 'dog', 'pig']); | |
9 | //=> 'cow' | |
10 | ``` | |
11 | */ | |
12 | const levenary: (str: string, array: string[]) => string; | |
13 | export default levenary; | |
14 | } |
0 | declare module "levenary" { | |
1 | /** | |
2 | Return the string within `array`, whose Levenshtein distance from `str` is minimal. | |
3 | ||
4 | @example | |
5 | ``` | |
6 | import levenary from 'levenary'; | |
7 | ||
8 | levenary('cat', ['cow', 'dog', 'pig']); | |
9 | //=> 'cow' | |
10 | ``` | |
11 | */ | |
12 | declare function levenary (str: string, array: string[]): string; | |
13 | declare export default typeof levenary; | |
14 | } |
0 | "use strict"; | |
1 | ||
2 | Object.defineProperty(exports, "__esModule", { | |
3 | value: true | |
4 | }); | |
5 | exports.default = levenArray; | |
6 | ||
7 | var _leven = _interopRequireDefault(require("leven")); | |
8 | ||
9 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | |
10 | ||
11 | function levenArray(str, array) { | |
12 | var minLeven = Number.POSITIVE_INFINITY; | |
13 | var result = undefined; | |
14 | ||
15 | for (var _i2 = 0; _i2 < array.length; _i2++) { | |
16 | var item = array[_i2]; | |
17 | var distance = (0, _leven.default)(str, item); | |
18 | ||
19 | if (distance < minLeven) { | |
20 | minLeven = distance; | |
21 | result = item; | |
22 | } | |
23 | } | |
24 | ||
25 | return result; | |
26 | } |
0 | import leven from 'leven'; | |
1 | ||
2 | export default function levenArray(str, array) { | |
3 | let minLeven = Number.POSITIVE_INFINITY; | |
4 | let result = undefined; | |
5 | for(const item of array) { | |
6 | const distance = leven(str, item); | |
7 | if (distance < minLeven) { | |
8 | minLeven = distance; | |
9 | result = item; | |
10 | } | |
11 | } | |
12 | return result; | |
13 | }⏎ |
0 | { | |
1 | "name": "levenary", | |
2 | "version": "1.1.1", | |
3 | "main": "index.js", | |
4 | "module": "index.mjs", | |
5 | "author": "Tan Li Hau <lhtan93@gmail.com>", | |
6 | "license": "MIT", | |
7 | "repository": "tanhauhau/levenary", | |
8 | "scripts": { | |
9 | "test": "jest", | |
10 | "build": "babel index.mjs --out-file index.js", | |
11 | "bench": "matcha bench.js" | |
12 | }, | |
13 | "dependencies": { | |
14 | "leven": "^3.1.0" | |
15 | }, | |
16 | "files": [ | |
17 | "index.mjs", | |
18 | "index.js", | |
19 | "index.d.ts", | |
20 | "index.flow.js" | |
21 | ], | |
22 | "engines": { | |
23 | "node": ">= 6" | |
24 | }, | |
25 | "devDependencies": { | |
26 | "@babel/cli": "^7.7.5", | |
27 | "@babel/core": "^7.7.5", | |
28 | "@babel/plugin-transform-for-of": "^7.7.4", | |
29 | "@babel/preset-env": "^7.7.6", | |
30 | "babel-jest": "^24.9.0", | |
31 | "bench": "^0.3.6", | |
32 | "didyoumean": "^1.2.1", | |
33 | "didyoumean2": "^3.1.2", | |
34 | "jest": "^24.9.0", | |
35 | "matcha": "^0.7.0" | |
36 | }, | |
37 | "browserslist": "> 0.25%, not dead", | |
38 | "keywords": [ | |
39 | "leven", | |
40 | "levenshtein", | |
41 | "distance", | |
42 | "array", | |
43 | "string", | |
44 | "algorithm", | |
45 | "algo", | |
46 | "string", | |
47 | "difference", | |
48 | "diff", | |
49 | "fast", | |
50 | "fuzzy", | |
51 | "similar", | |
52 | "similarity", | |
53 | "compare", | |
54 | "comparison", | |
55 | "edit", | |
56 | "text", | |
57 | "match", | |
58 | "matching" | |
59 | ] | |
60 | } |