[![npm version](https://badge.fury.io/js/@albertocruzluis%2Fconstant-folding.svg)](https://badge.fury.io/js/@albertocruzluis%2Fconstant-folding)
[![CI for constant-folding](https://github.com/ULL-ESIT-PL-2021/constant-folding-module-alu0101217734/actions/workflows/nodejs.yml/badge.svg)](https://github.com/ULL-ESIT-PL-2021/constant-folding-module-alu0101217734/actions/workflows/nodejs.yml)

## 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
```

```bash
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:

```javascript
// 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](https://ull-esit-pl-2021.github.io/constant-folding-module-aluXXX/).

## Examples
- Simple

```js
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
```js
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 - alu0101217734@ull.edu.es

## Tests

```bash
npm test
```

```
> @albertocruzluis/constant-folding@1.0.2 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)
```



