1.1.4 • Published 2 years ago

@0ria/constant-folding v1.1.4

Weekly downloads
-
License
Unlicense
Repository
github
Last release
2 years ago

npm version CI for constant-folding

constant-folding

Author

0ria - alu0101027340

Installation

npm install @0ria/constant-folding

Usage as executable:

Default output:

npx cf inputFile.js

Desired output:

npx xf inputFile.js -o outputFile.js

Usage from code:

const constantFolding = require('constant-folding');
const code = "[1, 2, 3].length"

console.log('Code after constant folding' + constantFolding(code));

The documentation of the function.

Examples

inputs:

['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();

outputs:

a, b, c, d, e, f, g, h;
a, b, c;
a & b & c;
3;
2;
1;
1, 2;
c;
c, b, a;

Tests

To run the tests create a directory test with a script containing tests and run npm test

const constantFolding =  require('@0ria/constant-folding');
require("chai").should();

const checks = [
  { input: "['a', 'b', 'c'].concat(['d', 'e'], 'f', 'g', ['h']);", result: "a, b, c, d, e, f, g, h;"},
  { input: '["a", "b", "c"].join();', result: "a, b, c;"},
  { input: '["a", "b", "c"].join("&");', result: "a & b & c;"},
  { input: '[1, 2, 3].length;', result: "3;"},
  { input: '[1, 2, 3][2 - 1]', result: "2;"},
  { input: '[1, 2, 3].shift();', result: "1;"},
  { input: '[1, 2, 3].slice(0, 1 + 1);', result: "1, 2;"},
  { input: '["a", "b", "c"].pop();', result: "c;"},
  { input: '["a", "b", "c"].reverse();', result: "c, b, a;"},
  { input: '[7, 2*3, 9/3].reverse()', result: "3, 6, 7;"},
  { input: 'a = 2*3*4', result: 'a = 24;'}
];

describe("Testing constant folding operations", () => {
  for (let c of checks) {
    it(`Test ${c.input} = ${c.result}`, () => {
      constantFolding(c.input).should.equal(c.result);
    })
  }
})

Test output:

Testing constant folding operations
    ✔ Test ['a', 'b', 'c'].concat(['d', 'e'], 'f', 'g', ['h']); = a, b, c, d, e, f, g, h;
    ✔ Test ["a", "b", "c"].join(); = a, b, c;
    ✔ Test ["a", "b", "c"].join("&"); = a & b & c;
    ✔ Test [1, 2, 3].length; = 3;
    ✔ Test [1, 2, 3][2 - 1] = 2;
    ✔ Test [1, 2, 3].shift(); = 1;
    ✔ Test [1, 2, 3].slice(0, 1 + 1); = 1, 2;
    ✔ Test ["a", "b", "c"].pop(); = c;
    ✔ Test ["a", "b", "c"].reverse(); = c, b, a;
    ✔ Test [7, 2*3, 9/3].reverse() = 3, 6, 7;
    ✔ Test a = 2*3*4 = a = 24;

  11 passing (45ms)
1.1.1

2 years ago

1.1.0

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago