API Documentation for sifter
Sifter
Sifter
Kind: global class
- Sifter
- new Sifter(items, items)
- .tokenize(query) ⇒
array
- .iterator(object)
- .getScoreFunction(search, options) ⇒
function
- ~scoreObject ⇒
number
- ~scoreValue(value, token) ⇒
number
- ~scoreObject ⇒
- .getSortFunction(search, options) ⇒
- ~get_field(name, result) ⇒
mixed
- ~get_field(name, result) ⇒
- .prepareSearch(query, options) ⇒
object
- .search(query, options) ⇒
object
new Sifter(items, items)
Sifter is a client and server-side library (via UMD) for textually searching arrays and hashes of objects by property – or multiple properties. It's designed specifically for autocomplete. The process is three-step: score, filter, sort.
- Supports díåcritîçs. - For example, if searching for "montana" and an item in the set has a value of "montaña", it will still be matched. Sorting will also play nicely with diacritics
- Smart scoring. - Items are scored / sorted intelligently depending on where a match is found in the string (how close to the beginning) and what percentage of the string matches.
- Multi-field sorting. - When scores aren't enough to go by – like when getting results for an empty query – it can sort by one or more fields. For example, sort by a person's first name and last name without actually merging the properties to a single string.
- Nested properties. - Allows to search and sort on nested properties so you can perform search on complex objects without flattening them simply by using dot-notation to reference fields (ie. nested.property).
Param | Type |
---|---|
items | array | object |
items | object |
sifter.tokenize(query) ⇒ array
Splits a search string into an array of individual regexps to be used to match results.
Kind: instance method of Sifter
Param | Type |
---|---|
query | string |
sifter.iterator(object)
Iterates over arrays and hashes.
this.iterator(this.items, function(item, id) {
// invoked for each item
});
Kind: instance method of Sifter
Param | Type |
---|---|
object | array | object |
sifter.getScoreFunction(search, options) ⇒ function
Returns a function to be used to score individual results.
Good matches will have a higher score than poor matches. If an item is not a match, 0 will be returned by the function.
Kind: instance method of Sifter
Param | Type | Description |
---|---|---|
search | object | string | |
options | object | (optional) |
- .getScoreFunction(search, options) ⇒
function
- ~scoreObject ⇒
number
- ~scoreValue(value, token) ⇒
number
- ~scoreObject ⇒
getScoreFunction~scoreObject ⇒ number
Calculates the score of an object against the search query.
Kind: inner property of getScoreFunction
Param | Type |
---|---|
token | object |
data | object |
getScoreFunction~scoreValue(value, token) ⇒ number
Calculates how close of a match the given value is against a search token.
Kind: inner method of getScoreFunction
Param | Type |
---|---|
value | mixed |
token | object |
sifter.getSortFunction(search, options) ⇒
Returns a function that can be used to compare two
results, for sorting purposes. If no sorting should
be performed, null
will be returned.
Kind: instance method of Sifter
Returns: function(a,b)
Param | Type |
---|---|
search | string | object |
options | object |
getSortFunction~get_field(name, result) ⇒ mixed
Fetches the specified sort field value from a search result item.
Kind: inner method of getSortFunction
Param | Type |
---|---|
name | string |
result | object |
sifter.prepareSearch(query, options) ⇒ object
Parses a search query and returns an object with tokens and fields ready to be populated with results.
Kind: instance method of Sifter
Param | Type |
---|---|
query | string |
options | object |
sifter.search(query, options) ⇒ object
Searches through all items and returns a sorted array of matches.
The options
parameter can contain:
- fields {string|array}
- sort {array}
- score {function}
- filter {bool}
- limit {integer}
Returns an object containing:
- options {object}
- query {string}
- tokens {array}
- total {int}
- items {array}
Kind: instance method of Sifter
Param | Type |
---|---|
query | string |
options | object |
getattr(obj, name, nesting) ⇒ Object
A property getter resolving dot-notation
Kind: global function
Returns: Object
- The resolved property value
Param | Type | Description |
---|---|---|
obj | Object | The root object to fetch property on |
name | String | The optionally dotted property name to fetch |
nesting | Boolean | Handle nesting or not |