Skip to content

Commit

Permalink
filterBy accepts array or regex. Closes #11
Browse files Browse the repository at this point in the history
  • Loading branch information
radiovisual committed Dec 29, 2015
1 parent 8688e95 commit 5b698d0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
9 changes: 7 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import objectAssign from 'object-assign';
import {EventEmitter} from 'events';
import isRegexp from 'is-regexp';
import report from './report';
import hashRegex from 'hash-regex';

export default class Birdwatch {
/**
Expand Down Expand Up @@ -60,8 +61,12 @@ export default class Birdwatch {
throw new Error('Screenname required');
}

if (options.filterTags && !isRegexp(options.filterTags)) {
throw new Error(`Invalid regex: ${options.filterTags} for ${feed.screenname}`);
if (options.filterTags) {
if (Array.isArray(options.filterTags)) {
options.filterTags = hashRegex(options.filterTags);
} else if (!isRegexp(options.filterTags)) {
throw new Error(`Invalid regex: ${options.filterTags} for ${feed.screenname}`);
}
}

this.feeds.push({screenname: feed.screenname, options});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"dependencies": {
"babel-runtime": "^5.8.29",
"chalk": "~1.1.0",
"hash-regex": "^1.0.0",
"is-regexp": "~1.0.0",
"mkdirp": "^0.5.1",
"oauth": "^0.9.14",
Expand Down
9 changes: 7 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,14 @@ Feed options.

##### filterTags

Type: `Regex`<br>
Type: `Regex|Array`<br>

The regular expression containing the tags you want to filter with. If you do not supply a feed with a filter, then Birdwatch simply returns all tweets from that feed.
The regular expression containing the tags you want to filter with, or an array of strings. For example, both of these values will result in the same filter:

```js
.feed('user1', {filterTags: /#01|#02/gi })
.feed('user2', {filterTags: ['01','02'] })
```

**Tip:** If you need help writing your regular expressions, try [regexpal.com](http://regexpal.com/)

Expand Down
5 changes: 5 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,8 @@ test('should not expose private keys in birdwatch-config.js', t => {
configuration.accessTokenSecret === 'YOUR_ACCESS_TOKEN_SECRET'
);
});

test('filterTags should accept an array of strings', async t => {
const bw = await new Birdwatch({useTestData:true}).feed('test', {filterTags:['01','02']}).start();
t.is(bw.getCachedTweets().length, 2);
});

0 comments on commit 5b698d0

Please sign in to comment.