JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 13
  • Score
    100M100P100Q46886F
  • License Unlicense

Constant Folding javascript code

Package Exports

  • @adrianepi/constant-folding
  • @adrianepi/constant-folding/src/constant-folding.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@adrianepi/constant-folding) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

npm version CI for constant-folding

Constant-Folding

A small library providing utility methods to constant-folding. This allow to use constant folding with array, strings and number methods. For using this, the JS code is parsed into an AST and then modified for allowing the execution of the methods.

Installation

npm install @adrianepi/constant-folding --save

Usage as executable:

Usage help:

Constant Folding javascript code

Arguments:
  filename                 file with the original code

Options:
  -V, --version            output the version number
  -o, --output <filename>  file where the output is going to be stored (default: "output.js")
  -p, --pattern <pattern>  other parameters (--js | -j) for only store the js output and (--ast | -a) for only storing the ast output.
  -h, --help               display help for command

Examples of execution:

cf inputFile -o outputFile -p pattern
cf examples/example1.js -o output.js -p --js

Usage from code:

const constantFolding = require('constant-folding');

// Input must be js code.
const input = `
var f = 3+null;
var e = 4 | 3;
var d = 3+"c";
var b = 9 +1;
var a = 2+3*5+b;
[a, b, c].concat([d, e], f, g, [h]);
["a", "b", "c"].join();
["a", "b", "c"].join('@');
[1, 2, 3].length;
[1, 2, 3][2-1];
[1, 2, 3].shift();
[1, 2, 3].slice(0, 1+1);
[a, b, c].pop();
[a, b, c].reverse();
"abc"[0];
"abc".charAt();
"abc".charAt(1);
"abc".length;
"a,b,c".split(",");
(100 + 23).toString();
`;

const pattern = "--js"; 
// Patterns can be one of the following:
// (-j | --js) for js output.
// (-a | --ast) for ast output.
// If no pattern received, it will return both of them, AST code + JS code.
console.log(constantFolding(input, pattern));

The documentation of the function.

Examples

Simple input examples:

var f = 3+null;
var e = 4 | 3;
var d = 3+"c";
var b = 9 +1;
var a = 2+3*5+b;

Output of simple examples:

var f = 3;
var e = 7;
var d = 3c;
var b = 10;
var a = 17 + b;

Array input examples:

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

Output of array examples:

[ a, b, c, d, e, f, g, h];
"a,b,c";
"a@b@c";
3;
2;
1;
[ 1, 2];
c;
[ c, b, a];

String input examples:

"abc"[0];
"abc".charAt();
"abc".charAt(1);
"abc".length;
"a,b,c".split(",");

Output of string examples:

"a";
"a";
"b";
3;
"a", "b", "c";

Author

Adrián Epifanio Rodríguez Hernández (alu0101158280)

Tests

For executing the test there are some examples in the examples/ folder which can be used for testing the different ways of applying constant folding (array, strings, numbers).

For running tests the command is:

npm test

Output for the tests:

> @adrianepi/constant-folding@1.3.0 test
> mocha



  ConstantFolding simple tests
    ✔ Testing var f = 3+null;
    ✔ Testing var e = 4 | 3;
    ✔ Testing var d = 3+"c";
    ✔ Testing var b = 9 +1;
    ✔ Testing var a = 2+3*5+b;

  ConstantFolding array tests
    ✔ Testing [a, b, c].concat([d, e], f, g, [h]);
    ✔ Testing ["a", "b", "c"].join();
    ✔ Testing ["a", "b", "c"].join('@');
    ✔ Testing [1, 2, 3].length;
    ✔ Testing [1, 2, 3][2-1];
    ✔ Testing [1, 2, 3].shift();
    ✔ Testing [1, 2, 3].slice(0, 1+1);
    ✔ Testing [a, b, c].pop();
    ✔ Testing [a, b, c].reverse();

  ConstantFolding string tests
    ✔ Testing "abc"[0];
    ✔ Testing "abc".charAt();
    ✔ Testing "abc".charAt(1);
    ✔ Testing "abc".length;
    ✔ Testing "a,b,c".split(",");

  ConstantFolding AST tests
    ✔ Testing example1 AST
    ✔ Testing example2 AST
    ✔ Testing example3 AST


  22 passing (46ms)