Skip to content

Commit

Permalink
feat: 新增过滤器 discardKeywords
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Oct 29, 2019
1 parent 891168b commit b9f0ecb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/guide/advance/custom-filter.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ module.exports = {
};
```

### discardKeywords <Badge text="v1.1.1" vertical="middle" />

生成一个反向关键词过滤器。第二个入参是开启严格模式。

:::warning 注意
`discardKeywords` 的目的是 **过滤掉**`useKeywords` 的目的是 **过滤出**
:::

### useRegexp

生成一个正则表达式过滤器。
Expand Down
11 changes: 10 additions & 1 deletion lib/utils/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ export const useKeywords = (keywords: ReadonlyArray<string>, isStrict?: boolean)
return item => keywords[isStrict ? 'every' : 'some'](keyword => item.nodeName.includes(keyword));
};

export const discardKeywords = (keywords: ReadonlyArray<string>, isStrict?: boolean): NodeNameFilterType => {
// istanbul ignore next
if (!Array.isArray(keywords)) {
throw new Error('keywords 请使用数组。');
}

return item => !keywords[isStrict ? 'every' : 'some'](keyword => item.nodeName.includes(keyword));
};

export const useRegexp = (regexp: RegExp): NodeNameFilterType => {
// istanbul ignore next
if (!_.isRegExp(regexp)) {
Expand All @@ -38,7 +47,7 @@ export const netflixFilter: NodeNameFilterType = item => {
export const usFilter: NodeNameFilterType = item => {
return [
'🇺🇸', '美', 'us', '波特兰', '达拉斯', '俄勒冈',
'凤凰城', '费利蒙', '硅谷', '拉斯维加斯', '洛杉矶',
'凤凰城', '费利蒙', '硅谷', '拉斯维加斯', '洛杉矶',
'圣何塞', '圣克拉拉', '西雅图', '芝加哥',
].some(key => item.nodeName.toLowerCase().includes(key));
};
Expand Down
26 changes: 26 additions & 0 deletions test/utils/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@ test('useKeywords', t => {
}));
});

test('discardKeywords', t => {
const fn1 = filter.discardKeywords(['测试', 'test']);
const fn2 = filter.discardKeywords(['测试', 'test'], true);

t.false(fn1({
nodeName: '测试',
type: NodeTypeEnum.Shadowsocks,
}));
t.true(fn1({
nodeName: '美国',
type: NodeTypeEnum.Shadowsocks,
}));
t.true(fn2({
nodeName: '测试',
type: NodeTypeEnum.Shadowsocks,
}));
t.true(fn2({
nodeName: '美国测试',
type: NodeTypeEnum.Shadowsocks,
}));
t.false(fn2({
nodeName: '测试 test',
type: NodeTypeEnum.Shadowsocks,
}));
});

test('useRegexp', t => {
const fn = filter.useRegexp(/(测试|test)/i);

Expand Down

0 comments on commit b9f0ecb

Please sign in to comment.