Options
All
  • Public
  • Public/Protected
  • All
Menu

Provides a fuzzy text search in the fields of a collection of JavaScript objects.

Type parameters

  • T

Hierarchy

  • TrigramIndex

Index

Constructors

Methods

  • Adds the given "document" to the search index.

    Parameters

    • doc: T

      An object of type T.

    • Optional searchableString: string

      (Optional) The keywords the given object can be found by, i.e. some relevant text representation of the object. If no string is given here, the mapper given by searchStringMapper (possibly the default mapper) will be used to generate a string.

    Returns TrigramIndex<T>

    "this", for chain notations.

  • Adds all document objects in the array, using the mapper function to generate the search keywords.

    Parameters

    • docs: Iterable<T>

      An array of objects of type T.

    Returns TrigramIndex<T>

    "this", for chain notations.

  • If an empty string is given to the find function, this function will be invoked to provide the list. An example use of this could be to track the most recently used documents, and return those when the search string is empty.

    see

    emptySearchReturnsAll

    see

    emptySearchReturnsAllIfFewerThan

    see

    emptySearchReturnsNone

    Parameters

    • supplier: () => T[]
        • (): T[]
        • Returns T[]

    Returns TrigramIndex<T>

  • If an empty string is given to the find function, the TrigramIndex will return the full list of documents. This is useful if the search represents a narrowing of data which is also paginated, such as in a table view.

    see

    emptySearchReturnsNone

    see

    emptySearchReturnsAllIfFewerThan

    see

    emptySearchReturns

    Returns TrigramIndex<T>

  • emptySearchReturnsAllIfFewerThan(threshold: number): TrigramIndex<T>
  • If an empty string is given to the find function, it may return either an empty array, or all documents. The primary use case for this is a combobox where a dropdown presents the available choices even before the user starts typing anything to narrow it down, but it doesn't make sense to show the full list when there are too many entries, since it would overwhelm the UI in both size and performance.

    see

    emptySearchReturnsAll

    see

    emptySearchReturnsNone

    see

    emptySearchReturns

    Parameters

    • threshold: number

      If the total number of documents in the TrigramIndex is less than this threshold, the full list will be returned when the search string is empty.

    Returns TrigramIndex<T>

  • If an empty string is given to the find function, the TrigramIndex will return an empty array. This is useful if the search represents an active user action, not showing anything until the user has typed some search criteria.

    see

    emptySearchReturnsAll

    see

    emptySearchReturnsAllIfFewerThan

    see

    emptySearchReturns

    Returns TrigramIndex<T>

  • find(searchString: undefined | string, maxHits?: number, quality?: number): T[]
  • Performs a fuzzy search amongst the registered document objects.

    Parameters

    • searchString: undefined | string

      The text to search for. If an empty string or undefined is given, the index will return either an empty array, or all documents, depending the setting of emptySearchReturnsAllIfFewerThan

    • maxHits: number = 10

      The maximum number of documents this function should return.

    • quality: number = 0.3

      A number from 0 to 1, describing the fraction of search trigrams which must be found in a document for it to be considered a usable result. By requiring a certain threshold, we prevent the algorithm from returning technically-best-but-practically-irrelevant results.

    Returns T[]

    An array of objects of type T, sorted by relevance.

  • searchStringMapper(mapperFunction: (doc: T) => string): TrigramIndex<T>
  • The mapper function given here will be used to turn document objects of type T into text to be used as searchable keywords. Be aware that if this method is called after documents have already been added, the index will be completely rebuilt using the new mapper function, so you generally want to call this function before addAll, or use the convenient, type-inferring static shorthand TrigramIndex.of.

    Parameters

    • mapperFunction: (doc: T) => string

      A function taking an object of type T and returning a string.

        • (doc: T): string
        • Parameters

          • doc: T

          Returns string

    Returns TrigramIndex<T>

    "this", for chain notations.

  • of<U>(docs: Iterable<U>, mapperFunction?: (doc: U) => string): TrigramIndex<U>
  • Builds a new TrigramIndex based on the given array and search-string mapper function.

    Type parameters

    • U

    Parameters

    • docs: Iterable<U>
    • Optional mapperFunction: (doc: U) => string
        • (doc: U): string
        • Parameters

          • doc: U

          Returns string

    Returns TrigramIndex<U>