mirror of
https://github.com/swc-project/swc.git
synced 2024-12-28 08:04:43 +03:00
61 lines
1.8 KiB
JavaScript
61 lines
1.8 KiB
JavaScript
|
import swc from "../../..";
|
||
|
|
||
|
it("should output same result", async () => {
|
||
|
const origin = `
|
||
|
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;
|
||
|
}
|
||
|
toFixed(1.2345, 2, Math.round, 1);
|
||
|
`
|
||
|
|
||
|
async function minify() {
|
||
|
const { code } = await swc.minify(origin, {
|
||
|
compress: true,
|
||
|
mangle: false
|
||
|
});
|
||
|
return code;
|
||
|
}
|
||
|
|
||
|
async function transform() {
|
||
|
const { code } = await swc.transform(origin, {
|
||
|
jsc: {
|
||
|
minify: {
|
||
|
compress: true,
|
||
|
mangle: false
|
||
|
},
|
||
|
},
|
||
|
isModule: false,
|
||
|
minify: true
|
||
|
});
|
||
|
return code;
|
||
|
}
|
||
|
|
||
|
const [minifyResult, transformResult] = await Promise.all([minify(), transform()]);
|
||
|
expect(minifyResult).toEqual(transformResult);
|
||
|
});
|