UNPKG

@alu0101051420/constant-folding

Version:
67 lines (44 loc) 2.08 kB
[![npm version](https://badge.fury.io/js/@alu0101051420%2Fconstant-folding.svg)](https://badge.fury.io/js/@alu0101051420%2Fconstant-folding) [![CI for constant-folding](https://github.com/ULL-ESIT-PL-2122/constant-folding-module-nicolas-cabrera-dominguez-alu0101051420/actions/workflows/nodejs.yml/badge.svg)](https://github.com/ULL-ESIT-PL-2122/constant-folding-module-nicolas-cabrera-dominguez-alu0101051420/actions/workflows/nodejs.yml) ## constant-folding Small utility, applies constant-folding in source (javascript) code, using AST modification. ## Installation ``` npm install @alu0101051420/constant-folding ``` ## Usage from code: ```javascript 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](https://ull-esit-pl-2122.github.io/constant-folding-module-nicolas-cabrera-dominguez-alu0101051420/). ## 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 ```js 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](https://github.com/jncabdom) ## 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