1 | # fuzzaldrin [![Build Status](https://travis-ci.org/atom/fuzzaldrin.png?branch=master)](https://travis-ci.org/atom/fuzzaldrin)
|
2 |
|
3 | Fuzzy filtering and string scoring.
|
4 |
|
5 | ## Using
|
6 |
|
7 | ```sh
|
8 | npm install fuzzaldrin
|
9 | ```
|
10 |
|
11 | ### filter(candidates, query, [options])
|
12 |
|
13 | Sort 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 |
|
21 | Returns 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
|
27 | candidates = ['Call', 'Me', 'Maybe']
|
28 | results = filter(candidates, 'me')
|
29 | console.log(results) # ['Me', 'Maybe']
|
30 |
|
31 | # With an array of objects
|
32 | candidates = [
|
33 | {name: 'Call', id: 1}
|
34 | {name: 'Me', id: 2}
|
35 | {name: 'Maybe', id: 3}
|
36 | ]
|
37 | results = filter(candidates, 'me', key: 'name')
|
38 | console.log(results) # [{name: 'Me', id: 2}, {name: 'Maybe', id: 3}]
|
39 | ```
|
40 |
|
41 | ### score(string, query)
|
42 |
|
43 | Score 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 |
|
51 | score('Me', 'me') # 0.75
|
52 | score('Maybe', 'me') # 0.31499999999999995
|
53 | ```
|
54 |
|
55 | ## Developing
|
56 |
|
57 | ```sh
|
58 | git clone https://github.com/atom/fuzzaldrin.git
|
59 | cd fuzzaldrin
|
60 | npm install
|
61 | npm test
|
62 | ```
|