From b508685c8741461e9d4209fc90275f08a93e97b7 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Tue, 7 Jun 2016 14:35:21 -0700 Subject: [PATCH] Fix: Ensure final result conforms to predicate --- index.js | 6 +++--- test/index.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 3561d97..b7fbe16 100644 --- a/index.js +++ b/index.js @@ -26,11 +26,11 @@ function normalize(type, value) { var result = value.apply(null, args); - if (typeof result !== type) { - return null; + if (conforms(type, result)) { + return result; } - return result; + return null; } function conforms(type, value) { diff --git a/test/index.js b/test/index.js index c695756..9847d01 100644 --- a/test/index.js +++ b/test/index.js @@ -80,6 +80,22 @@ describe('normalize', function() { done(); }); + it('checks the result of function against predicate', function(done) { + var expected = 'test string'; + var called = false; + var predicate = function(value) { + called = true; + return (typeof value === 'string'); + }; + var value = function() { + return expected; + }; + var result = normalize(predicate, value); + expect(result).toEqual(expected); + expect(called).toEqual(true); + done(); + }); + it('calls the function, passing extra arguments', function(done) { var type = 'string'; var expected = 'test string'; @@ -100,6 +116,18 @@ describe('normalize', function() { expect(result).toEqual(null); done(); }); + + it('returns null if the result of function does not satisfy predicate', function(done) { + var predicate = function(value) { + return (typeof value === 'string'); + }; + var value = function() { + return 123; + }; + var result = normalize(predicate, value); + expect(result).toEqual(null); + done(); + }); }); describe('normalize.object', function() {