Skip to content
This repository has been archived by the owner on May 19, 2024. It is now read-only.

Commit

Permalink
Added test function
Browse files Browse the repository at this point in the history
  • Loading branch information
nblackburn committed Jun 6, 2019
1 parent 0c54d60 commit dc267f8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
19 changes: 18 additions & 1 deletion source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,21 @@ const match = (pattern, url, unsafe) => {
return zipObject(tokens, matches);
};

module.exports = match;
/**
* Test a url matches the given pattern.
*
* @param {string} pattern
* @param {string} url
*
* @return {boolean}
*/
const test = (pattern, url) => {
let expression = buildExpression(pattern);

return expression.test(url);
};

module.exports = {
test,
match,
};
6 changes: 3 additions & 3 deletions test/match.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const match = require('../source/index');
const { match } = require('../source/index');

describe('match', () => {
test('get required parameter', () => {
Expand All @@ -17,7 +17,7 @@ describe('match', () => {

test('do not allow large urls', () => {
let route = '';

for (let i = 0; i < 10001; i++) {
route += String(i % 10);
}
Expand All @@ -31,7 +31,7 @@ describe('match', () => {

test('throws for non-strings', () => {
let route = '';

let matcher = () => {
return match(0, route);
};
Expand Down
17 changes: 17 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { test: valid } = require('../source/index');

describe('test', () => {
test('test that a matching url returns true', () => {
const route = '/api/v1/users/1';
const matches = valid('/api/:version?/users/:id', route);

expect(matches).toBe(true);
});

test('test that a unmatching url returns false', () => {
const route = '/api/v1/films/1';
const matches = valid('/api/:version?/users/:id', route);

expect(matches).toBe(false);
});
});

0 comments on commit dc267f8

Please sign in to comment.