Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for string parameters and variables #56

Merged
merged 4 commits into from
Apr 23, 2024

Conversation

LuigiPulcini
Copy link
Contributor

@LuigiPulcini LuigiPulcini commented Apr 19, 2024

If there is any interest in adding support for string parameters and variables, this PR adjusts the version forked by @issidorov (https://github.com/issidorov/fparse/), updating it to the latest master branch (including the refactoring to Typescript).

This is also requested in #51.

An example of what this version can do is offered below:

const f1 = new Formula( 'if( compare( [quantity], 10, ">=" ), .9, 1 ) * [price]' );

f1.if = ( value, ifTrue, ifFalse ) => {
    return ! ! value ? ifTrue : ifFalse;
};

f1.compare = ( value, reference, comparison ) => {
    switch ( comparison ) {
        case '>=':
            return value >= reference ? 1 : 0;
        ...
        case '>':
        default:
            return value > reference ? 1 : 0;
    }
};

const res = f1.evaluate( { quantity: 10, price: 100 } );
console.log( res );

// res = 900

I hope this helps.

@bylexus bylexus mentioned this pull request Apr 19, 2024
@bylexus bylexus changed the base branch from master to develop April 20, 2024 12:18
Copy link
Owner

@bylexus bylexus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution. I started the review by checking out your branch, building it (using nodejs 20), and ran the tests (npm run test). There are 13 tests failing.

Would you have a look into it, or maybe I did oversee something?
Thanks

Comment on lines 3 to 9
beforeEach(function () {
if (typeof require !== 'undefined') {
Fparser = require('../../dist/fparser-dev');
} else {
Fparser = window.Formula;
}
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CommonJS syntax is no longer needed / supported since the switch to a TypeScript library. Please remove this require "hack". (see for example spec/specs/basicSpec.js).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems not to be fixed - when executing the tests with npm run test, the window object is not present - please remove the whole beforeEach() function, as it is no longer needed since the TypeScript build.

Copy link
Owner

@bylexus bylexus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am very sorry, but still, most of the tests fail when running npm run test. Maybe I am missing something?

Please correct either the failing code, or fix the tests (one of the two seems not correct).

Comment on lines 3 to 9
beforeEach(function () {
if (typeof require !== 'undefined') {
Fparser = require('../../dist/fparser-dev');
} else {
Fparser = window.Formula;
}
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems not to be fixed - when executing the tests with npm run test, the window object is not present - please remove the whole beforeEach() function, as it is no longer needed since the TypeScript build.

});
});

it('blocking math functions', function () {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test fails.

});
});

it('blocking math operator "^"', function () {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test fails.

expect(Fparser.calc('myFnFoo([myVar])', { myFnFoo: myFnFoo, myVar: 'bar' })).toEqual('foobar');
});

it('blocking math operator "*" of number and variable', function () {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test fails.

});

it('correct in parentheses', function () {
expect(Fparser.calc('(")")')).toEqual(')');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test fails.

expect(() => Fparser.calc('2x', { x: 'foo' })).toThrowError(expectedError);
});

it('blocking math operator "+"', function () {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test fails.

expect(Fparser.calc('" "')).toEqual(' ');
});

it('allow special chars', function () {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test fails.

});
});

it('blocking math operator "/"', function () {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test fails.

});
});

it('blocking math operator "*"', function () {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test fails.

expect(Fparser.calc('[myVar]', { myVar: 'foo' })).toEqual('foo');
});

it('support usage by custom function', function () {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test fails.

@LuigiPulcini
Copy link
Contributor Author

Hi, @bylexus.

First of all, thank you so much for promptly reviewing my pull request. I am sorry I completely missed the fact that the tests would all fail with the library refactoring in Typescript.

I have now adjusted a few more details in the library and fixed all the tests accordingly. When I run npm run test, everything runs smoothly, now.

bylexus added a commit that referenced this pull request Apr 23, 2024
Adding string support

Merge branch 'barn2plugins-master' into develop
@bylexus bylexus merged commit 1c88fea into bylexus:develop Apr 23, 2024
@bylexus
Copy link
Owner

bylexus commented Apr 23, 2024

Hi @LuigiPulcini,

Thanks for your contribution, this is a valuable addition. I did some adaptions to your original pull request, mostly type definitions and some documentation.

For the time being, I merged it back to the develop branch, I want to implement some additional changes before merging it to the main and creating a version, namely:

  • support for double- and single quote strings (now: only double quotes)
  • make parser state names via enum, instead of error-prone strings

As soon as I added those additions, I will publish a new version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants