Skip to content

Commit

Permalink
feat: 增加 mellow 规则处理方法
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Nov 13, 2019
1 parent bef00c7 commit b646199
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ export default function getEngine(templateDir: string): nunjucks.Environment {
.join('\n');
});

engine.addFilter('mellow', (str: string) => {
const array = str.split('\n');

return array
.filter(item => {
return item && item.trim() !== '' &&
item.toUpperCase().indexOf('URL-REGEX') === -1 &&
item.toUpperCase().indexOf('USER-AGENT') === -1;
})
.map((item: string) => {
if (item.startsWith('#')) {
return item;
}
return item
.replace(/,no-resolve/, '')
.replace(/\/\/.*$/, '')
.trim();
})
.join('\n');
});

// istanbul ignore next
engine.addFilter('yaml', (obj: JsonObject) => YAML.stringify(obj));

Expand Down
20 changes: 20 additions & 0 deletions test/template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,23 @@ test('quantumultx filter', t => {

t.is(result, '');
});

test('mellow filter 1', t => {
const body = `{{ str | mellow }}`;
const str = `IP-CIDR,67.198.55.0/24,Proxy,no-resolve // test rule`;
const result = templateEngine.renderString(body, {
str,
});

t.is(result, 'IP-CIDR,67.198.55.0/24,Proxy');
});

test('mellow filter 2', t => {
const body = `{{ str | mellow }}`;
const str = `URL-REGEX,xxxxxxxxxxxx`;
const result = templateEngine.renderString(body, {
str,
});

t.is(result, '');
});

0 comments on commit b646199

Please sign in to comment.