@albertocruzluis/constant-folding
v1.1.0
Published
Constant Folding javascript code
Downloads
17
Maintainers
Readme
constant-folding
A small library that evaluating constant expressions at compile time.
Installation
npm i @albertocruzluis/constant-folding
Usage as executable:
cf input.js output.js
Usage: cf [options] <filename>
Constant Folding javascript code
Arguments:
filename file with the original code
Options:
-V, --version output the version number
-h, --help display help for command
Usage from code:
// Import library
const constantFolding = require('@albertocruzluis/constant-folding');
// Examples
const input = a=2*3;
// Call function
const result = constantFolding(input)
console.log(result) // result a = 6;
The documentation of the function.
Examples
- Simple
const example1 = {
input: `a=2*3;`,
output: `a = 6;`
}
const example2 = {
input: `b=1+4*5+f;`,
output: `b = 21 + f;`
}
const example3 = {
input: `c= 2+"as";`,
output: `c = '2as';`
}
const example4 = {
input: `d = 3+null`,
output: `d = 3;`
}
- Array Expression
const example1 = {
input: `["a", "b", "c"].join();`,
output: `'a,b,c';`
}
const example2 = {
input: `["a", "b", "c"].join('@');`,
output: `'a@b@c';`
}
const example3 = {
input: `["a", "b", "c"].pop();`,
output: `'c';`
}
const example4 = {
input: `[1, 2, 3].shift();`,
output: `1;`
}
Author
Alberto Cruz Luis - [email protected]
Tests
npm test
> @albertocruzluis/[email protected] test
> mocha
constantFolding simple tests
✔ works correctly with operation a=2*3
✔ works correctly with operation a=1+4*5+b
✔ works correctly with operation d= 2+"as"
✔ works correctly with operation d = 3+null
constantFolding arrayExpression tests
✔ works correctly with operation ["a", "b", "c"].join()
✔ works correctly with operation ["a", "b", "c"].join("@");
✔ works correctly with operation ["a", "b", "c"].pop();
✔ works correctly with operation [1, 2, 3].shift();
8 passing (29ms)