moroshko / autosuggest-trie

Minimalistic trie implementation for autosuggest and autocomplete components
MIT License
25 stars 4 forks source link

Custom whitespace regex #4

Closed yunyu closed 7 years ago

yunyu commented 7 years ago

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.

moroshko commented 7 years ago

I see. Makes sense. We could add options.whitespaceRegex to createTrie. Would you like to submit a PR?

moroshko commented 7 years ago

v2.1.0 ships with options.splitRegex for createTrie. Could you please test it and let me know if it works for you as expected?

yunyu commented 7 years ago

Works with /\s+|-/, thanks for merging