Skip to content

Commit 1c9df49

Browse files
patinthehatPatrick
authored andcommitted
remove multimatch package and use custom pattern matching function instead
1 parent 0c93957 commit 1c9df49

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
"typescript": "^4.1.3"
6969
},
7070
"dependencies": {
71-
"multimatch": "^5.0.0",
7271
"node-ray": "^1.4.0"
7372
}
7473
}

src/shared/helpers.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { PackageInfo } from './PackageInfo';
2-
import multimatch from 'multimatch';
32

43
// @ts-ignore
54
export const createPackageMetaProperty = obj => {
@@ -27,13 +26,27 @@ export const encodeHtmlEntities = (str: string) => {
2726
return str.replace(regex, m => `&${escapeChars[m]};`);
2827
};
2928

29+
export const matchPattern = (str: string, patterns: string[]): boolean => {
30+
for (let pattern of patterns) {
31+
pattern = pattern.replace(/\*/g, '.*');
32+
33+
const re = new RegExp(pattern, 'g');
34+
35+
if (re.exec(str)) {
36+
return true;
37+
}
38+
}
39+
40+
return false;
41+
};
42+
3043
export const filterObjectByKeys = (obj: any, includeKeyPatterns: string[]) => {
3144
const result: any = {};
3245

3346
Object.keys(obj).forEach(key => {
34-
multimatch(key, includeKeyPatterns).forEach(match => {
35-
result[match] = obj[match];
36-
});
47+
if (matchPattern(key, includeKeyPatterns)) {
48+
result[key] = obj[key];
49+
}
3750
});
3851

3952
return result;

tests/shared/helpers.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-undef */
22

3-
import { createPackageMetaProperty, encodeHtmlEntities } from '../../src/shared/helpers';
3+
import { createPackageMetaProperty, encodeHtmlEntities, matchPattern } from '../../src/shared/helpers';
44

55
it('creates a package meta property', () => {
66
const obj: any = { $rayMeta: null };
@@ -15,3 +15,9 @@ it('creates a package meta property', () => {
1515
it('encodes html entities', () => {
1616
expect(encodeHtmlEntities('<p>')).toBe('&lt;p&gt;');
1717
});
18+
19+
it('matches patterns', () => {
20+
expect(matchPattern('test', ['te*', 'a*'])).toBeTruthy();
21+
expect(matchPattern('test', ['*t', 't*a'])).toBeTruthy();
22+
expect(matchPattern('test', ['x*', 'a*'])).toBeFalsy();
23+
});

0 commit comments

Comments
 (0)