File tree Expand file tree Collapse file tree 3 files changed +24
-6
lines changed Expand file tree Collapse file tree 3 files changed +24
-6
lines changed Original file line number Diff line number Diff line change 68
68
"typescript" : " ^4.1.3"
69
69
},
70
70
"dependencies" : {
71
- "multimatch" : " ^5.0.0" ,
72
71
"node-ray" : " ^1.4.0"
73
72
}
74
73
}
Original file line number Diff line number Diff line change 1
1
import { PackageInfo } from './PackageInfo' ;
2
- import multimatch from 'multimatch' ;
3
2
4
3
// @ts -ignore
5
4
export const createPackageMetaProperty = obj => {
@@ -27,13 +26,27 @@ export const encodeHtmlEntities = (str: string) => {
27
26
return str . replace ( regex , m => `&${ escapeChars [ m ] } ;` ) ;
28
27
} ;
29
28
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
+
30
43
export const filterObjectByKeys = ( obj : any , includeKeyPatterns : string [ ] ) => {
31
44
const result : any = { } ;
32
45
33
46
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
+ }
37
50
} ) ;
38
51
39
52
return result ;
Original file line number Diff line number Diff line change 1
1
/* eslint-disable no-undef */
2
2
3
- import { createPackageMetaProperty , encodeHtmlEntities } from '../../src/shared/helpers' ;
3
+ import { createPackageMetaProperty , encodeHtmlEntities , matchPattern } from '../../src/shared/helpers' ;
4
4
5
5
it ( 'creates a package meta property' , ( ) => {
6
6
const obj : any = { $rayMeta : null } ;
@@ -15,3 +15,9 @@ it('creates a package meta property', () => {
15
15
it ( 'encodes html entities' , ( ) => {
16
16
expect ( encodeHtmlEntities ( '<p>' ) ) . toBe ( '<p>' ) ;
17
17
} ) ;
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments