Skip to content

Commit

Permalink
fix: Match properly when using / in include/exclude (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolauska authored Nov 3, 2020
1 parent 68ba692 commit 861e9d6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 deletions.
34 changes: 15 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
"handlebars": "^4.7.6",
"inquirer": "^7.1.0",
"js-yaml": "^3.13.1",
"micromatch": "^4.0.2",
"picomatch": "^2.2.2",
"yargs": "^15.1.0"
},
"devDependencies": {
"@types/diff": "^4.0.2",
"@types/inquirer": "^6.5.0",
"@types/js-yaml": "^3.12.2",
"@types/micromatch": "^4.0.1",
"@types/node": "^13.7.0",
"@types/picomatch": "^2.2.1",
"@types/yargs": "^15.0.3",
"@typescript-eslint/eslint-plugin": "^2.20.0",
"@typescript-eslint/parser": "^2.20.0",
Expand Down
7 changes: 7 additions & 0 deletions src/lib/utils/match.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ test('Validate match log', t => {
const excl = ['*dev*'];
t.deepEqual(match(str, inc, excl), false);
});

test('Validate log group match with /', t => {
const str = '/aws/lambda/lambda-dev';
const inc: string[] = [];
const excl = ['/aws/*/lambda-dev'];
t.deepEqual(match(str, inc, excl), false);
});
23 changes: 20 additions & 3 deletions src/lib/utils/match.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import { isMatch } from 'micromatch';
import { isMatch } from 'picomatch';

// This fix is needed for picomatch library
// Otherwise there will be error when matching log groups for example
function fixMatchString(str: string): string {
return str.replace(/\//g, '-');
}

function matchIncluded(str: string, included: string[]): boolean {
return included.length === 0 || isMatch(str, included);
}

function matchExcluded(str: string, excluded: string[]): boolean {
return excluded.length === 0 || !isMatch(str, excluded);
}

export default function match(str: string, include: string[], exclude: string[]): boolean {
const matchStr = str.replace(/\//g, '-');
return (include.length === 0 || isMatch(matchStr, include)) && (exclude.length === 0 || !isMatch(matchStr, exclude));
const matchStr = fixMatchString(str);
const included = include.map(str => fixMatchString(str));
const excluded = exclude.map(str => fixMatchString(str));

return matchIncluded(matchStr, included) && matchExcluded(matchStr, excluded);
}

0 comments on commit 861e9d6

Please sign in to comment.