Skip to content

Commit

Permalink
test: Add an execution test for paren_remover (vercel/turborepo#8371)
Browse files Browse the repository at this point in the history
### Description

I copied the test from swc-project/swc#8442.
This input file fails if the `paren_remover` is not applied.

### Testing Instructions

Closes PACK-3110
  • Loading branch information
kdy1 authored Jun 7, 2024
1 parent 22ef996 commit 7ebf8b9
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function toFixed(value, maxDecimals, roundingFunction, optionals) {
var splitValue = value.toString().split("."),
minDecimals = maxDecimals - (optionals || 0),
optionalsRegExp,
power,
output;
var boundedPrecisions;
// var unused = 'xxxx';
// Use the smallest precision value possible to avoid errors from floating point representation
if (splitValue.length === 2) {
boundedPrecisions = Math.min(
Math.max(splitValue[1].length, minDecimals),
maxDecimals
);
} else {
boundedPrecisions = minDecimals;
}
power = Math.pow(10, boundedPrecisions);
// Multiply up by precision, round accurately, then divide and use native toFixed():
output = (roundingFunction(value + "e+" + boundedPrecisions) / power).toFixed(
boundedPrecisions
);
if (optionals > maxDecimals - boundedPrecisions) {
optionalsRegExp = new RegExp(
"\\.?0{1," + (optionals - (maxDecimals - boundedPrecisions)) + "}$"
);
output = output.replace(optionalsRegExp, "");
}
return output;
}

it("should work", () => {
expect(toFixed(1.2345, 2, Math.round, 1)).toBe("1.23");
});

0 comments on commit 7ebf8b9

Please sign in to comment.