Skip to content

Commit

Permalink
Merge pull request #1761 from chromakode/explicit-null-and-defined
Browse files Browse the repository at this point in the history
Explicitly check `null` and `undefined` values in exports.type
  • Loading branch information
Joshua Appelman committed Jul 4, 2015
2 parents c27110b + 0d16e02 commit fc3fee6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,11 @@ function emptyRepresentation(value, type) {
* type(global) // 'global'
*/
exports.type = function type(value) {
if (typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) {
if (value === undefined) {
return 'undefined';
} else if (value === null) {
return 'null';
} else if (typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) {
return 'buffer';
}
return Object.prototype.toString.call(value)
Expand Down
21 changes: 21 additions & 0 deletions test/acceptance/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,12 +341,33 @@ describe('lib/utils', function () {
type(1).should.equal('number');
type(Infinity).should.equal('number');
type(null).should.equal('null');
type(undefined).should.equal('undefined');
type(new Date()).should.equal('date');
type(/foo/).should.equal('regexp');
type('type').should.equal('string');
type(global).should.equal('global');
type(true).should.equal('boolean');
});

describe('when toString on null or undefined stringifies window', function () {
var toString = Object.prototype.toString;

beforeEach(function () {
// some JS engines such as PhantomJS 1.x exhibit this behavior
Object.prototype.toString = function () {
return '[object DOMWindow]';
};
});

it('should recognize null and undefined', function () {
type(null).should.equal('null');
type(undefined).should.equal('undefined');
});

afterEach(function () {
Object.prototype.toString = toString;
});
});
});

describe('lookupFiles', function () {
Expand Down

0 comments on commit fc3fee6

Please sign in to comment.