From b33cac4deaed1bdab8dae7e2dedc2744df55331b Mon Sep 17 00:00:00 2001 From: Jamie Davis Date: Sat, 24 Mar 2018 00:48:44 -0400 Subject: [PATCH] handle undefined string Problem: As reported in #19, PR #18 changed behavior on: - undefined string - non-strings Solution: Revert to pre-#18 behavior on non-strings: return false. Test: I added test cases to clarify this behavior. It shouldn't happen again. --- index.js | 4 ++++ test/index.js | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/index.js b/index.js index 1d3e349..3ea3d20 100644 --- a/index.js +++ b/index.js @@ -24,6 +24,10 @@ var nonLocalhostDomainRE = /^[^\s\.]+\.\S{2,}$/; */ function isUrl(string){ + if (typeof string !== 'string') { + return false; + } + var match = string.match(protocolAndDomainRE); if (!match) { return false; diff --git a/test/index.js b/test/index.js index 52cfacb..404e1b6 100644 --- a/test/index.js +++ b/test/index.js @@ -118,6 +118,22 @@ describe('is-url', function () { it('google.com', function () { assert(!url('google.com')); }); + + it('empty', function () { + assert(!url('')); + }); + + it('undef', function () { + assert(!url(undefined)); + }); + + it('object', function () { + assert(!url({})); + }); + + it('re', function () { + assert(!url(/abc/)); + }); }); describe('redos', function () {