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(std/node): add os Symbol.toPrimitive methods #4073

Merged
merged 1 commit into from
Feb 23, 2020

Conversation

bnoordhuis
Copy link
Contributor

Node's os module exports a number of methods that evaluate to themselves
when coerced to a primitive.

I.e., "" + os.arch and os.arch() evaluate to the same string, and
now Deno's shims do too.

Node's os module exports a number of methods that evaluate to themselves
when coerced to a primitive.

I.e., `"" + os.arch` and `os.arch()` evaluate to the same string, and
now Deno's shims do too.
@@ -89,6 +89,17 @@ interface UserInfo {
homedir: string;
}

arch[Symbol.toPrimitive] = (): string => arch();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not using arch[Symbol.toPrimitive] = arch; directly ?
Same applie for the rest of the symbols.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because that's how Node.js does it. I figured that if we're adding a compatibility shim, we might as well aim for 100% fidelity. :-)

There's an observable difference in that os.arch[Symbol.toPrimitive].name is the empty string in Node.js but it would be 'arch' with your suggestion, i.e., it'd print differently.

Copy link
Contributor

Choose a reason for hiding this comment

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

If there is a unit test for this use case on node side, it would be mandatory to do as node do.
Can you check ?

Copy link
Contributor Author

@bnoordhuis bnoordhuis Feb 22, 2020

Choose a reason for hiding this comment

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

I know the answer but it's the wrong question to ask, IMO. Node is so widely used that chances are > 50% that there's some code somewhere depending on this particular quirk. Even if Node's test suite didn't test for it, that doesn't mean we can just deviate.

Random example: people complained their code broke when the util.inspect() representation of streams changed in a really minor way. When the user base is big enough, any change is going to get noticed by someone.

Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

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

LGTM - thanks Ben!

@ry ry merged commit 45eb2f9 into denoland:master Feb 23, 2020
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 21, 2021
Node's os module exports a number of methods that evaluate to themselves
when coerced to a primitive.

I.e., `"" + os.arch` and `os.arch()` evaluate to the same string, and
now Deno's shims do too.
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 24, 2021
Node's os module exports a number of methods that evaluate to themselves
when coerced to a primitive.

I.e., `"" + os.arch` and `os.arch()` evaluate to the same string, and
now Deno's shims do too.
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 24, 2021
Node's os module exports a number of methods that evaluate to themselves
when coerced to a primitive.

I.e., `"" + os.arch` and `os.arch()` evaluate to the same string, and
now Deno's shims do too.
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 24, 2021
Node's os module exports a number of methods that evaluate to themselves
when coerced to a primitive.

I.e., `"" + os.arch` and `os.arch()` evaluate to the same string, and
now Deno's shims do too.
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
Node's os module exports a number of methods that evaluate to themselves
when coerced to a primitive.

I.e., `"" + os.arch` and `os.arch()` evaluate to the same string, and
now Deno's shims do too.
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
Node's os module exports a number of methods that evaluate to themselves
when coerced to a primitive.

I.e., `"" + os.arch` and `os.arch()` evaluate to the same string, and
now Deno's shims do too.
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
Node's os module exports a number of methods that evaluate to themselves
when coerced to a primitive.

I.e., `"" + os.arch` and `os.arch()` evaluate to the same string, and
now Deno's shims do too.
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Jan 31, 2021
Node's os module exports a number of methods that evaluate to themselves
when coerced to a primitive.

I.e., `"" + os.arch` and `os.arch()` evaluate to the same string, and
now Deno's shims do too.
caspervonb pushed a commit to caspervonb/deno_std that referenced this pull request Feb 1, 2021
Node's os module exports a number of methods that evaluate to themselves
when coerced to a primitive.

I.e., `"" + os.arch` and `os.arch()` evaluate to the same string, and
now Deno's shims do too.
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

3 participants