Parse strings and collect tokens that match filter functions
npm install token-collector
var tc = require('token-collector')();
tc.addFilter(function (str) {
return str && str.length && str.length > 3
});
tc.parse('which of these words are greater than three characters');
console.log(tc.get());
/*
[ { token: 'which', count: 1 },
{ token: 'these', count: 1 },
{ token: 'words', count: 1 },
{ token: 'greater', count: 1 },
{ token: 'than', count: 1 },
{ token: 'three', count: 1 },
{ token: 'characters', count: 1 } ]
*/
var tc = require('token-collector')();
Add a filter function to which each token is passed. If the function returns true
then the
token is added to the collection. If the function returns false
then the token is discarded.
//only accept tokens starting with the letter a
tc.addFilter(function (tok) {
return (tok.substr(0,1) === "a")
});
Parse string
for tokens and add them to the collection if they pass all filters
Add string
to the collection if it passes all filters
Return an array of token objects that take the form:
{
"token" : "string"
, "count" : 1
}
Return an array of token strings from the collection
Return a camma separated list of tokens from the collection
MIT