Skip to content

Commit

Permalink
Fix: Return incorrect result when type array contains "function" or "…
Browse files Browse the repository at this point in the history
…object"
  • Loading branch information
sttk committed Jan 16, 2022
1 parent 2e4071c commit ffeab72
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,21 @@ function normalize(coercer, value) {
if (coercer === 'function') {
return value;
}
if (Array.isArray(coercer) && coercer.indexOf('function') >= 0) {
return value;
}
value = value.apply(this, slice(arguments, 2));
}

if (typeof value === 'object') {
if (coercer === 'object') {
return value;
}
if (Array.isArray(coercer) && coercer.indexOf('object') >= 0) {
return value;
}
}

return coerce(this, coercer, value);
}

Expand Down
75 changes: 74 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,87 @@ describe('normalize', function () {
done();
});

it('compares each type and the type of the value', function (done) {
it('compares each type and the type of the value (string)', function (done) {
var type = ['number', 'string', 'object'];
var value = 'test string';
var result = normalize(type, value);
expect(result).toBe(value);
done();
});

it('compares each type and the type of the value (function)', function(done) {
var type = ['string', 'function'];
var value = function() {};
var result = normalize(type, value);
expect(result).toBe(value);
done();
});

it('compares each type and the type of the value (number)', function(done) {
var type = ['string', 'number'];
var value = 123;
var result = normalize(type, value);
expect(result).toBe(value);
done();
});

it('compares each type and the type of the value (boolean)', function(done) {
var type = ['string', 'boolean'];
var value = true;
var result = normalize(type, value);
expect(result).toBe(value);
done();
});

it('compares each type and the type of the value (object)', function(done) {
var type = ['string', 'object'];
var value = { a: 1 };
var result = normalize(type, value);
expect(result).toBe(value);

value = null;
result = normalize(type, value);
expect(result).toBe(value);
done();
});

it('compares each type and the type of the value (date)', function(done) {
var type = ['string', 'object'];
var value = new Date();
var result = normalize(type, value);
expect(result).toBe(value);
done();
});

it('compares each type and the type of the value (null)', function(done) {
var type = ['string', 'object'];
var value = null;
var result = normalize(type, value);
expect(result).toBe(value);
done();
});

it('compares each type and the type of the value (undefined)', function(done) {
var type = ['string', 'undefined'];
var value = undefined;
var result = normalize(type, value);
expect(result).toBe(value);
done();
});

it('compares each type and the type of the value (symbol)', function(done) {
if (!global.Symbol) {
console.log('Only available on platforms that support Symbol');
this.skip();
return;
}
var type = ['string', 'symbol'];
var value = Symbol('foo');
var result = normalize(type, value);
expect(result).toBe(value);
done();
});

it('returns undefined if value does not match any type', function (done) {
var type = ['string', 'undefined'];
var value = 1;
Expand Down

0 comments on commit ffeab72

Please sign in to comment.