moroshko / autosuggest-trie Goto Github PK
View Code? Open in Web Editor NEWMinimalistic trie implementation for autosuggest and autocomplete components
License: MIT License
Minimalistic trie implementation for autosuggest and autocomplete components
License: MIT License
If the key is 'San Francisco' and search for 'cisc' then doesn't show 'San Francisco'. Is it a easy fix?
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!
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 => {}
)
}
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.
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.