Skip to content

Commit

Permalink
feat(fast-path-parse): add <id> like param support
Browse files Browse the repository at this point in the history
  • Loading branch information
dalisoft committed Sep 1, 2024
1 parent 6fc6ff2 commit bf92d30
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/fast-path-parse/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ parsePath('/user/john');
| `/foo/bar_*` | ⚠️ | 2 | 80% |
| `/foo/bar-*` | ⚠️ | 2 | 80% |
| `/foo/bar*` | ⚠️ | 2 | 80% |
| `/foo/bar/<id>` | 🕖 | 3 | 90% |
| `/foo/bar/<id>` | | 3 | 90% |
| `/foo/task/:bar` || 4 | 80% |
| `/foo/:task/:bar` || 4 | 70% |
| `/foo/:bar` || 5 | 90% |
Expand Down
8 changes: 7 additions & 1 deletion packages/fast-path-parse/tests/match-safe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ describe('fast-path-parse/match safe', async () => {
assert.equal(match('/foo/bar')('/foo/bar/'), true);
assert.equal(match('/foo/bar/')('/foo/bar/'), true);
});
await it('Simple segmenting', () => {
await it('Simple segmenting, `:id`', () => {
assert.equal(match('/foo/:id/bar')('/foo/123/bar'), true);
assert.equal(match('/foo/:id/bar/')('/foo/123/bar'), true);
assert.equal(match('/foo/:id/bar')('/foo/123/bar/'), true);
assert.equal(match('/foo/:id/bar/')('/foo/123/bar/'), true);
});
await it('Simple segmenting, `<id>`', () => {
assert.equal(match('/foo/<id>/bar')('/foo/123/bar'), true);
assert.equal(match('/foo/<id>/bar/')('/foo/123/bar'), true);
assert.equal(match('/foo/<id>/bar')('/foo/123/bar/'), true);
assert.equal(match('/foo/<id>/bar/')('/foo/123/bar/'), true);
});
await it('Simple two segmenting', () => {
assert.equal(match('/foo/:id/bar/:task')('/foo/123/bar/run'), true);
assert.equal(match('/foo/:id/bar/:task/')('/foo/123/bar/run'), true);
Expand Down
8 changes: 7 additions & 1 deletion packages/fast-path-parse/tests/match-unsafe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ describe('fast-path-parse/match unsafe', async () => {
assert.equal(match('/foo/bar')('/foo/bar/'), true);
assert.equal(match('/foo/bar/')('/foo/bar/'), true);
});
await it('Simple segmenting', () => {
await it('Simple segmenting, `:id`', () => {
assert.equal(match('/foo/:id/bar')('/foo/123/bar'), true);
assert.equal(match('/foo/:id/bar/')('/foo/123/bar'), true);
assert.equal(match('/foo/:id/bar')('/foo/123/bar/'), true);
assert.equal(match('/foo/:id/bar/')('/foo/123/bar/'), true);
});
await it('Simple segmenting, `<id>`', () => {
assert.equal(match('/foo/<id>/bar')('/foo/123/bar'), true);
assert.equal(match('/foo/<id>/bar/')('/foo/123/bar'), true);
assert.equal(match('/foo/<id>/bar')('/foo/123/bar/'), true);
assert.equal(match('/foo/<id>/bar/')('/foo/123/bar/'), true);
});
await it('Simple two segmenting', () => {
assert.equal(match('/foo/:id/bar/:task')('/foo/123/bar/run'), true);
assert.equal(match('/foo/:id/bar/:task/')('/foo/123/bar/run'), true);
Expand Down
16 changes: 15 additions & 1 deletion packages/fast-path-parse/tests/parse-safe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('fast-path-parse/parse safe', async () => {
assert.deepStrictEqual(parsePathname('/foo/bar')('/foo/bar/'), {});
assert.deepStrictEqual(parsePathname('/foo/bar/')('/foo/bar/'), {});
});
await it('Simple segmenting', () => {
await it('Simple segmenting, `:id`', () => {
assert.deepStrictEqual(parsePathname('/foo/:id/bar')('/foo/123/bar'), {
id: '123'
});
Expand All @@ -25,6 +25,20 @@ describe('fast-path-parse/parse safe', async () => {
id: '123'
});
});
await it('Simple segmenting, `<id>`', () => {
assert.deepStrictEqual(parsePathname('/foo/<id>/bar')('/foo/123/bar'), {
id: '123'
});
assert.deepStrictEqual(parsePathname('/foo/<id>/bar/')('/foo/123/bar'), {
id: '123'
});
assert.deepStrictEqual(parsePathname('/foo/<id>/bar')('/foo/123/bar/'), {
id: '123'
});
assert.deepStrictEqual(parsePathname('/foo/<id>/bar/')('/foo/123/bar/'), {
id: '123'
});
});
await it('Simple two segmenting', () => {
assert.deepStrictEqual(
parsePathname('/foo/:id/bar/:task')('/foo/123/bar/run'),
Expand Down
7 changes: 6 additions & 1 deletion packages/fast-path-parse/tests/parse-unsafe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ describe('fast-path-parse/parse unsafe', async () => {
await it('No segmenting', () => {
assert.deepStrictEqual(compilePathname('/foo/bar')('/foo/bar'), {});
});
await it('Simple segmenting', () => {
await it('Simple segmenting, `:id`', () => {
assert.deepStrictEqual(compilePathname('/foo/:id/bar')('/foo/123/bar'), {
id: '123'
});
});
await it('Simple segmenting, `<id>`', () => {
assert.deepStrictEqual(compilePathname('/foo/<id>/bar')('/foo/123/bar'), {
id: '123'
});
});
await it('Simple two segmenting', () => {
assert.deepStrictEqual(
compilePathname('/foo/:id/bar/:task')('/foo/123/bar/run'),
Expand Down
54 changes: 53 additions & 1 deletion packages/fast-path-parse/tests/segment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('segments slice', async () => {
length: 2
});
});
await it('simple dynamic route parse', () => {
await it('simple dynamic route parse, `:id`', () => {
assert.deepStrictEqual(segmentsSlice('/foo/:id'), {
filled: [
{
Expand Down Expand Up @@ -95,4 +95,56 @@ describe('segments slice', async () => {
length: 2
});
});
await it('simple dynamic route parse, `<id>`', () => {
assert.deepStrictEqual(segmentsSlice('/foo/<id>'), {
filled: [
{
name: 'id',
segment: true,
last: true,
position: 5
}
],
segments: [
{
name: 'foo',
segment: false,
last: false,
position: 1
},
{
name: 'id',
segment: true,
last: true,
position: 5
}
],
length: 2
});
assert.deepStrictEqual(segmentsSlice('/foo/<id>/'), {
filled: [
{
name: 'id',
segment: true,
last: true,
position: 5
}
],
segments: [
{
name: 'foo',
segment: false,
last: false,
position: 1
},
{
name: 'id',
segment: true,
last: true,
position: 5
}
],
length: 2
});
});
});
8 changes: 8 additions & 0 deletions packages/fast-path-parse/utils/segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@
* @param {(() => number)} getIndex
* @param {number} position
*/
// eslint-disable-next-line complexity
const parseSegment = (name, getIndex, position = 1) => {
if (name === '*' || name === '(.*)') {
return { name: `*${getIndex()}`, segment: true, position: position + 2 };
}
if (name.charAt(0) === ':') {
return { name: name.substring(1), segment: true, position: position + 2 };
}
if (name.charAt(0) === '<' && name.charAt(name.length - 1) === '>') {
return {
name: name.substring(1, name.length - 1),
segment: true,
position: position + 2
};
}

return { name, segment: false, position: position + name.length + 1 };
};
Expand Down

0 comments on commit bf92d30

Please sign in to comment.