@alu0100953275/constant-folding
Version:
Constant Folding javascript code
104 lines (80 loc) • 2.18 kB
Markdown
[](https://classroom.github.com/a/fzrjycLm)
[](https://classroom.github.com/open-in-codespaces?assignment_repo_id=10591016)
## constant-folding
## Installation
npm install @alu0100953275/constant-folding@lastest
## Usage as executable:
- bash:
```bash
$ ./bin/constant-folding-cli.js input.js output.js
File read succesfully
Output written succesfully in output.js
```
- input.js:
```javascript
var f = 3+null;
var e = 4 | 3;
var d = 3+"c";
var b = 9 +1;
var a = 2+3*5+b;
```
- output.js:
```javascript
var f = 3;
var e = 7;
var d = '3c';
var b = 10;
var a = 17 + b;
```
## Usage from code:
```javascript
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](https://ull-esit-pl-2021.github.io/constant-folding-module-alu0100953275/).
## Author
alu0100953275@ull.edu.es
## Tests
- tests.js:
```javascript
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:
```bash
$ 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)
```