Giter VIP home page Giter VIP logo

autosuggest-trie's People

Contributors

moroshko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

autosuggest-trie's Issues

Doesn't do partial search

If the key is 'San Francisco' and search for 'cisc' then doesn't show 'San Francisco'. Is it a easy fix?

Add option to return all items when querying for an empty string

I noticed in autosuggest-trie.js, that when the query string is empty, an empty array is always returned:

function getPhraseIndices(phrase, limit) {
  phrase = phrase.trim();

  if (phrase === '') {
    return [];
  }
  ...
}

It probably is the default behavior for such a list. However, it would be a lot more flexible if there was an option you could set when creating the structure to make it the opposite - return all the items for an empty phrase.

My use case for that is an autocomplete functionality, where I would like to return all the items at the beginning and filter it down the road.

I don't think this would be a detrimental change to the project.
As to how to implement it without introducing breaking changes, I suggest changing the parameters on create function, perhaps for the following:

function create(items, textField, options) {

And then unwrap the options, having backwards compatibility in mind:

let itemsComparator;
let returnAllWhenEmptyPhrase = false;

if (typeof options === 'function') {
  itemsComparator = options;
} else {
  { itemsComparator, returnAllWhenEmptyPhrase } = options;
}

I can submit a Pull Request if you find this functionality welcome. I would really want that, as it's the only feature missing for my project. If this doesn't get incorporated into the project, I will, unfortunately, have to refer to the use of another library.

By the way, good job on this library. I extremely appreciate your effort!

Usage with React Autosuggest

can you show how you might use this with React Autosuggest (assuming I can't download all the data when the component mounts)?

onSuggestionsFetchRequested = ({value}) => {
   CallMySearchApi(value)
   .then(
      results => {
        this.setState({
            suggestions: // shouldn't I be adding to exisiting trie here?
        })  
      },
      error => {}
   )
}

Weird issue with splitting with custom regex

I get an error with the following code:

var createTrie = require('autosuggest-trie');

var splitByHyphen = /\s+|-/;
var universities = [{ name: 'University of Tennessee-Knoxville' }];
const trie = createTrie(universities, 'name', { splitRegex: splitByHyphen });
console.log(trie.getMatches('tennessee-', { limit: 15, splitRegex: splitByHyphen }));

The error is:

C:\Users\Yunyu\Documents\VHF2017-landingpage\node_modules\autosuggest-trie\dist\create-trie\create-trie.js:79
    var length = ids.length;
                    ^

TypeError: Cannot read property 'length' of undefined
    at getWordIndices (C:\Users\Yunyu\Documents\VHF2017-landingpage\node_modules\autosuggest-trie\dist\create-trie\create-trie.js:79:21)
    at getPhraseIndices (C:\Users\Yunyu\Documents\VHF2017-landingpage\node_modules\autosuggest-trie\dist\create-trie\create-trie.js:107:43)
    at Object.getMatches (C:\Users\Yunyu\Documents\VHF2017-landingpage\node_modules\autosuggest-trie\dist\create-trie\create-trie.js:116:19)
trie\dist\create-trie\create-trie.js:116:19)

It doesn't occur when searching for a string like "of " or "tennessee-k", which is strange.

Custom whitespace regex

I have a use case where I need to regard hyphens as whitespace, and at the moment, there doesn't seem to be a way to change the whitespace regex without modifying the library. This is what I currently use:

const universities = JSON.parse(fs.readFileSync('universities.json'))
  .map(uni => {
    return { name: uni.name, nameIndex: replaceAll('-', ' ', uni.name), addr: uni.city + ', ' + uni.state };
  });
...
results = trie.getMatches(query, { limit: 5 }).map(result => ({ name: result.name, addr: result.addr }));

It would be nice to have a less hacky approach.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.