@stemcstudio/js-to-mathscript
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

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();
        }
    }
});

Readme

Keywords

none

Package Sidebar

Install

npm i @stemcstudio/js-to-mathscript

Weekly Downloads

1

Version

1.0.3

License

MIT

Unpacked Size

3.3 MB

Total Files

66

Last publish

Collaborators

  • geometryzen