package000755 000000 000000 00000000000 13613725400 010541 5ustar00000000 000000 package/LICENSE000644 000000 000000 00000002066 13613725341 011635 0ustar00000000 000000 The MIT License (MIT) Copyright (c) 2019 Tan Li Hau Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package/README.md000644 000000 000000 00000002037 13613725341 012105 0ustar00000000 000000 # levenary [![npm-version](https://img.shields.io/npm/v/levenary.svg)](https://www.npmjs.com/package/levenary) [![github-actions](https://github.com/tanhauhau/levenary/workflows/CI/badge.svg)](https://github.com/tanhauhau/levenary/actions) > Given a string, A and an array of strings XS, return the string X from XS whose Levenshtein distance from A is minimal. ## Install ``` $ npm install levenary ``` ## Usage ```js import levenary from 'levenary'; levenary('cat', ['cow', 'dog', 'pig']); //=> 'cow' ``` ## Why `levenary`? 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) 1. Only 1 API. Simple and clean. If you want more, please use [didyoumean2](https://www.npmjs.com/package/didyoumean2). 1. [Flow](http://flow.org/) and [TypeScript](http://typescriptlang.org/) support. ## Benchmark ``` $ npm run bench ``` ``` 311,915 op/s » levenary 74,030 op/s » didyoumean 141,423 op/s » didyoumean2 ``` package/index.d.ts000644 000000 000000 00000000502 13613725341 012522 0ustar00000000 000000 declare module "levenary" { /** Return the string within `array`, whose Levenshtein distance from `str` is minimal. @example ``` import levenary from 'levenary'; levenary('cat', ['cow', 'dog', 'pig']); //=> 'cow' ``` */ const levenary: (str: string, array: string[]) => string; export default levenary; } package/index.flow.js000644 000000 000000 00000000531 13613725341 013236 0ustar00000000 000000 declare module "levenary" { /** Return the string within `array`, whose Levenshtein distance from `str` is minimal. @example ``` import levenary from 'levenary'; levenary('cat', ['cow', 'dog', 'pig']); //=> 'cow' ``` */ declare function levenary (str: string, array: string[]): string; declare export default typeof levenary; } package/index.js000644 000000 000000 00000001135 13613725341 012271 0ustar00000000 000000 "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = levenArray; var _leven = _interopRequireDefault(require("leven")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function levenArray(str, array) { var minLeven = Number.POSITIVE_INFINITY; var result = undefined; for (var _i2 = 0; _i2 < array.length; _i2++) { var item = array[_i2]; var distance = (0, _leven.default)(str, item); if (distance < minLeven) { minLeven = distance; result = item; } } return result; } package/index.mjs000644 000000 000000 00000000501 13613725341 012442 0ustar00000000 000000 import leven from 'leven'; export default function levenArray(str, array) { let minLeven = Number.POSITIVE_INFINITY; let result = undefined; for(const item of array) { const distance = leven(str, item); if (distance < minLeven) { minLeven = distance; result = item; } } return result; }package/package.json000644 000000 000000 00000002222 13613725561 013114 0ustar00000000 000000 { "name": "levenary", "version": "1.1.1", "main": "index.js", "module": "index.mjs", "author": "Tan Li Hau ", "license": "MIT", "repository": "tanhauhau/levenary", "scripts": { "test": "jest", "build": "babel index.mjs --out-file index.js", "bench": "matcha bench.js" }, "dependencies": { "leven": "^3.1.0" }, "files": [ "index.mjs", "index.js", "index.d.ts", "index.flow.js" ], "engines": { "node": ">= 6" }, "devDependencies": { "@babel/cli": "^7.7.5", "@babel/core": "^7.7.5", "@babel/plugin-transform-for-of": "^7.7.4", "@babel/preset-env": "^7.7.6", "babel-jest": "^24.9.0", "bench": "^0.3.6", "didyoumean": "^1.2.1", "didyoumean2": "^3.1.2", "jest": "^24.9.0", "matcha": "^0.7.0" }, "browserslist": "> 0.25%, not dead", "keywords": [ "leven", "levenshtein", "distance", "array", "string", "algorithm", "algo", "string", "difference", "diff", "fast", "fuzzy", "similar", "similarity", "compare", "comparison", "edit", "text", "match", "matching" ] }