s-sortbydistance

1.1.0 • Public • Published

s-sortbydistance

NPM version Dependencies build status Test Coverage NPM license

Simple way to sort arrays of strings by Levenshtein distance to a string. Useful for implementing fuzzy search. Uses Sift4 algorithm.

Installation

npm install --save s-sortbydistance

Usage

var sort = require('s-sortbydistance');
 
var unsorted = ['foo', 'foobar', 'bar', 'foo1', '~foo~'];
sort(unsorted, 'foo'); // ['foo', 'foo1', '~foo~', 'foobar', 'bar']

Configure the sift4 maxOffset (default 5) by setting sort.maxOffset before running the sort.

To remove items from the sorted array that are greater than or equal to the maxDistance, pass the maxDistance (Number) as the third argument to the sort function.

var unsorted = ['foo', 'foobar', 'asdasdasdasdasd'];
sort(unsorted, 'foo', 5); // ['foo', 'foobar'];

License (ISC)

Copyright (c) 2015, Sebastian Sandqvist s.github@sparque.me

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Dependents (1)

Package Sidebar

Install

npm i s-sortbydistance

Weekly Downloads

169

Version

1.1.0

License

ISC

Last publish

Collaborators

  • seabass