From 431bab21a490ee51d35395966a504501e8c685da Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Fri, 13 Oct 2023 21:22:53 -0700 Subject: [PATCH] [New] add special handling for the global object --- index.js | 7 +++++++ package.json | 1 + test/global.js | 17 +++++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 test/global.js diff --git a/index.js b/index.js index 8496225..7ab73c9 100644 --- a/index.js +++ b/index.js @@ -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; diff --git a/package.json b/package.json index 5a76496..ae441b7 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "for-each": "^0.3.3", "functions-have-names": "^1.2.3", "glob": "=10.3.7", + "globalthis": "^1.0.3", "has-tostringtag": "^1.0.0", "in-publish": "^2.0.1", "jackspeak": "=2.1.1", diff --git a/test/global.js b/test/global.js new file mode 100644 index 0000000..756aa06 --- /dev/null +++ b/test/global.js @@ -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(); +});