@alu0101051420/constant-folding

1.3.0 • Public • Published

npm version CI for constant-folding

constant-folding

Small utility, applies constant-folding in source (javascript) code, using AST modification.

Installation

npm install @alu0101051420/constant-folding

Usage from code:

const constantFolding = require('constant-folding');
const example_code = "a = 3+2;"

console.log("Original Code: \n", example_code);
console.log("Code with constant folding: \n", constantFolding(example_code));

The documentation of the function.

Usage as executable:

An input file must be specified. You also may or may not specify an output file. If not given, it will default to "output.js".

npx cf input_file --o output_file

Examples

constantFolding(`['a', 'b', 'c'].concat(['d', 'e'], 'f', 'g', ['h']); `); // ['a','b','c','d','e','f','g','h']
constantFolding(`["a", "b", "c"].join(); `);    // a,b,c
constantFolding(`["a", "b", "c"].join('&'); `); // a&b&c
constantFolding(`[1, 2, 3].length; `);          // 3
constantFolding(`[1, 2, 3][2 - 1]; `);          // 2
constantFolding(`[1, 2, 3].shift(); `);         // 1
constantFolding(`[1, 2, 3].slice(0, 1 + 1); `); // [1, 2]
constantFolding(`['a', 'b', 'c'].pop(); `);     // c
constantFolding(`['a', 'b', 'c'].reverse(); `); // ['c','b','a']

Author

alu0101051420 (Nicolás Cabrera) - GitHub Profile

Tests

npm run test

Version History

1.0.0 Initial Release

1.1.0 Added npx executable

1.1.1 Minor documentation fixes

1.2.0 Added constant folding for array functions

Package Sidebar

Install

npm i @alu0101051420/constant-folding

Weekly Downloads

6

Version

1.3.0

License

Unlicense

Unpacked Size

1.12 MB

Total Files

55

Last publish

Collaborators

  • alu0101051420