match-sorter

Simple, expected, and deterministic best-match sorting of an array in JavaS...

README

match-sorter

Simple, expected, and deterministic best-match sorting of an array in JavaScript




[![Build Status][build-badge]][build] [![Code Coverage][coverage-badge]][coverage] [![version][version-badge]][package] [![downloads][downloads-badge]][npmtrends] [![MIT License][license-badge]][license] [![All Contributors][all-contributors-badge]](#contributors-) [![PRs Welcome][prs-badge]][prs] [![Code of Conduct][coc-badge]][coc] [![Examples][examples-badge]][examples]


The problem


1.  You have a list of dozens, hundreds, or thousands of items
2.  You want to filter and sort those items intelligently (maybe you have a
    filter input for the user)
3.  You want simple, expected, and deterministic sorting of the items (no fancy
    math algorithm that fancily changes the sorting as they type)

This solution


This follows a simple and sensible (user friendly) algorithm that makes it easy
for you to filter and sort a list of items based on given input. Items are
ranked based on sensible criteria that result in a better user experience.

To explain the ranking system, I'll use countries as an example:

1.  CASE SENSITIVE EQUALS: Case-sensitive equality trumps all. These will be
    first. (ex. France would match France, but not france)
2.  EQUALS: Case-insensitive equality (ex. France would match france)
3.  STARTS WITH: If the item starts with the given value (ex. Sou would
    match South Korea or South Africa)
4.  WORD STARTS WITH: If the item has multiple words, then if one of those
    words starts with the given value (ex. Repub would match
    Dominican Republic)
5.  CONTAINS: If the item contains the given value (ex. ham would match
    Bahamas)
6.  ACRONYM: If the item's acronym is the given value (ex. us would match
    United States)
7.  SIMPLE MATCH: If the item has letters in the same order as the letters
    of the given value (ex. iw would match Zimbabwe, but not Kuwait
    because it must be in the same order). Furthermore, if the item is a closer
    match, it will rank higher (ex. ua matches Uruguay more closely than
    United States of America, therefore Uruguay will be ordered before
    United States of America)

This ranking seems to make sense in people's minds. At least it does in mine.
Feedback welcome!




  - [keys: [string]](#keys-string)
  - [threshold: number](#threshold-number)
  - [keepDiacritics: boolean](#keepdiacritics-boolean)
  - [baseSort: function(itemA, itemB): -1 | 0 | 1](#basesort-functionitema-itemb--1--0--1)
  - [sorter: function(rankedItems): rankedItems](#sorter-functionrankeditems-rankeditems)
  - 🐛 Bugs



Installation


This module is distributed via [npm][npm] which is bundled with [node][node] and
should be installed as one of your project's dependencies:

  1. ```
  2. npm install match-sorter
  3. ```

Usage


  1. ``` js
  2. import {matchSorter} from 'match-sorter'
  3. // or const {matchSorter} = require('match-sorter')
  4. // or window.matchSorter.matchSorter
  5. const list = ['hi', 'hey', 'hello', 'sup', 'yo']
  6. matchSorter(list, 'h') // ['hello', 'hey', 'hi']
  7. matchSorter(list, 'y') // ['yo', 'hey']
  8. matchSorter(list, 'z') // []
  9. ```

Advanced options


keys: [string]


_Default: undefined_

By default it just uses the value itself as above. Passing an array tells
match-sorter which keys to use for the ranking.

  1. ``` js
  2. const objList = [
  3.   {name: 'Janice', color: 'Green'},
  4.   {name: 'Fred', color: 'Orange'},
  5.   {name: 'George', color: 'Blue'},
  6.   {name: 'Jen', color: 'Red'},
  7. ]
  8. matchSorter(objList, 'g', {keys: ['name', 'color']})
  9. // [{name: 'George', color: 'Blue'}, {name: 'Janice', color: 'Green'}, {name: 'Fred', color: 'Orange'}]

  10. matchSorter(objList, 're', {keys: ['color', 'name']})
  11. // [{name: 'Jen', color: 'Red'}, {name: 'Janice', color: 'Green'}, {name: 'Fred', color: 'Orange'}, {name: 'George', color: 'Blue'}]
  12. ```

Array of values: When the specified key matches an array of values, the best
match from the values of in the array is going to be used for the ranking.

  1. ``` js
  2. const iceCreamYum = [
  3.   {favoriteIceCream: ['mint', 'chocolate']},
  4.   {favoriteIceCream: ['candy cane', 'brownie']},
  5.   {favoriteIceCream: ['birthday cake', 'rocky road', 'strawberry']},
  6. ]
  7. matchSorter(iceCreamYum, 'cc', {keys: ['favoriteIceCream']})
  8. // [{favoriteIceCream: ['candy cane', 'brownie']}, {favoriteIceCream: ['mint', 'chocolate']}]
  9. ```

Nested Keys: You can specify nested keys using dot-notation.

  1. ``` js
  2. const nestedObjList = [
  3.   {name: {first: 'Janice'}},
  4.   {name: {first: 'Fred'}},
  5.   {name: {first: 'George'}},
  6.   {name: {first: 'Jen'}},
  7. ]
  8. matchSorter(nestedObjList, 'j', {keys: ['name.first']})
  9. // [{name: {first: 'Janice'}}, {name: {first: 'Jen'}}]

  10. const nestedObjList = [
  11.   {name: [{first: 'Janice'}]},
  12.   {name: [{first: 'Fred'}]},
  13.   {name: [{first: 'George'}]},
  14.   {name: [{first: 'Jen'}]},
  15. ]
  16. matchSorter(nestedObjList, 'j', {keys: ['name.0.first']})
  17. // [{name: {first: 'Janice'}}, {name: {first: 'Jen'}}]

  18. // matchSorter(nestedObjList, 'j', {keys: ['name[0].first']}) does not work
  19. ```

This even works with arrays of multiple nested objects: just specify the key
using dot-notation with the * wildcard instead of a numeric index.

  1. ``` js
  2. const nestedObjList = [
  3.   {aliases: [{name: {first: 'Janice'}},{name: {first: 'Jen'}}]},
  4.   {aliases: [{name: {first: 'Fred'}},{name: {first: 'Frederic'}}]},
  5.   {aliases: [{name: {first: 'George'}},{name: {first: 'Georgie'}}]},
  6. ]
  7. matchSorter(nestedObjList, 'jen', {keys: ['aliases.*.name.first']})
  8. // [{aliases: [{name: {first: 'Janice'}},{name: {first: 'Jen'}}]}]
  9. matchSorter(nestedObjList, 'jen', {keys: ['aliases.0.name.first']})
  10. // []
  11. ```

Property Callbacks: Alternatively, you may also pass in a callback function
that resolves the value of the key(s) you wish to match on. This is especially
useful when interfacing with libraries such as Immutable.js

  1. ``` js
  2. const list = [{name: 'Janice'}, {name: 'Fred'}, {name: 'George'}, {name: 'Jen'}]
  3. matchSorter(list, 'j', {keys: [item => item.name]})
  4. // [{name: 'Janice'}, {name: 'Jen'}]
  5. ```

For more complex structures, expanding on the nestedObjList example above, you
can use map:

  1. ``` js
  2. const nestedObjList = [
  3.   {
  4.     name: [
  5.       {first: 'Janice', last: 'Smith'},
  6.       {first: 'Jon', last: 'Doe'},
  7.     ],
  8.   },
  9.   {
  10.     name: [
  11.       {first: 'Fred', last: 'Astaire'},
  12.       {first: 'Jenny', last: 'Doe'},
  13.       {first: 'Wilma', last: 'Flintstone'},
  14.     ],
  15.   },
  16. ]
  17. matchSorter(nestedObjList, 'doe', {
  18.   keys: [
  19.     item => item.name.map(i => i.first),
  20.     item => item.name.map(i => i.last),
  21.   ],
  22. })
  23. // [name: [{ first: 'Janice', last: 'Smith' },{ first: 'Jon', last: 'Doe' }], name: [{ first: 'Fred', last: 'Astaire' },{ first: 'Jenny', last: 'Doe' },{ first: 'Wilma', last: 'Flintstone' }]]
  24. ```

Threshold: You may specify an individual threshold for specific keys. A key
will only match if it meets the specified threshold. _For more information
regarding thresholds see below_

  1. ``` js
  2. const list = [
  3.   {name: 'Fred', color: 'Orange'},
  4.   {name: 'Jen', color: 'Red'},
  5. ]
  6. matchSorter(list, 'ed', {
  7.   keys: [{threshold: matchSorter.rankings.STARTS_WITH, key: 'name'}, 'color'],
  8. })
  9. //[{name: 'Jen', color: 'Red'}]
  10. ```

Min and Max Ranking: You may restrict specific keys to a minimum or maximum
ranking by passing in an object. A key with a minimum rank will only get
promoted if there is at least a simple match.

  1. ``` js
  2. const tea = [
  3.   {tea: 'Earl Grey', alias: 'A'},
  4.   {tea: 'Assam', alias: 'B'},
  5.   {tea: 'Black', alias: 'C'},
  6. ]
  7. matchSorter(tea, 'A', {
  8.   keys: ['tea', {maxRanking: matchSorter.rankings.STARTS_WITH, key: 'alias'}],
  9. })
  10. // without maxRanking, Earl Grey would come first because the alias "A" would be CASE_SENSITIVE_EQUAL
  11. // `tea` key comes before `alias` key, so Assam comes first even though both match as STARTS_WITH
  12. // [{tea: 'Assam', alias: 'B'}, {tea: 'Earl Grey', alias: 'A'},{tea: 'Black', alias: 'C'}]
  13. ```

  1. ``` js
  2. const tea = [
  3.   {tea: 'Milk', alias: 'moo'},
  4.   {tea: 'Oolong', alias: 'B'},
  5.   {tea: 'Green', alias: 'C'},
  6. ]
  7. matchSorter(tea, 'oo', {
  8.   keys: ['tea', {minRanking: matchSorter.rankings.EQUAL, key: 'alias'}],
  9. })
  10. // minRanking bumps Milk up to EQUAL from CONTAINS (alias)
  11. // Oolong matches as STARTS_WITH
  12. // Green is missing due to no match
  13. // [{tea: 'Milk', alias: 'moo'}, {tea: 'Oolong', alias: 'B'}]
  14. ```

threshold: number


_Default: MATCHES_

Thresholds can be used to specify the criteria used to rank the results.
Available thresholds (from top to bottom) are:

- CASE_SENSITIVE_EQUAL
- EQUAL
- STARTS_WITH
- WORD_STARTS_WITH
- STRING_CASE
- STRING_CASE_ACRONYM
- CONTAINS
- ACRONYM
- MATCHES _(default value)_
- NO_MATCH

  1. ``` js
  2. const fruit = ['orange', 'apple', 'grape', 'banana']
  3. matchSorter(fruit, 'ap', {threshold: matchSorter.rankings.NO_MATCH})
  4. // ['apple', 'grape', 'orange', 'banana'] (returns all items, just sorted by best match)

  5. const things = ['google', 'airbnb', 'apple', 'apply', 'app'],
  6. matchSorter(things, 'app', {threshold: matchSorter.rankings.EQUAL})
  7. // ['app'] (only items that are equal)

  8. const otherThings = ['fiji apple', 'google', 'app', 'crabapple', 'apple', 'apply']
  9. matchSorter(otherThings, 'app', {threshold: matchSorter.rankings.WORD_STARTS_WITH})
  10. // ['app', 'apple', 'apply', 'fiji apple'] (everything that matches with "word starts with" or better)
  11. ```

keepDiacritics: boolean


_Default: false_

By default, match-sorter will strip diacritics before doing any comparisons.
This is the default because it makes the most sense from a UX perspective.

You can disable this behavior by specifying keepDiacritics: true

  1. ``` js
  2. const thingsWithDiacritics = [
  3.   'jalapeño',
  4.   'à la carte',
  5.   'café',
  6.   'papier-mâché',
  7.   'à la mode',
  8. ]
  9. matchSorter(thingsWithDiacritics, 'aa')
  10. // ['jalapeño', 'à la carte', 'à la mode', 'papier-mâché']

  11. matchSorter(thingsWithDiacritics, 'aa', {keepDiacritics: true})
  12. // ['jalapeño', 'à la carte']

  13. matchSorter(thingsWithDiacritics, 'à', {keepDiacritics: true})
  14. // ['à la carte', 'à la mode']
  15. ```

baseSort: function(itemA, itemB): -1 | 0 | 1


_Default: (a, b) => String(a.rankedValue).localeCompare(b.rankedValue)_

By default, match-sorter uses the String.localeCompare function to tie-break
items that have the same ranking. This results in a stable, alphabetic sort.

  1. ``` js
  2. const list = ['C apple', 'B apple', 'A apple']
  3. matchSorter(list, 'apple')
  4. // ['A apple', 'B apple', 'C apple']
  5. ```

_You can customize this behavior by specifying a custom baseSort function:_

  1. ``` js
  2. const list = ['C apple', 'B apple', 'A apple']
  3. // This baseSort function will use the original index of items as the tie breaker
  4. matchSorter(list, 'apple', {baseSort: (a, b) => (a.index < b.index ? -1 : 1)})
  5. // ['C apple', 'B apple', 'A apple']
  6. ```

sorter: function(rankedItems): rankedItems


_Default:
matchedItems => matchedItems.sort((a, b) => sortRankedValues(a, b, baseSort))_

By default, match-sorter uses an internal sortRankedValues function to sort
items after matching them.

_You can customize the core sorting behavior by specifying a custom sorter
function:_

Disable sorting entirely:
  1. ``` js
  2. const list = ['appl', 'C apple', 'B apple', 'A apple', 'app', 'applebutter']
  3. matchSorter(list, 'apple', {sorter: rankedItems => rankedItems})
  4. // ['C apple', 'B apple', 'A apple', 'applebutter']
  5. ```

Return the unsorted rankedItems, but in reverse order:
  1. ``` js
  2. const list = ['appl', 'C apple', 'B apple', 'A apple', 'app', 'applebutter']
  3. matchSorter(list, 'apple', {sorter: rankedItems => [...rankedItems].reverse()})
  4. // ['applebutter', 'A apple', 'B apple', 'C apple']
  5. ```

Recipes


Match PascalCase, camelCase, snake_case, or kebab-case as words


By default, match-sorter assumes spaces to be the word separator. However, if
your data has a different word separator, you can use a property callback to
replace your separator with spaces. For example, for snake_case:

  1. ``` js
  2. const list = [
  3.   {name: 'Janice_Kurtis'},
  4.   {name: 'Fred_Mertz'},
  5.   {name: 'George_Foreman'},
  6.   {name: 'Jen_Smith'},
  7. ]
  8. matchSorter(list, 'js', {keys: [item => item.name.replace(/_/g, ' ')]})
  9. // [{name: 'Jen_Smith'}, {name: 'Janice_Kurtis'}]
  10. ```

Match many words across multiple fields (table filtering)


By default, match-sorter will return matches from objects where one of the
properties matches _the entire_ search term. For multi-column data sets it can
be beneficial to split words in search string and match each word separately.
This can be done by chaining match-sorter calls.

The benefit of this is that a filter string of "two words" will match both "two"
and "words", but will return rows where the two words are found in _different_
columns as well as when both words match in the same column. For single-column
matches it will also return matches out of order (column = "wordstwo" will match
just as well as column="twowords", the latter getting a higher score).

  1. ``` js
  2. function fuzzySearchMultipleWords(
  3.   rows, // array of data [{a: "a", b: "b"}, {a: "c", b: "d"}]
  4.   keys, // keys to search ["a", "b"]
  5.   filterValue: string, // potentially multi-word search string "two words"
  6. ) {
  7.   if (!filterValue || !filterValue.length) {
  8.     return rows
  9.   }

  10.   const terms = filterValue.split(' ')
  11.   if (!terms) {
  12.     return rows
  13.   }

  14.   // reduceRight will mean sorting is done by score for the _first_ entered word.
  15.   return terms.reduceRight(
  16.     (results, term) => matchSorter(results, term, {keys}),
  17.     rows,
  18.   )
  19. }
  20. ```


Inspiration


Actually, most of this code was extracted from the _very first_ library I ever
wrote: [genie][genie]!

Other Solutions


You might try Fuse.js. It uses advanced math
fanciness to get the closest match. Unfortunately what's "closest" doesn't
always really make sense. So I extracted this from [genie][genie].

Issues


_Looking to contribute? Look for the [Good First Issue][good-first-issue]
label._

🐛 Bugs


Please file an issue for bugs, missing documentation, or unexpected behavior.

[See Bugs][bugs]

💡 Feature Requests


Please file an issue to suggest new features. Vote on feature requests by adding
a 👍. This helps maintainers prioritize what to work on.

[See Feature Requests][requests]

Contributors ✨


Thanks goes to these people ([emoji key][emojis]):





Kent C. Dodds

💻 📖 🚇 ⚠️ 👀

Conor Hastings

💻 📖 ⚠️ 👀

Rogelio Guzman

📖

Claudéric Demers

💻 📖 ⚠️

Kevin Davis

💻 ⚠️

Denver Chen

💻 📖 ⚠️

Christian Ruigrok

🐛 💻 📖

Hozefa

🐛 💻 ⚠️ 🤔

pushpinder107

💻

Mordy Tikotzky

💻 📖 ⚠️

Steven Brannum

💻 ⚠️

Christer van der Meeren

🐛

Samuel Petrosyan

💻 🐛

Brandon Kalinowski

🐛

Eric Berry

🔍

Skubie Doo

📖

Michaël De Boey

💻 👀

Tanner Linsley

💻 ⚠️

Victor

📖

Rebecca Stevens

🐛 📖

Marco Moretti

📖

Ricardo Busquet

🤔 👀 💻

Weyert de Boer

🤔 👀

Philipp Garbowsky

💻

Mart

💻 ⚠️ 📖

Aleksey Levenstein

💻

Take Weiland

💻

Amit Abershitz

📖






This project follows the [all-contributors][all-contributors] specification.
Contributions of any kind welcome!

LICENSE


MIT


[npm]: https://www.npmjs.com
[node]: https://nodejs.org
[build-badge]: https://img.shields.io/github/workflow/status/kentcdodds/match-sorter/validate?logo=github&style=flat-square
[build]: https://github.com/kentcdodds/match-sorter/actions?query=workflow%3Avalidate
[coverage-badge]: https://img.shields.io/codecov/c/github/kentcdodds/match-sorter.svg?style=flat-square
[coverage]: https://codecov.io/github/kentcdodds/match-sorter
[version-badge]: https://img.shields.io/npm/v/match-sorter.svg?style=flat-square
[package]: https://www.npmjs.com/package/match-sorter
[downloads-badge]: https://img.shields.io/npm/dm/match-sorter.svg?style=flat-square
[npmtrends]: https://www.npmtrends.com/match-sorter
[license-badge]: https://img.shields.io/npm/l/match-sorter.svg?style=flat-square
[license]: https://github.com/kentcdodds/match-sorter/blob/master/LICENSE
[prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
[prs]: http://makeapullrequest.com
[coc-badge]: https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square
[coc]: https://github.com/kentcdodds/match-sorter/blob/master/CODE_OF_CONDUCT.md
[examples-badge]: https://img.shields.io/badge/%F0%9F%92%A1-examples-8C8E93.svg?style=flat-square
[examples]: https://github.com/kentcdodds/match-sorter/blob/master/other/EXAMPLES.md
[emojis]: https://github.com/all-contributors/all-contributors#emoji-key
[all-contributors]: https://github.com/all-contributors/all-contributors
[all-contributors-badge]: https://img.shields.io/github/all-contributors/kentcdodds/match-sorter?color=orange&style=flat-square
[bugs]: https://github.com/kentcdodds/match-sorter/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Acreated-desc+label%3Abug
[requests]: https://github.com/kentcdodds/match-sorter/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3Aenhancement
[good-first-issue]: https://github.com/kentcdodds/match-sorter/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3Aenhancement+label%3A%22good+first+issue%22

[genie]: https://github.com/kentcdodds/genie