Skip to content

Commit

Permalink
feat(options): add support for callback properties (#21) (#19)
Browse files Browse the repository at this point in the history
* Added support for property callbacks

* Updated tests for property callbacks

* Updated documentation for property callbacks

* Updated contributors section

* Fix linting error
  • Loading branch information
Claudéric Demers authored and Kent C. Dodds committed Sep 21, 2016
1 parent 5f37e2e commit eacc0e9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
11 changes: 11 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@
"contributions": [
"doc"
]
},
{
"login": "clauderic",
"name": "Claudéric Demers",
"avatar_url": "https://avatars.githubusercontent.com/u/1416436?v=3",
"profile": "http://ced.io",
"contributions": [
"code",
"doc",
"test"
]
}
]
}
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Simple, expected, and deterministic best-match sorting of an array in JavaScript
[![downloads][downloads-badge]][npm-stat]
[![MIT License][license-badge]][LICENSE]

[![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-4-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Donate][donate-badge]][donate]
[![Code of Conduct][coc-badge]][coc]
Expand Down Expand Up @@ -112,6 +112,19 @@ matchSorter(nestedObjList, 'j', {keys: ['name.first']})
// [{name: {first: 'Janice'}}, {name: {first: 'Jen'}}]
```

__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

```javascript
const list = [
{name: 'Janice'},
{name: 'Fred'},
{name: 'George'},
{name: 'Jen'},
]
matchSorter(list, 'j', {keys: [(item) => item.name]})
// [{name: 'Janice'}, {name: 'Jen'}]
```

### threshold: `number`

_Default: `MATCHES`_
Expand Down Expand Up @@ -180,8 +193,8 @@ You might try [Fuse.js](https://github.com/krisk/Fuse). It uses advanced math fa
Thanks goes to these people ([emoji key][emojis]):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub>Kent C. Dodds</sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/match-sorter/commits?author=kentcdodds) [📖](https://github.com/kentcdodds/match-sorter/commits?author=kentcdodds) 🚇 [⚠️](https://github.com/kentcdodds/match-sorter/commits?author=kentcdodds) 👀 | [<img src="https://avatars.githubusercontent.com/u/8263298?v=3" width="100px;"/><br /><sub>Conor Hastings</sub>](http://conorhastings.com)<br />[💻](https://github.com/kentcdodds/match-sorter/commits?author=conorhastings) [📖](https://github.com/kentcdodds/match-sorter/commits?author=conorhastings) [⚠️](https://github.com/kentcdodds/match-sorter/commits?author=conorhastings) 👀 | [<img src="https://avatars.githubusercontent.com/u/574806?v=3" width="100px;"/><br /><sub>Rogelio Guzman</sub>](https://github.com/rogeliog)<br />[📖](https://github.com/kentcdodds/match-sorter/commits?author=rogeliog) |
| :---: | :---: | :---: |
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub>Kent C. Dodds</sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/match-sorter/commits?author=kentcdodds) [📖](https://github.com/kentcdodds/match-sorter/commits?author=kentcdodds) 🚇 [⚠️](https://github.com/kentcdodds/match-sorter/commits?author=kentcdodds) 👀 | [<img src="https://avatars.githubusercontent.com/u/8263298?v=3" width="100px;"/><br /><sub>Conor Hastings</sub>](http://conorhastings.com)<br />[💻](https://github.com/kentcdodds/match-sorter/commits?author=conorhastings) [📖](https://github.com/kentcdodds/match-sorter/commits?author=conorhastings) [⚠️](https://github.com/kentcdodds/match-sorter/commits?author=conorhastings) 👀 | [<img src="https://avatars.githubusercontent.com/u/574806?v=3" width="100px;"/><br /><sub>Rogelio Guzman</sub>](https://github.com/rogeliog)<br />[📖](https://github.com/kentcdodds/match-sorter/commits?author=rogeliog) | [<img src="https://avatars.githubusercontent.com/u/1416436?v=3" width="100px;"/><br /><sub>Claudéric Demers</sub>](http://ced.io)<br />[💻](https://github.com/kentcdodds/match-sorter/commits?author=clauderic) [📖](https://github.com/kentcdodds/match-sorter/commits?author=clauderic) [⚠️](https://github.com/kentcdodds/match-sorter/commits?author=clauderic) |
| :---: | :---: | :---: | :---: |
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors][all-contributors] specification. Contributions of any kind welcome!
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,13 @@ function prepareValueForComparison(value, {keepDiacritics}) {
/**
* Gets value for key in item at arbitrarily nested keypath
* @param {Object} item - the item
* @param {Object} key - the potentially nested keypath
* @param {Object|Function} key - the potentially nested keypath or property callback
* @return {String} - the value at nested keypath
*/
function getItemValue(item, key) {
if (typeof key === 'function') {
return key(item)
}
const isNested = key.indexOf('.') !== -1
if (!isNested) {
return item[key]
Expand Down
15 changes: 15 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,21 @@ const tests = {
{name: {first: 'bat'}},
],
},
'can handle property callback': {
input: [
[
{name: {first: 'baz'}},
{name: {first: 'bat'}},
{name: {first: 'foo'}},
],
'ba',
{keys: [item => item.name.first]},
],
output: [
{name: {first: 'baz'}},
{name: {first: 'bat'}},
],
},
'can handle keys that are an array of values': {
input: [
[
Expand Down

0 comments on commit eacc0e9

Please sign in to comment.