Skip to content

Commit

Permalink
feat: #357 Support keyMatch5 (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
klren0312 authored Apr 15, 2022
1 parent 7803ccb commit e6a6d8a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/model/functionMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 21 additions & 0 deletions src/util/builtinOperators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -326,5 +346,6 @@ export {
ipMatchFunc,
generateGFunction,
keyMatch4Func,
keyMatch5Func,
globMatch,
};
9 changes: 9 additions & 0 deletions test/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e6a6d8a

Please sign in to comment.