constant-folding
Installation
npm install @alu0100953275/constant-folding@lastest
Usage as executable:
- bash:
$ ./bin/constant-folding-cli.js input.js output.js
File read succesfully
Output written succesfully in output.js
- input.js:
var f = 3+null;
var e = 4 | 3;
var d = 3+"c";
var b = 9 +1;
var a = 2+3*5+b;
- output.js:
var f = 3;
var e = 7;
var d = '3c';
var b = 10;
var a = 17 + b;
Usage from code:
const constantFolding = require('constant-folding');
const input = `
var f = 3+null;
var e = 4 | 3;
var d = 3+"c";
var b = 9 +1;
var a = 2+3*5+b;
`;
console.log(constantFolding(input));
/*
var f = 3;
var e = 7;
var d = '3c';
var b = 10;
var a = 17 + b;
*/
The documentation of the function.
Author
Tests
- tests.js:
const example1 = `a=2*3;`;
const example2 = `3+null;`;
const example3 = `4 | 3;`;
const example4 = `3+"c";`;
const example5 = `9 +1;`;
const example6 = `2+3*5+b;`;
const expectedOutput1 = `a = 6;`;
const expectedOutput2 = `3;`;
const expectedOutput3 = `7;`;
const expectedOutput4 = `'3c';`;
const expectedOutput5 = `10;`;
const expectedOutput6 = `17 + b;`;
- run test:
$ npm run test
> @alu0100953275/constant-folding@1.1.0 test
> mocha
constantFolding tests
✔ works correctly with normal functions 1
✔ works correctly with normal functions 2
✔ works correctly with normal functions 3
✔ works correctly with normal functions 4
✔ works correctly with normal functions 5
✔ works correctly with normal functions 6
6 passing (34ms)