diff --git a/source/index.js b/source/index.js index 7b56181..42907c7 100644 --- a/source/index.js +++ b/source/index.js @@ -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, +}; diff --git a/test/match.js b/test/match.js index ca34cfa..285a4b4 100644 --- a/test/match.js +++ b/test/match.js @@ -1,4 +1,4 @@ -const match = require('../source/index'); +const { match } = require('../source/index'); describe('match', () => { test('get required parameter', () => { @@ -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); } @@ -31,7 +31,7 @@ describe('match', () => { test('throws for non-strings', () => { let route = ''; - + let matcher = () => { return match(0, route); }; diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..a515614 --- /dev/null +++ b/test/test.js @@ -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); + }); +});