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

@alu0101130507/constant-folding

v1.3.1

Published

Constant Folding javascript code

Downloads

7

Readme

npm version CI for constant-folding

Constant folding

This module provides functionalities to perform the transformation of the constant folding for a JavaScript input, producing an output which has the same code as the input but with some of the constants in methods, arrays or operations are folded.

Functions

constantFolding(code) ⇒ string

A function that receives a piece of code and returns the same code but with its constants folded.

Kind: global function Returns: string - The modified code with its constants folded

| Param | Type | Description | | --- | --- | --- | | code | string | The code to receive |

replaceByLiteral(n)

Replaces a node that represents the operation between two children literals with a single node that has the result of said operation

Kind: global function

| Param | Type | Description | | --- | --- | --- | | n | ASTnode | The AST father node. |

replaceConcat(n)

Replaces a node that represents the concat() method of an array with a single node that has the result of said method

Kind: global function

| Param | Type | Description | | --- | --- | --- | | n | ASTnode | The AST father node. |

replaceJoin(n)

Replaces a node that represents the join() method of an array with a single node that has the result of said method

Kind: global function

| Param | Type | Description | | --- | --- | --- | | n | ASTnode | The AST father node. |

replaceLength(n)

Replaces a node that represents the length member expression of an array with a single node that has the result of that length.

Kind: global function

| Param | Type | Description | | --- | --- | --- | | n | ASTnode | The AST father node. |

replaceBracketOperator(n)

Replaces a node that represents the bracket operator [] member expression of an array with a single node that has the result of that operator.

Kind: global function

| Param | Type | Description | | --- | --- | --- | | n | ASTnode | The AST father node. |

replaceShift(n)

Replaces a node that represents the shift method of an array with a single node that has the result of that method.

Kind: global function

| Param | Type | Description | | --- | --- | --- | | n | ASTnode | The AST father node. |

replaceSlice(n)

Replaces a node that represents the slice method of an array with a single node that has the result of that method.

Kind: global function

| Param | Type | Description | | --- | --- | --- | | n | ASTnode | The AST father node. |

replacePop(n)

Replaces a node that represents the pop method of an array with a single node that has the result of that method.

Kind: global function

| Param | Type | Description | | --- | --- | --- | | n | ASTnode | The AST father node. |

replaceReverse(n)

Replaces a node that represents the reverse method of an array with a single node that has the result of that method.

Kind: global function

| Param | Type | Description | | --- | --- | --- | | n | ASTnode | The AST father node. |

Installation

To install this module, simply run the following command:

npm i @ULL-ESIT-PL-2122/constant-folding

Usage as executable:

The module provides an executable that can run as:

Usage: cf [options] <filename>

Constant Folding javascript code

Arguments:
  filename                 file with the original code

Options:
  -V, --version            output the version number
  -o, --output <filename>  name of the file to write the result (default: "output.js")
  -h, --help               display help for command

Usage from code:

The module also provides one function, constantFolding(), which can be used to parse any piece of code and perform the required transformations. The usage of this function is described in the Functions section.

const constantFolding = require('@ULL-ESIT-PL-2122/constant-folding');
//call the function
let outputCode = constantFolding(inputCode);

Examples

Example 1: Binary expressions

// Example input with binary expressions:
let f = 3 + null;
let e = 4 | 3;
let d = 3 + "c";
let b = 9 +1;
let a = 2 + 3 * 5 + b;

// Will produce the following output:
let f = 3;
let e = 7;
let d = '3c';
let b = 10;
let a = 17 + b;

Example 2: Member expressions

// Example input with member expressions:
['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();

// Will produce the following output:
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
'a,b,c';
'a@b@c';
3;
2;
1;
[1, 2];
'c';
['c', 'b', 'a'];

Author

Markus Schüller Perdigón - alu0101130507

Tests

The tests can be found in test/test.js and run with npm run test. It contains tests for the examples found in the Examples section.