diff --git a/src/model/functionMap.ts b/src/model/functionMap.ts index 1616547..0c3ef89 100644 --- a/src/model/functionMap.ts +++ b/src/model/functionMap.ts @@ -37,6 +37,7 @@ export class FunctionMap { fm.addFunction('keyGet2', util.keyGet2Func); fm.addFunction('keyMatch3', util.keyMatch3Func); fm.addFunction('keyMatch4', util.keyMatch4Func); + fm.addFunction('keyMatch5', util.keyMatch5Func); fm.addFunction('regexMatch', util.regexMatchFunc); fm.addFunction('ipMatch', util.ipMatchFunc); fm.addFunction('globMatch', util.globMatch); diff --git a/src/util/builtinOperators.ts b/src/util/builtinOperators.ts index c1869c2..cd1df51 100644 --- a/src/util/builtinOperators.ts +++ b/src/util/builtinOperators.ts @@ -233,6 +233,26 @@ function keyMatch4Func(...args: any[]): boolean { return keyMatch4(name1, name2); } +// KeyMatch determines whether key1 matches the pattern of key2 and ignores the parameters in key2. +// For example, "/foo/bar?status=1&type=2" matches "/foo/bar" +function KeyMatch5(key1: string, key2: string): boolean { + const i: number = key1.indexOf('?'); + if (i === -1) { + return key1 === key2; + } + + return key1.slice(0, i) === key2; +} + +// keyMatch5Func is the wrapper for KeyMatch5. +function keyMatch5Func(...args: any[]): boolean { + const [arg0, arg1] = args; + const name1: string = (arg0 || '').toString(); + const name2: string = (arg1 || '').toString(); + + return KeyMatch5(name1, name2); +} + // regexMatchFunc is the wrapper for regexMatch. function regexMatchFunc(...args: any[]): boolean { const [arg0, arg1] = args; @@ -326,5 +346,6 @@ export { ipMatchFunc, generateGFunction, keyMatch4Func, + keyMatch5Func, globMatch, }; diff --git a/test/util.test.ts b/test/util.test.ts index 02f97d1..fe03076 100644 --- a/test/util.test.ts +++ b/test/util.test.ts @@ -92,6 +92,15 @@ test('test keyMatch4Func', () => { expect(util.keyMatch4Func('/parent/123/child/456', '/parent/{id}/child/{id}/book/{id}')).toEqual(false); }); +test('test keyMatch5Func', () => { + expect(util.keyMatch5Func('/parent/child?status=1&type=2', '/parent/child')).toEqual(true); + expect(util.keyMatch5Func('/parent?status=1&type=2', '/parent/child')).toEqual(false); + + expect(util.keyMatch5Func('/parent/child/?status=1&type=2', '/parent/child/')).toEqual(true); + expect(util.keyMatch5Func('/parent/child/?status=1&type=2', '/parent/child')).toEqual(false); + expect(util.keyMatch5Func('/parent/child?status=1&type=2', '/parent/child/')).toEqual(false); +}); + test('test ipMatchFunc', () => { expect(util.ipMatchFunc('::1', '::0:1')).toEqual(true); expect(util.ipMatchFunc('192.168.1.1', '192.168.1.1')).toEqual(true);