Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add fallback to Symbol.for(…) #30

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ var match = String.prototype.match;
var bigIntValueOf = typeof BigInt === 'function' ? BigInt.prototype.valueOf : null;

var inspectCustom = require('./util.inspect').custom;
/* eslint-disable no-restricted-properties */
if (!inspectCustom && typeof Symbol === 'function' && typeof Symbol['for'] === 'function') {
inspectCustom = Symbol['for']('nodejs.util.inspect.custom');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will mean that in the browser, however, a symbol will be registered and usable. it seems to me that util.inspect's custom symbol should only ever work in node.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this logic only helps in node >= 10.12; the dependency is still required for node 6.6-10.11, so i don’t see the value in this added complexity.

}
/* eslint-enable no-restricted-properties */

var inspectSymbol = inspectCustom && isSymbol(inspectCustom) ? inspectCustom : null;

module.exports = function inspect_(obj, options, depth, seen) {
Expand Down Expand Up @@ -143,6 +149,10 @@ module.exports = function inspect_(obj, options, depth, seen) {
return String(obj);
};

if (inspectSymbol) {
module.exports.custom = inspectSymbol;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd prefer not to re-export this.

Suggested change
module.exports.custom = inspectSymbol;

}

function wrapQuotes(s, defaultStyle, opts) {
var quoteChar = (opts.quoteStyle || defaultStyle) === 'double' ? '"' : "'";
return quoteChar + s + quoteChar;
Expand Down