Skip to content

Commit

Permalink
Object.toString() is [object Object] not ''
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Feb 3, 2016
1 parent 995cf7a commit a57f3c8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
7 changes: 6 additions & 1 deletion test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2455,11 +2455,16 @@ describe('Validators', function () {
});

it('should convert non-string input', function () {
var empty = [undefined, null, [], NaN, Object.create(null)];
var empty = [undefined, null, [], NaN];
empty.forEach(function (item) {
assert.equal(validator.toString(item), '');
assert(validator.isNull(item));
});

var objects = [{}, Object.create(null)];
objects.forEach(function (item) {
assert.equal(validator.toString(item), '[object Object]');
});
});

});
8 changes: 4 additions & 4 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@
};

validator.toString = function (input) {
if (typeof input === 'object') {
if (input !== null && typeof input.toString === 'function') {
if (typeof input === 'object' && input !== null) {
if (typeof input.toString === 'function') {
input = input.toString();
} else {
input = '';
input = '[object Object]';
}
} else if (typeof input === 'undefined' || (isNaN(input) && !input.length)) {
} else if (input === null || typeof input === 'undefined' || (isNaN(input) && !input.length)) {
input = '';
}
return '' + input;
Expand Down
Loading

0 comments on commit a57f3c8

Please sign in to comment.