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

fix(compress): toFixed, toPrecision, toExponential and toString of Number #6960

Merged
merged 16 commits into from
Feb 19, 2023

Conversation

magic-akari
Copy link
Member

Description:

The code snippet from StackOverflow is for handling toPrecision instead of toFixed

BREAKING CHANGE:

Related issue (if exists):

@kdy1 kdy1 added this to the Planned milestone Feb 18, 2023
@kdy1 kdy1 self-assigned this Feb 18, 2023
@magic-akari
Copy link
Member Author

magic-akari commented Feb 18, 2023

The num_to_precision cannot handle JS toPrecision. It will return unexpected results.

assert.sameValue((10).toPrecision(1), "1e+1");
assert.sameValue((11).toPrecision(1), "1e+1");
assert.sameValue((17).toPrecision(1), "2e+1");
assert.sameValue((19).toPrecision(1), "2e+1");
assert.sameValue((20).toPrecision(1), "2e+1");

compress result:

assert.sameValue("10", "1e+1");
assert.sameValue("11", "1e+1");
assert.sameValue("17", "2e+1");
assert.sameValue("19", "2e+1");
assert.sameValue("20", "2e+1");

@kdy1
Copy link
Member

kdy1 commented Feb 18, 2023

Do you mean it's not possible at all?

@magic-akari
Copy link
Member Author

Do you mean it's not possible at all?

No, we can calculate the result step by step according to the ECMA262 specification.

@magic-akari
Copy link
Member Author

There are two test case files from test262.
I don't know if there is a license issue. Is it OK to include them?

@kdy1
Copy link
Member

kdy1 commented Feb 19, 2023

I think it's fine

@magic-akari magic-akari changed the title fix(compress): num.toFixed() and num.toPrecision() fix(compress): toFixed, toPrecision, toExponential and toString of Number Feb 19, 2023
@magic-akari magic-akari marked this pull request as ready for review February 19, 2023 10:05
Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

Thank you!


swc-bump:

  • swc_ecma_minifier

Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

Thank you!


swc-bump:

  • swc_ecma_transforms_base

@kdy1 kdy1 enabled auto-merge (squash) February 19, 2023 14:17
@kdy1 kdy1 merged commit 9382bda into swc-project:main Feb 19, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.36 Feb 21, 2023
@magic-akari magic-akari deleted the fix/issue-6957 branch March 10, 2023 02:51
@swc-project swc-project locked as resolved and limited conversation to collaborators Apr 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

'compress' option breaks number literals ending with '0' when .toFixed() is called
2 participants