Skip to content

Commit

Permalink
Explicitly check null and undefined values in exports.type
Browse files Browse the repository at this point in the history
Some older JS engines, such as PhantomJS, return the `window` object
instead of "[object Null]", as would be expected here.
  • Loading branch information
chromakode committed Jun 23, 2015
1 parent d082a92 commit 0d16e02
Show file tree
Hide file tree
Showing 2 changed files with 25 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 @@ -372,7 +372,11 @@ var emptyRepresentation = function emptyRepresentation(value, type) {
* @returns {string}
*/
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
20 changes: 20 additions & 0 deletions test/acceptance/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,26 @@ describe('lib/utils', function () {
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 0d16e02

Please sign in to comment.