Skip to content

Commit 17d7612

Browse files
committed
feat(match API): implemented match method
provided `match` method for the first release
1 parent 481509c commit 17d7612

File tree

3 files changed

+13
-56
lines changed

3 files changed

+13
-56
lines changed

README.md

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,4 @@
44

55
## What is it?
66

7-
A functional route parsing, matching, and reversing library for Javascript in Node and the browser. Its api is inspired by [route-parser](https://github.com/rcs/route-parser), but is implemented in a functional way, don't relay in 'this' keyword.
8-
9-
10-
# Product Name
11-
> Short blurb about what your product does.
12-
[![NPM Version][npm-image]][npm-url]
13-
[![Build Status][travis-image]][travis-url]
14-
[![Downloads Stats][npm-downloads]][npm-url]
15-
One to two paragraph statement about your product and what it does.
16-
![](header.png)
17-
## Installation
18-
OS X & Linux:
19-
```sh
20-
npm install my-crazy-module --save
21-
```
22-
Windows:
23-
```sh
24-
edit autoexec.bat
25-
```
26-
## Usage example
27-
A few motivating and useful examples of how your product can be used. Spice this up with code blocks and potentially more screenshots.
28-
## Development setup
29-
Describe how to install all development dependencies and how to run an automated test-suite of some kind. Potentially do this for multiple platforms.
30-
```sh
31-
make install
32-
npm test
33-
```
34-
## Release History
35-
* 0.2.1
36-
* CHANGE: Update docs (module code remains unchanged)
37-
* 0.2.0
38-
* CHANGE: Remove `setDefaultXYZ()`
39-
* ADD: Add `init()`
40-
* 0.1.1
41-
* FIX: Crash when calling `baz()` (Thanks @GenerousContributorName!)
42-
* 0.1.0
43-
* The first proper release
44-
* CHANGE: Rename `foo()` to `bar()`
45-
* 0.0.1
46-
* Work in progress
47-
## Meta
48-
Your Name – [@YourTwitter](https://twitter.com/dbader_org)YourEmail@example.com
49-
Distributed under the XYZ license. See ``LICENSE`` for more information.
50-
[https://github.com/yourname/github-link](https://github.com/dbader/)
51-
[npm-image]: https://img.shields.io/npm/v/datadog-metrics.svg?style=flat-square
52-
[npm-url]: https://npmjs.org/package/datadog-metrics
53-
[npm-downloads]: https://img.shields.io/npm/dm/datadog-metrics.svg?style=flat-square
54-
[travis-image]: https://img.shields.io/travis/dbader/node-datadog-metrics/master.svg?style=flat-square
55-
[travis-url]: https://travis-ci.org/dbader/node-datadog-metrics
7+
A 10X FASTER and functional route parser, for Javascript in Node and the browser. Its api is inspired by [route-parser](https://github.com/rcs/route-parser), but is implemented in a functional way, don't relay in 'this' keyword.

src/index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const NAMED_SEGMENT = ':';
55
const ASSIGNMENT = '=';
66
const OR_SIGN = '|';
77
const MATCH_ANY = '*';
8+
const REGEX_MATCH_ANY = /^\/?([a-zA-Z0-9-_/])\/?$/;
89
const REGEX_SPACES = /\s/;
910
const VALID_SEGMENT = /^[a-z0-9_-]+$/i;
1011
const ANY_VALID_SEGMENT = '[a-zA-Z0-9_-]+';
@@ -17,9 +18,9 @@ module.exports = RouteParser;
1718
function RouteParser(route = '') {
1819
const { regex, path } = compileRoute(route);
1920

20-
return Object.freeze({ parse });
21+
return Object.freeze({ match });
2122

22-
function parse(route = '') {
23+
function match(route = '') {
2324
const regexResult = regex.exec(route);
2425

2526
if (regexResult === null) return false;
@@ -33,6 +34,10 @@ function RouteParser(route = '') {
3334
}
3435

3536
function compileRoute(route) {
37+
if (route === MATCH_ANY || route === DELIMETER) {
38+
return REGEX_MATCH_ANY;
39+
}
40+
3641
try {
3742
const segments = route
3843
.replace(REGEX_SPACES, '')
@@ -51,7 +56,7 @@ function createRegex(segments) {
5156
regexGroups.push(`(${segment[1] || segment[0]})`);
5257
return segment[0];
5358
});
54-
const regex = new RegExp(`^${regexGroups.join(DELIMETER)}$`);
59+
const regex = new RegExp(`^/?${regexGroups.join(DELIMETER)}$`);
5560

5661
return { path, regex };
5762
}

test/index.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,27 @@ describe('Route Parser', function () {
1010
it('should create a route parser with a `parse` method', function () {
1111
const route = RouteParser('my/test/:route');
1212

13-
expect(typeof route.parse).to.be.equal('function');
13+
expect(typeof route.match).to.be.equal('function');
1414
});
1515

1616
it('should parse a valid route', function () {
1717
const route = RouteParser('my/test/:route');
18-
const parsedRoute = route.parse('my/test/my-route');
18+
const parsedRoute = route.match('my/test/my-route');
1919

2020
expect(parsedRoute.route).to.be.equal('my-route');
2121
});
2222

2323
it('should parse a valid route with options', function () {
2424
const route = RouteParser('my/:action=(test|build)/:route');
25-
const parsedRoute = route.parse('my/test/my-route');
25+
const parsedRoute = route.match('my/test/my-route');
2626

2727
expect(parsedRoute.route).to.be.equal('my-route');
2828
expect(parsedRoute.action).to.be.equal('test');
2929
});
3030

3131
it('should parse a valid route with splat operator', function () {
3232
const route = RouteParser('*/:action=(test|build)/:route');
33-
const parsedRoute = route.parse('my/test/my-route');
33+
const parsedRoute = route.match('my/test/my-route');
3434

3535
expect(parsedRoute['0']).to.be.equal('my');
3636
});

0 commit comments

Comments
 (0)