How to use trie-search - 10 common examples

To help you get started, we’ve selected a few trie-search examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github SayMoreX / saymore-x / app / languageFinder / LanguageFinder.ts View on Github external
constructor(defaultContentLanguage: IIndexEntry) {
    this.defaultContentLanguage = defaultContentLanguage;

    // currently this uses a trie, which is overkill for this number of items,
    // but I'm using it because I do need prefix matching and this library does that.

    // Enhance this TrieSearch doesn't provide a way to include our alternative language name arrays
    // Enhance allow for matching even if close (e.g. levenshtein distance)
    // Configure the trie to match what is in the index json
    this.index = new TrieSearch([
      "englishName",
      "localName",
      //"altNames", TrieSearch can't handle array values, so we'll add them by hand below
      "iso639_1",
      "iso639_3"
    ]);

    // add the primary name and two codes
    const index = require("./langindex.json");
    this.index.addAll(index);

    // now add the alternative names
    index.forEach(indexEntry => {
      if (indexEntry.altNames && indexEntry.altNames.length > 0) {
        indexEntry.altNames.forEach(alternativeName => {
          this.index.map(alternativeName, indexEntry);
github spgennard / vscode_cobol / src / textprovider.ts View on Github external
import TrieSearch from 'trie-search';
import { CompletionItemProvider, TextDocument, Position, CancellationToken, CompletionItem, CompletionContext, ProviderResult, CompletionList, CompletionItemKind, Range } from 'vscode';

interface TrieObject {
	key: string;
	index: number;
}

export class TextAutocompleteCompletionItemProvider implements CompletionItemProvider {
	private words: TrieSearch = new TrieSearch('key');

	public constructor(keywords: string[]) {
		let i = 0;

		/* in the future, this could be extended to add a pri rather than just use the position
	     * the array 
		 */
		this.words.addAll(keywords.map((value: string) => {
			return {
				key: value,
				index: ++i
			};
		}));
	}

	private getKeywordsGivenPartialWord(wordToComplete: string, limit: number) : CompletionItem[]  {
github mmontag / chip-player-js / src / SearchWorker.js View on Github external
import TrieSearch from 'trie-search';

const trie = new TrieSearch('file', {
  indexField: 'id',
  idFieldOrFunction: 'id',
  splitOnRegEx: /[^A-Za-z0-9]/g,
});

onmessage = (message) => {
  const data = message.data;
  switch (data.type) {
    case 'load':
      const start = performance.now();
      const files = JSON.parse(data.payload).map((file, i) => ({id: i, file: file}));
      trie.addAll(files);
      const time = (performance.now() - start).toFixed(1);
      console.log('Added %s items (%s tokens) to search trie in %s ms.', files.length, trie.size, time);
      postMessage({
        type: 'status',
github mmontag / chip-player-js / public / index.js View on Github external
'search': async (params) => {
    const limit = parseInt(params.limit, 10);
    const query = params.query;
    const start = performance.now();
    let items = trie.get(query, TrieSearch.UNION_REDUCER) || [];
    const total = items.length;
    if (limit) items = items.slice(0, limit);
    const time = (performance.now() - start).toFixed(1);
    console.log('Returned %s results in %s ms.', items.length, time);
    return {
      items: items,
      total: total,
    };
  },
github pivotal-cf / pivotal-ui / library / src / pivotal-ui-react / autocomplete / autocomplete.js View on Github external
}).pipe(through(value => {
      if (typeof value === 'object') {
        if (!trie) trie = new TrieSearch(null, trieOptions);
        trie.addFromObject(value);
        resolve(trie);
        return;
      }
      if (!trie) trie = new TrieSearch('value', trieOptions);
      trie.add({value});
      resolve(trie);
    }));
  });
github pivotal-cf / pivotal-ui / src / react / autocomplete / autocomplete.js View on Github external
}).pipe(through(value => {
      if (typeof value === 'object') {
        if (!trie) trie = new TrieSearch(null, trieOptions);
        trie.addFromObject(value);
        resolve(trie);
        return;
      }
      if (!trie) trie = new TrieSearch('value', trieOptions);
      trie.add({value});
      resolve(trie);
    }));
  });
github pivotal-cf / pivotal-ui / src / react / autocomplete / autocomplete.js View on Github external
}).pipe(through(value => {
      if (typeof value === 'object') {
        if (!trie) trie = new TrieSearch(null, trieOptions);
        trie.addFromObject(value);
        resolve(trie);
        return;
      }
      if (!trie) trie = new TrieSearch('value', trieOptions);
      trie.add({value});
      resolve(trie);
    }));
  });
github pivotal-cf / pivotal-ui / library / src / pivotal-ui-react / autocomplete / autocomplete.js View on Github external
}).pipe(through(value => {
      if (typeof value === 'object') {
        if (!trie) trie = new TrieSearch(null, trieOptions);
        trie.addFromObject(value);
        resolve(trie);
        return;
      }
      if (!trie) trie = new TrieSearch('value', trieOptions);
      trie.add({value});
      resolve(trie);
    }));
  });
github ringa-js / ringa / src / Model.js View on Github external
index(recurse = false, trieSearchOptions = {}, trieSearch = undefined) {
    trieSearch = trieSearch || new TrieSearch(undefined, Object.assign(trieSearchOptions, {
        keepAll: true,
        keepAllKey: 'id'
      }));

    trieSearch.visitedById = trieSearch.visitedById || {};

    if (trieSearch.visitedById[this.id]) {
      return;
    }

    trieSearch.visitedById[this.id] = true;

    let _add = function(model, trieSearch, prop, obj) {
      if (typeof obj === 'object' && obj !== null && obj.indexValue) {
        trieSearch.map(obj.indexValue, model);
      } else if (obj && obj.toString() !== '') {
github mmontag / chip-player-js / src / SearchWorker.js View on Github external
function search(query, maxResults) {
  let results = trie.get(query, TrieSearch.UNION_REDUCER) || [];
  const count = results.length;
  if (maxResults) {
    results = results.slice(0, maxResults);
  }
  postMessage({
    type: 'results',
    payload: {
      results: results,
      count: count,
    }
  });
}

trie-search

A trie implementation that maps keys to objects for rapid retrieval by phrases. Most common use will be for typeahead searches.

MIT
Latest version published 11 months ago

Package Health Score

64 / 100
Full package analysis

Popular trie-search functions