Skip to content

Commit 0d21b5e

Browse files
committed
addressing comments and creating path pattern class
1 parent 6b261c1 commit 0d21b5e

File tree

6 files changed

+473
-305
lines changed

6 files changed

+473
-305
lines changed

spec/utilities/path-pattern.spec.ts

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// The MIT License (MIT)
2+
//
3+
// Copyright (c) 2022 Firebase
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
import { expect } from 'chai';
23+
import * as pathPattern from '../../src/utilities/path-pattern';
24+
25+
describe('path-pattern', () => {
26+
describe('trimParam', () => {
27+
it('should trim a capture param without equals', () => {
28+
expect(pathPattern.trimParam('{something}')).to.equal('something');
29+
});
30+
31+
it('should trim a capture param with equals', () => {
32+
expect(pathPattern.trimParam('{something=*}')).to.equal('something');
33+
});
34+
});
35+
36+
describe('extractMatches', () => {
37+
it('should parse without multi segment', () => {
38+
const pp = new pathPattern.PathPattern('{a}/something/else/{b}/end/{c}');
39+
40+
expect(
41+
pp.extractMatches('match_a/something/else/match_b/end/match_c')
42+
).to.deep.equal({
43+
a: 'match_a',
44+
b: 'match_b',
45+
c: 'match_c',
46+
});
47+
});
48+
49+
it('should parse multi segment with params after', () => {
50+
const pp = new pathPattern.PathPattern(
51+
'something/**/else/{a}/hello/{b}/world'
52+
);
53+
54+
expect(
55+
pp.extractMatches('something/is/a/thing/else/nothing/hello/user/world')
56+
).to.deep.equal({
57+
a: 'nothing',
58+
b: 'user',
59+
});
60+
});
61+
62+
it('should parse multi segment param with params after', () => {
63+
const pp = new pathPattern.PathPattern(
64+
'something/{path=**}/else/{a}/hello/{b}/world'
65+
);
66+
67+
expect(
68+
pp.extractMatches('something/is/a/thing/else/nothing/hello/user/world')
69+
).to.deep.equal({
70+
path: 'is/a/thing',
71+
a: 'nothing',
72+
b: 'user',
73+
});
74+
});
75+
76+
it('should parse multi segment with params before', () => {
77+
const pp = new pathPattern.PathPattern('{a}/something/{b}/**/end');
78+
79+
expect(
80+
pp.extractMatches(
81+
'match_a/something/match_b/thing/else/nothing/hello/user/end'
82+
)
83+
).to.deep.equal({
84+
a: 'match_a',
85+
b: 'match_b',
86+
});
87+
});
88+
89+
it('should parse multi segment param with params before', () => {
90+
const pp = new pathPattern.PathPattern('{a}/something/{b}/{path=**}/end');
91+
92+
expect(
93+
pp.extractMatches(
94+
'match_a/something/match_b/thing/else/nothing/hello/user/end'
95+
)
96+
).to.deep.equal({
97+
a: 'match_a',
98+
b: 'match_b',
99+
path: 'thing/else/nothing/hello/user',
100+
});
101+
});
102+
103+
it('should parse multi segment with params before and after', () => {
104+
const pp = new pathPattern.PathPattern('{a}/something/**/{b}/end');
105+
106+
expect(
107+
pp.extractMatches(
108+
'match_a/something/thing/else/nothing/hello/user/match_b/end'
109+
)
110+
).to.deep.equal({
111+
a: 'match_a',
112+
b: 'match_b',
113+
});
114+
});
115+
116+
it('should parse multi segment param with params before', () => {
117+
const pp = new pathPattern.PathPattern('{a}/something/{path=**}/{b}/end');
118+
119+
expect(
120+
pp.extractMatches(
121+
'match_a/something/thing/else/nothing/hello/user/match_b/end'
122+
)
123+
).to.deep.equal({
124+
a: 'match_a',
125+
b: 'match_b',
126+
path: 'thing/else/nothing/hello/user',
127+
});
128+
});
129+
130+
// handle an instance param
131+
it('should parse an instance', () => {
132+
const pp = new pathPattern.PathPattern('{a}-something-{b}-else-{c}');
133+
134+
expect(
135+
pp.extractMatches('match_a-something-match_b-else-match_c')
136+
).to.deep.equal({});
137+
138+
const anotherPP = new pathPattern.PathPattern('{a}');
139+
140+
expect(anotherPP.extractMatches('match_a')).to.deep.equal({
141+
a: 'match_a',
142+
});
143+
});
144+
});
145+
});

spec/v2/providers/database.spec.ts

Lines changed: 20 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
// SOFTWARE.
2222

2323
import { expect } from 'chai';
24+
import { PathPattern } from '../../../src/utilities/path-pattern';
2425
import * as database from '../../../src/v2/providers/database';
2526

2627
const RAW_RTDB_EVENT: database.RawRTDBCloudEvent = {
@@ -41,144 +42,6 @@ const RAW_RTDB_EVENT: database.RawRTDBCloudEvent = {
4142
type: 'type',
4243
};
4344

44-
describe('matchParams', () => {
45-
it('should parse without multi segment', () => {
46-
const params = {};
47-
48-
database.matchParams(
49-
'{a}/something/else/{b}/end/{c}'.split('/'),
50-
['{a}', '{b}', '{c}'],
51-
'match_a/something/else/match_b/end/match_c'.split('/'),
52-
params
53-
);
54-
55-
expect(params).to.deep.equal({
56-
a: 'match_a',
57-
b: 'match_b',
58-
c: 'match_c',
59-
});
60-
});
61-
62-
it('should parse multi segment with params after', () => {
63-
const params = {};
64-
65-
database.matchParams(
66-
'something/**/else/{a}/hello/{b}/world'.split('/'),
67-
['{a}', '{b}'],
68-
'something/is/a/thing/else/nothing/hello/user/world'.split('/'),
69-
params
70-
);
71-
72-
expect(params).to.deep.equal({
73-
a: 'nothing',
74-
b: 'user',
75-
});
76-
});
77-
78-
it('should parse multi segment param with params after', () => {
79-
const params = {};
80-
81-
database.matchParams(
82-
'something/{path=**}/else/{a}/hello/{b}/world'.split('/'),
83-
['{path=**}', '{a}', '{b}'],
84-
'something/is/a/thing/else/nothing/hello/user/world'.split('/'),
85-
params
86-
);
87-
88-
expect(params).to.deep.equal({
89-
path: 'is/a/thing',
90-
a: 'nothing',
91-
b: 'user',
92-
});
93-
});
94-
95-
it('should parse multi segment with params before', () => {
96-
const params = {};
97-
98-
database.matchParams(
99-
'{a}/something/{b}/**/end'.split('/'),
100-
['{a}', '{b}'],
101-
'match_a/something/match_b/thing/else/nothing/hello/user/end'.split('/'),
102-
params
103-
);
104-
105-
expect(params).to.deep.equal({
106-
a: 'match_a',
107-
b: 'match_b',
108-
});
109-
});
110-
111-
it('should parse multi segment param with params before', () => {
112-
const params = {};
113-
114-
database.matchParams(
115-
'{a}/something/{b}/{path=**}/end'.split('/'),
116-
['{a}', '{b}', '{path=**}'],
117-
'match_a/something/match_b/thing/else/nothing/hello/user/end'.split('/'),
118-
params
119-
);
120-
121-
expect(params).to.deep.equal({
122-
a: 'match_a',
123-
b: 'match_b',
124-
path: 'thing/else/nothing/hello/user',
125-
});
126-
});
127-
128-
it('should parse multi segment with params before and after', () => {
129-
const params = {};
130-
131-
database.matchParams(
132-
'{a}/something/**/{b}/end'.split('/'),
133-
['{a}', '{b}'],
134-
'match_a/something/thing/else/nothing/hello/user/match_b/end'.split('/'),
135-
params
136-
);
137-
138-
expect(params).to.deep.equal({
139-
a: 'match_a',
140-
b: 'match_b',
141-
});
142-
});
143-
144-
it('should parse multi segment param with params before', () => {
145-
const params = {};
146-
147-
database.matchParams(
148-
'{a}/something/{path=**}/{b}/end'.split('/'),
149-
['{a}', '{path=**}', '{b}'],
150-
'match_a/something/thing/else/nothing/hello/user/match_b/end'.split('/'),
151-
params
152-
);
153-
154-
expect(params).to.deep.equal({
155-
a: 'match_a',
156-
b: 'match_b',
157-
path: 'thing/else/nothing/hello/user',
158-
});
159-
});
160-
161-
// handle an instance param
162-
it('should parse an instance', () => {
163-
const params = {};
164-
165-
database.matchParams(
166-
['{a}-something-{b}-else-{c}'],
167-
['{a}', '{b}', '{c}'],
168-
['match_a-something-match_b-else-match_c'],
169-
params
170-
);
171-
172-
expect(params).to.deep.equal({});
173-
174-
database.matchParams(['{a}'], ['{a}'], ['match_a'], params);
175-
176-
expect(params).to.deep.equal({
177-
a: 'match_a',
178-
});
179-
});
180-
});
181-
18245
describe('makeParams', () => {
18346
it('should make params with basic path', () => {
18447
const event: database.RawRTDBCloudEvent = {
@@ -187,7 +50,11 @@ describe('makeParams', () => {
18750
};
18851

18952
expect(
190-
database.makeParams(event, '{a}/something/else/*/end/{b}', '*')
53+
database.makeParams(
54+
event,
55+
new PathPattern('{a}/something/else/*/end/{b}'),
56+
new PathPattern('*')
57+
)
19158
).to.deep.equal({
19259
a: 'match_a',
19360
b: 'match_b',
@@ -201,7 +68,11 @@ describe('makeParams', () => {
20168
};
20269

20370
expect(
204-
database.makeParams(event, 'something/**/else/{a}/hello/{b}/world', '*')
71+
database.makeParams(
72+
event,
73+
new PathPattern('something/**/else/{a}/hello/{b}/world'),
74+
new PathPattern('*')
75+
)
20576
).to.deep.equal({
20677
a: 'match_a',
20778
b: 'match_b',
@@ -217,8 +88,8 @@ describe('makeParams', () => {
21788
expect(
21889
database.makeParams(
21990
event,
220-
'something/{path=**}/else/{a}/hello/{b}/world',
221-
'*'
91+
new PathPattern('something/{path=**}/else/{a}/hello/{b}/world'),
92+
new PathPattern('*')
22293
)
22394
).to.deep.equal({
22495
path: 'is/a/thing',
@@ -236,8 +107,8 @@ describe('makeParams', () => {
236107
expect(
237108
database.makeParams(
238109
event,
239-
'something/{path=**}/else/{a}/hello/{b}/world',
240-
'{inst}'
110+
new PathPattern('something/{path=**}/else/{a}/hello/{b}/world'),
111+
new PathPattern('{inst}')
241112
)
242113
).to.deep.equal({
243114
path: 'is/a/thing',
@@ -279,8 +150,7 @@ describe('onOperation', () => {
279150
const func = database.onOperation(
280151
database.writtenEventType,
281152
'/foo/{bar}/',
282-
(event) => 2,
283-
true
153+
(event) => 2
284154
);
285155

286156
expect(func.__endpoint).to.deep.equal({
@@ -302,8 +172,7 @@ describe('onOperation', () => {
302172
const func = database.onOperation(
303173
database.createdEventType,
304174
'/foo/{bar}/',
305-
(event) => 2,
306-
true
175+
(event) => 2
307176
);
308177

309178
expect(func.__endpoint).to.deep.equal({
@@ -325,8 +194,7 @@ describe('onOperation', () => {
325194
const func = database.onOperation(
326195
database.updatedEventType,
327196
'/foo/{bar}/',
328-
(event) => 2,
329-
true
197+
(event) => 2
330198
);
331199

332200
expect(func.__endpoint).to.deep.equal({
@@ -348,8 +216,7 @@ describe('onOperation', () => {
348216
const func = database.onOperation(
349217
database.deletedEventType,
350218
'/foo/{bar}/',
351-
(event) => 2,
352-
true
219+
(event) => 2
353220
);
354221

355222
expect(func.__endpoint).to.deep.equal({
@@ -377,8 +244,7 @@ describe('onOperation', () => {
377244
cpu: 'gcf_gen1',
378245
minInstances: 2,
379246
},
380-
(event) => 2,
381-
true
247+
(event) => 2
382248
);
383249

384250
expect(func.__endpoint).to.deep.equal({

0 commit comments

Comments
 (0)