From 2c7bff3cd42f5dd57e43276a9d337a34095b9b79 Mon Sep 17 00:00:00 2001 From: Vignesh Shanmugam Date: Wed, 8 Mar 2017 18:25:26 +0100 Subject: [PATCH 1/3] optimize numeric literals properly [fixes #459] --- .../__tests__/numeric-literals-test.js | 7 ++++- .../src/index.js | 26 ++++++++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/packages/babel-plugin-minify-numeric-literals/__tests__/numeric-literals-test.js b/packages/babel-plugin-minify-numeric-literals/__tests__/numeric-literals-test.js index 5b3695cfa..f570d8e50 100644 --- a/packages/babel-plugin-minify-numeric-literals/__tests__/numeric-literals-test.js +++ b/packages/babel-plugin-minify-numeric-literals/__tests__/numeric-literals-test.js @@ -18,7 +18,7 @@ describe("numeric-literals", () => { expect(transform(source)).toBe(expected); source = "[1e3, -1e4, 1e-5, 1.5e12, 1.23456, .1];"; - expected = "[1e3, -1e4, 1e-5, 1.5e12, 1.23456, .1];"; + expected = "[1e3, -1e4, 1e-5, 15e11, 1.23456, .1];"; expect(transform(source)).toBe(expected); @@ -33,5 +33,10 @@ describe("numeric-literals", () => { expected = "[+1e-12, -1e-11];"; expect(transform(source)).toBe(expected); + + source = "[666200008e1, 666200008e-1];"; + expected = "[6662000080, 66620000.8];"; + + expect(transform(source)).toBe(expected); }); }); diff --git a/packages/babel-plugin-minify-numeric-literals/src/index.js b/packages/babel-plugin-minify-numeric-literals/src/index.js index 9e6572e13..4c8ad7b4a 100644 --- a/packages/babel-plugin-minify-numeric-literals/src/index.js +++ b/packages/babel-plugin-minify-numeric-literals/src/index.js @@ -1,15 +1,35 @@ "use strict"; module.exports = function({ types: t }) { + + const toExp = (val) => { + const floatVal = parseFloat(val).toString(); + const expVal = val.toExponential().toString(); + + if (floatVal.length < expVal.length) { + val = floatVal; + } else { + val = expVal; + } + + // 1.1e4 -> 11e3 + if (val.indexOf(".") >= 0 && val.indexOf("e") >= 0) { + val = val.replace(/\+/g, ""); + const lastChar = val.substr(val.lastIndexOf("e") + 1); + const dotIndex = val.lastIndexOf(".") + 1; + const subLength = val.substr(dotIndex, val.lastIndexOf("e") - dotIndex).length; + val = val.replace(".", "").replace(lastChar, Number(lastChar) - subLength); + } + return val.replace(/\+/g, "").replace(/e0/, ""); + }; + return { name: "minify-numeric-literals", visitor: { NumericLiteral(path) { if (!path.node.extra) return; - const exponential = path.node.value.toExponential() - .replace(/\+/g, "") - .replace(/e0/, ""); + const exponential = toExp(path.node.value) if (path.node.extra.raw.length > exponential.length) { const literal = t.numericLiteral(path.node.value); From 039e9be586a6259597a86873d90acc3d4435612f Mon Sep 17 00:00:00 2001 From: Vignesh Shanmugam Date: Wed, 8 Mar 2017 19:26:27 +0100 Subject: [PATCH 2/3] fix lint --- packages/babel-plugin-minify-numeric-literals/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-plugin-minify-numeric-literals/src/index.js b/packages/babel-plugin-minify-numeric-literals/src/index.js index 4c8ad7b4a..55a78085a 100644 --- a/packages/babel-plugin-minify-numeric-literals/src/index.js +++ b/packages/babel-plugin-minify-numeric-literals/src/index.js @@ -29,7 +29,7 @@ module.exports = function({ types: t }) { NumericLiteral(path) { if (!path.node.extra) return; - const exponential = toExp(path.node.value) + const exponential = toExp(path.node.value); if (path.node.extra.raw.length > exponential.length) { const literal = t.numericLiteral(path.node.value); From 76f455c92284f849fdaec5a7df053abb7b6a81e1 Mon Sep 17 00:00:00 2001 From: Vignesh Shanmugam Date: Sun, 12 Mar 2017 22:06:56 +0100 Subject: [PATCH 3/3] few opts and test cases --- .../__tests__/numeric-literals-test.js | 5 ++++ .../src/index.js | 30 +++++++------------ 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/packages/babel-plugin-minify-numeric-literals/__tests__/numeric-literals-test.js b/packages/babel-plugin-minify-numeric-literals/__tests__/numeric-literals-test.js index f570d8e50..67714e520 100644 --- a/packages/babel-plugin-minify-numeric-literals/__tests__/numeric-literals-test.js +++ b/packages/babel-plugin-minify-numeric-literals/__tests__/numeric-literals-test.js @@ -38,5 +38,10 @@ describe("numeric-literals", () => { expected = "[6662000080, 66620000.8];"; expect(transform(source)).toBe(expected); + + source = "[11000, 66.00000, 6600000, 1.5e-3];"; + expected = "[11e3, 66, 66e5, 15e-4];"; + + expect(transform(source)).toBe(expected); }); }); diff --git a/packages/babel-plugin-minify-numeric-literals/src/index.js b/packages/babel-plugin-minify-numeric-literals/src/index.js index 55a78085a..728213849 100644 --- a/packages/babel-plugin-minify-numeric-literals/src/index.js +++ b/packages/babel-plugin-minify-numeric-literals/src/index.js @@ -1,26 +1,23 @@ "use strict"; -module.exports = function({ types: t }) { +module.exports = function() { - const toExp = (val) => { + const transform = (val) => { const floatVal = parseFloat(val).toString(); - const expVal = val.toExponential().toString(); - + const expVal = val.toExponential().replace(/\+/g, ""); if (floatVal.length < expVal.length) { - val = floatVal; - } else { - val = expVal; + return floatVal; } - // 1.1e4 -> 11e3 + val = expVal; if (val.indexOf(".") >= 0 && val.indexOf("e") >= 0) { - val = val.replace(/\+/g, ""); const lastChar = val.substr(val.lastIndexOf("e") + 1); const dotIndex = val.lastIndexOf(".") + 1; const subLength = val.substr(dotIndex, val.lastIndexOf("e") - dotIndex).length; - val = val.replace(".", "").replace(lastChar, Number(lastChar) - subLength); + val = val.substr(0, val.lastIndexOf("e") + 1) + (lastChar - subLength); + val = val.replace(".", "").replace(/e0/, ""); } - return val.replace(/\+/g, "").replace(/e0/, ""); + return val; }; return { @@ -29,15 +26,10 @@ module.exports = function({ types: t }) { NumericLiteral(path) { if (!path.node.extra) return; - const exponential = toExp(path.node.value); + const newVal = transform(path.node.value); - if (path.node.extra.raw.length > exponential.length) { - const literal = t.numericLiteral(path.node.value); - literal.extra = { - raw: exponential, - rawValue: path.node.value - }; - path.replaceWith(literal); + if (path.node.extra.raw.length > newVal.length) { + path.node.extra.raw = newVal; } } },