UNPKG

1.51 kBMarkdownView Raw
1# fuzzaldrin [![Build Status](https://travis-ci.org/atom/fuzzaldrin.png?branch=master)](https://travis-ci.org/atom/fuzzaldrin)
2
3Fuzzy filtering and string scoring.
4
5## Using
6
7```sh
8npm install fuzzaldrin
9```
10
11### filter(candidates, query, [options])
12
13Sort and filter the given candidates by matching them against the given query.
14
15* `candidates` - An array of strings or objects.
16* `query` - A string query to match each candidate against.
17* `options` - An optional object with the following keys:
18 * `key` - The property to use for scoring if the candidates are objects.
19 * `maxResults` - The maximum numbers of results to return.
20
21Returns an array of candidates sorted by best match against the query.
22
23```coffee
24{filter} = require 'fuzzaldrin'
25
26# With an array of strings
27candidates = ['Call', 'Me', 'Maybe']
28results = filter(candidates, 'me')
29console.log(results) # ['Me', 'Maybe']
30
31# With an array of objects
32candidates = [
33 {name: 'Call', id: 1}
34 {name: 'Me', id: 2}
35 {name: 'Maybe', id: 3}
36]
37results = filter(candidates, 'me', key: 'name')
38console.log(results) # [{name: 'Me', id: 2}, {name: 'Maybe', id: 3}]
39```
40
41### score(string, query)
42
43Score the given string against the given query.
44
45* `string` - The string the score.
46* `query` - The query to score the string against.
47
48```coffee
49{score} = require 'fuzzaldrin'
50
51score('Me', 'me') # 0.75
52score('Maybe', 'me') # 0.31499999999999995
53```
54
55## Developing
56
57```sh
58git clone https://github.com/atom/fuzzaldrin.git
59cd fuzzaldrin
60npm install
61npm test
62```