Skip to content

Commit 2c2f0c6

Browse files
committed
Fix global support in node.js 10
1 parent 8cd6b41 commit 2c2f0c6

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/object.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ import { arrayToString } from "./array";
77
* Transform an object into a string.
88
*/
99
export const objectToString: ToString = (value, space, next, key) => {
10-
if (typeof (Buffer as unknown) === "function" && Buffer.isBuffer(value)) {
10+
// Support buffer in all environments.
11+
if (typeof Buffer === "function" && Buffer.isBuffer(value)) {
1112
return `Buffer.from(${next(value.toString("base64"))}, 'base64')`;
1213
}
1314

15+
// Support `global` under test environments that don't print `[object global]`.
16+
if (typeof global === "object" && value === global) {
17+
return globalToString(value, space, next, key);
18+
}
19+
1420
// Use the internal object string to select stringify method.
1521
const toString = OBJECT_TYPES[Object.prototype.toString.call(value)];
1622
return toString ? toString(value, space, next, key) : undefined;
@@ -20,8 +26,6 @@ export const objectToString: ToString = (value, space, next, key) => {
2026
* Stringify an object of keys and values.
2127
*/
2228
const rawObjectToString: ToString = (obj, indent, next, key) => {
23-
if (obj === globalThis) return globalToString(obj, indent, next, key);
24-
2529
const eol = indent ? "\n" : "";
2630
const space = indent ? " " : "";
2731

0 commit comments

Comments
 (0)