Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wankdanker/node-token-collector
Collect string tokens if they match filters
https://github.com/wankdanker/node-token-collector
Last synced: 14 days ago
JSON representation
Collect string tokens if they match filters
- Host: GitHub
- URL: https://github.com/wankdanker/node-token-collector
- Owner: wankdanker
- Created: 2015-06-03T16:10:32.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-06-03T16:52:47.000Z (over 9 years ago)
- Last Synced: 2024-12-06T01:46:06.600Z (about 1 month ago)
- Language: JavaScript
- Size: 113 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
token-collector
---------------Parse strings and collect tokens that match filter functions
install
-------```bash
npm install token-collector
```example
-------```js
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 } ]
*/
```api
---### constructor
```js
var tc = require('token-collector')();
```### .addFilter(fn)
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.```js
//only accept tokens starting with the letter a
tc.addFilter(function (tok) {
return (tok.substr(0,1) === "a")
});
```### .parse(string)
Parse `string` for tokens and add them to the collection if they pass all filters
### .add(string)
Add `string` to the collection if it passes all filters
### .get()
Return an array of token objects that take the form:
```json
{
"token" : "string"
, "count" : 1
}
```### .toArray()
Return an array of token strings from the collection
### .toString()
Return a camma separated list of tokens from the collection
license
-------
MIT