From 8f095497d678c2ec3495a99ab3928748731e73ee Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Thu, 12 Sep 2024 11:14:03 -0700 Subject: [PATCH] Add error on bad input values --- index.js | 4 ++++ test.js | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/index.js b/index.js index 1150335..750c2bf 100644 --- a/index.js +++ b/index.js @@ -65,6 +65,10 @@ function pathToRegexp(path, keys, options) { return new RegExp(path.join('|'), flags); } + if (typeof path !== 'string') { + throw new TypeError('path must be a string, array of strings, or regular expression'); + } + path = path.replace( /\\.|(\/)?(\.)?:(\w+)(\(.*?\))?(\*)?(\?)?|[.*]|\/\(/g, function (match, slash, format, key, capture, star, optional, offset) { diff --git a/test.js b/test.js index 63ecb86..d239195 100644 --- a/test.js +++ b/test.js @@ -2,6 +2,12 @@ var pathToRegExp = require('./'); var assert = require('assert'); describe('path-to-regexp', function () { + it('should throw on invalid input', function () { + assert.throws(function () { + pathToRegExp(function () {}); + }, /path must be a string, array of strings, or regular expression/); + }); + describe('strings', function () { it('should match simple paths', function () { var params = [];