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

@alu0101244488/constant-folding

v1.0.2

Published

Constant Folding javascript code

Downloads

11

Readme

npm version CI for constant-folding

constant-folding

Installation

For installation it is only required to use the next command:

npm install @alu0101244488/constant-folding

You can see the npm package on the following link:

https://www.npmjs.com/package/@alu0101244488/constant-folding

Then you can install the package dependencies with:

npm i

Usage as executable:

To execute you can use the following command where [inputFile] is the file with the javasctipt code to compute and [outputFile] is the file to print the corresponding code with the transformations applied. Note is that the -o [outputFile] or --output [outputFile] is optional and the resulting code will be displayed on terminal in any case (a test file is given to try).

cf [inputFile] -o [outputFile]

Other options suported are -h or --help, and -V or --version which shows all the corresponding information or the version of the package correspondingly.

Example of the execution. You can see more details about the result of the execution in the Examples section.

result of the execution

Usage from code:

const constantFolding = require('constant-folding');
//call the function

Functions implemented to to apply the constant folding:

  • constantFolding
/**
 * Function to traverse the ast and modify the corresponding nodes to apply
 * the transformation in the tree (constant folding)
 * @param {string} code String with the hole code
 * @return {string} String with the resulting javascript code
 * 									after appliying the corresponding ast transformations
 */
function constantFolding(code) {
  ...
}
  • replaceByLiteral
/**
 * Function to modify the ast for binary expressions for two literals
 * @param {Node} node Node with two literals and the operator (binary expresion)
 */
function replaceByLiteral(node) {
  ...
}
  • replaceByMemberExpression
/**
 * Function to replace any member function depending if its an array or a literal
 * (a string) with its corresponding result.
 * @param {Node} node Node to apply the constant folding
 */
function replaceByMemberExpression(node) {
  ...
}
  • replaceByCallExpression
/**
 * Function to alter an ast call expression. This is applyed when a methods
 * call is encountered. This is for exmple for a object method or literals like
 * strings or arrays.
 * @param {Node} node Node to apply the transformation
 */
function replaceByCallExpression(node) {
  ...
}
  • generateArray
/**
 * Receives a node and generates an array from the elements of that node.
 * Function that given a node (which hods the information of an array) creates 
 * the corresponding array and returns it
 * @param {Node} node Node with the information of the array
 * @return {Array}  Resulting array
 */
function generateArray(node) {
  ...
}
  • generateOutput
/**
 * Function to formatt the resulting code with spaces and new lines
 * @param {string} code Resulting code
 * @return {string} Formatted code
 */
function generateOutput(code) {
  ...
}

Examples

Here is an example of the execution of a very simple code where we want to transform the sum of two literals (numbers).

Input file:

3 + 1;

Output:

4 + 1;

The program can also work with other types like arrays and strings and with other operations as for methods and properties of those types. An example can be seen in the next example:

[1, 2, 3].length
["a", "b", "c"].join();
"abc"[0];

output

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

Author

Aram Pérez Dios alu0101244488

Tests

The tests are splited in three different sections.

  • The first one is related to binary operators with. There are tests with two or more operands literals and non literals.
  • Then tests about methods and properties. This si splited further into two categories, arrays and strings.
    • Tests for properties and methods of literal arrays.
    • Tests for properties and methods of literal strings.
  • The last test is reseved to try different blocks of code and try multiple sentences.