Skip to main content

API Documentation for sifter

Sifter

Sifter

Kind: global class

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).
ParamType
itemsarray | object
itemsobject

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

ParamType
querystring

sifter.iterator(object)

Iterates over arrays and hashes.

this.iterator(this.items, function(item, id) {
// invoked for each item
});

Kind: instance method of Sifter

ParamType
objectarray | 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

ParamTypeDescription
searchobject | string
optionsobject(optional)

getScoreFunction~scoreObject ⇒ number

Calculates the score of an object against the search query.

Kind: inner property of getScoreFunction

ParamType
tokenobject
dataobject

getScoreFunction~scoreValue(value, token) ⇒ number

Calculates how close of a match the given value is against a search token.

Kind: inner method of getScoreFunction

ParamType
valuemixed
tokenobject

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)

ParamType
searchstring | object
optionsobject

getSortFunction~get_field(name, result) ⇒ mixed

Fetches the specified sort field value from a search result item.

Kind: inner method of getSortFunction

ParamType
namestring
resultobject

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

ParamType
querystring
optionsobject

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

ParamType
querystring
optionsobject

getattr(obj, name, nesting) ⇒ Object

A property getter resolving dot-notation

Kind: global function
Returns: Object - The resolved property value

ParamTypeDescription
objObjectThe root object to fetch property on
nameStringThe optionally dotted property name to fetch
nestingBooleanHandle nesting or not