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

Conversation

ExE-Boss
Copy link

@ExE-Boss ExE-Boss commented Feb 1, 2020

Node 10.12 and newer registers the util.inspect.custom symbol as nodejs.util.inspect.custom, making it possible to use Symbol.for('nodejs.util.inspect.custom') to get the equivalent util.inspect.custom symbol without depending on util.

@ExE-Boss ExE-Boss requested a review from ljharb February 1, 2020 10:40
@codecov
Copy link

codecov bot commented Feb 1, 2020

Codecov Report

Merging #30 into master will not change coverage.
The diff coverage is 75%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master    #30   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           2      2           
  Lines         206    210    +4     
  Branches       75     77    +2     
=====================================
+ Hits          206    210    +4
Impacted Files Coverage Δ
index.js 100% <75%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3b28eca...3a485db. Read the comment docs.

@@ -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;

@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants