Skip to content

Commit

Permalink
Fix: Ensure final result conforms to predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jun 7, 2016
1 parent 75c41b5 commit b508685
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
28 changes: 28 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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() {
Expand Down

0 comments on commit b508685

Please sign in to comment.