1.0.3 • Published 1 year ago

@stemcstudio/js-to-mathscript v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

js-to-mathscript

A transpiler and validator from JavaScript syntax to some similar syntax.

Example of replacing the name of a Binary operator:

    it('Exponentiation', function () {
      const src = 'x**2';
      const { code } = jsToMathScript(src,
        {
          namespace: '',
          binOp: {
            '**': { kind: 'rename', value: '^' }
          },
          unaryOp: {}
        },
        {
          format: {
            semicolons: false
          }
        }
      );
      assert.strictEqual(code, 'x ^ 2');
    });

Example of converting a Binary operator to a function call:

it('Bitwise XOR (^) should be turned into a function.', function () {
    const src = 'x ^ y';
    const { code } = jsToMathScript(src, {
            namespace: 'MathScript',
            binOp: {
                '**': { kind: 'rename', value: '^' },
                '^': { kind: 'function', value: 'wedge' }
            },
            unaryOp: {}
        },
        {
        format: {
            semicolons: false
        }
    });
    assert.strictEqual(code, 'MathScript.wedge(x, y)');
});

Example of rejecting a Unary operator:

it("error", function () {
    try {
        const src = "const a = +b";
        jsToMathScript(src, {
            unaryOp: {
                '+': { kind: 'error', value: "Unexpected unary plus (+)" }
            }
        });
        assert.fail();
    } catch (e) {
        if (e instanceof Error) {
            assert.strictEqual(e.name, "Error");
            assert.strictEqual(e.message, "Unexpected unary plus (+)");
        } else {
            assert.fail();
        }
    }
});
1.0.3

1 year ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago