npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@adrianepi/constant-folding

v1.4.1

Published

Constant Folding javascript code

Downloads

26

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/[email protected] 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)