Skip to content

Commit

Permalink
[New] add special handling for the global object
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Oct 14, 2023
1 parent b453f6c commit 7ada44f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,13 @@ module.exports = function inspect_(obj, options, depth, seen) {
if (isString(obj)) {
return markBoxed(inspect(String(obj)));
}
if (obj === global) {
/* eslint-env browser */
if (typeof window !== 'undefined') {
return '{ [object Window] }';
}
return '{ [object global] }';
}
if (!isDate(obj) && !isRegExp(obj)) {
var ys = arrObjKeys(obj, inspect);
var isPlainObject = gPO ? gPO(obj) === Object.prototype : obj instanceof Object || obj.constructor === Object;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"eslint": "=8.8.0",
"for-each": "^0.3.3",
"functions-have-names": "^1.2.3",
"globalthis": "^1.0.3",
"has-tostringtag": "^1.0.0",
"in-publish": "^2.0.1",
"jackspeak": "=2.1.1",
Expand Down
17 changes: 17 additions & 0 deletions test/global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

var inspect = require('../');

var test = require('tape');
var globalThis = require('globalthis')();

test('global object', function (t) {
/* eslint-env browser */
var expected = typeof window === 'undefined' ? 'global' : 'Window';
t.equal(
inspect([globalThis]),
'[ { [object ' + expected + '] } ]'
);

t.end();
});

0 comments on commit 7ada44f

Please sign in to comment.