Skip to content

Commit

Permalink
fixing merge request: correcting types and added some tests/docs
Browse files Browse the repository at this point in the history
  • Loading branch information
bylexus committed Apr 23, 2024
1 parent 1c88fea commit 5bb9493
Show file tree
Hide file tree
Showing 11 changed files with 361 additions and 1,285 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/develop-build.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Develp Build CI
name: Develop Build CI

on:
push:
Expand Down
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,25 @@ const res2 = fObj.evaluate();

### Using strings

You can also pass strings as values or variable values (not only numbers): It is then in your responsibility to
provide a function that can make sense of the string:

E.g. you can create a function that concats 2 values:

```javascript
const fObj = new Formula('concat([var1], "Bar")');
let result = fObj.evaluate({ var1: 'Foo', concat: (s1, s2) => s1 + s2 });
```

Here, the result of the evaluation is again a string.

Of course you can use strings to make decisions: Here, we provide a function `longer` that
returns the length of the longer of two strings, and calculates the remaining length:

```javascript
const fObj = new Formula('compare( [p1], [p2], "<" )', { supportStrings: true });
let result = fObj.evaluate({ x: 1.5 });
const fObj = new Formula('20 - longer([var1], "Bar")');
let result = fObj.evaluate({ var1: 'FooBar', longer: (s1, s2) => s1.length > s2.length ? s1.length : s2.length });
// --> 14
```

### Re-use a Formula object
Expand Down Expand Up @@ -307,6 +323,12 @@ edge cases.
- Make sure you include dist/fparser.js if you are using it as a browser library.
- Drop support for Bower, as there are more modern approaches (npm) for package dependency nowadays

## Contributors

Thanks to all the additional contributors:

- [LuigiPulcini](https://github.com/LuigiPulcini) for the Strings support

## License

Licensed under the MIT license, see LICENSE file.
4 changes: 2 additions & 2 deletions demopage/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions dist/fparser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type FormulaOptions = {
memoization?: boolean;
};
type ValueObject = {
[key: string]: number | Function | ValueObject;
[key: string]: number | string | Function | ValueObject;
};
declare class Expression {
static createOperatorExpression(operator: string, left: Expression, right: Expression): PowerExpression | MultDivExpression | PlusMinusExpression;
Expand Down Expand Up @@ -57,7 +57,7 @@ declare class FunctionExpression extends Expression {
formulaObject: Formula | null;
blacklisted: boolean | undefined;
constructor(fn: string | null, argumentExpressions: Expression[], formulaObject?: Formula | null);
evaluate(params?: ValueObject): number;
evaluate(params?: ValueObject): number | string;
toString(): string;
isBlacklisted(): boolean;
}
Expand All @@ -66,7 +66,7 @@ declare class VariableExpression extends Expression {
varPath: string[];
formulaObject: Formula | null;
constructor(fullPath: string, formulaObj?: Formula | null);
evaluate(params?: {}): number;
evaluate(params?: {}): number | string;
toString(): string;
}
export default class Formula {
Expand Down Expand Up @@ -150,7 +150,7 @@ export default class Formula {
*
* We want to create an expression tree out of the string. This is done in 2 stages:
* 1. form single expressions from the string: parse the string into known expression objects:
* - numbers/variables
* - numbers/[variables]/"strings"
* - operators
* - braces (with a sub-expression)
* - functions (with sub-expressions (aka argument expressions))
Expand Down Expand Up @@ -206,14 +206,14 @@ export default class Formula {
* @param {ValueObject|Array<ValueObject>} valueObj An object containing values for variables and (unknown) functions,
* or an array of such objects: If an array is given, all objects are evaluated and the results
* also returned as array.
* @return {Number|Array<Number>} The evaluated result, or an array with results
* @return {Number|String|(Number|String)[]} The evaluated result, or an array with results
*/
evaluate(valueObj: ValueObject | ValueObject[]): number | number[] | string | string[];
evaluate(valueObj: ValueObject | ValueObject[]): number | string | (number | string)[];
hashValues(valueObj: ValueObject): string;
resultFromMemory(valueObj: ValueObject): number | string | null;
storeInMemory(valueObj: ValueObject, value: number | string): void;
getExpression(): Expression | null;
getExpressionString(): string;
static calc(formula: string, valueObj?: ValueObject | null, options?: {}): string | number | string[] | number[];
static calc(formula: string, valueObj?: ValueObject | null, options?: {}): string | number | (string | number)[];
}
export {};
Loading

0 comments on commit 5bb9493

Please sign in to comment.