1.1.0 • Published 3 years ago

@albertocruzluis/constant-folding v1.1.0

Weekly downloads
-
License
-
Repository
github
Last release
3 years ago

npm version CI for constant-folding

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

Tests

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)