Skip to content

Commit

Permalink
Fix josdejong#2319: make the API of Parser.evaluate consistent with…
Browse files Browse the repository at this point in the history
… `math.evaluate`: support a list with expressions as input
  • Loading branch information
josdejong committed Sep 20, 2021
1 parent a0e6e34 commit ed0fe96
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# not yet published, version 9.4.6

- Fix #2319: make the API of `Parser.evaluate` consistent with `math.evaluate`:
support a list with expressions as input.
- Improved documentation of function `setCartesian`. Thanks @fieldfoxWim.


Expand Down
11 changes: 5 additions & 6 deletions src/expression/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { factory } from '../utils/factory.js'
import { createEmptyMap, toObject } from '../utils/map.js'

const name = 'Parser'
const dependencies = ['parse']
const dependencies = ['evaluate']

export const createParserClass = /* #__PURE__ */ factory(name, dependencies, ({ parse }) => {
export const createParserClass = /* #__PURE__ */ factory(name, dependencies, ({ evaluate }) => {
/**
* @constructor Parser
* Parser contains methods to evaluate or parse expressions, and has a number
Expand Down Expand Up @@ -73,15 +73,14 @@ export const createParserClass = /* #__PURE__ */ factory(name, dependencies, ({

/**
* Parse and evaluate the given expression
* @param {string} expr A string containing an expression, for example "2+3"
* @param {string | string[]} expr A string containing an expression,
* for example "2+3", or a list with expressions
* @return {*} result The result, or undefined when the expression was empty
* @throws {Error}
*/
Parser.prototype.evaluate = function (expr) {
// TODO: validate arguments
return parse(expr)
.compile()
.evaluate(this.scope)
return evaluate(expr, this.scope)
}

/**
Expand Down
8 changes: 8 additions & 0 deletions test/unit-tests/expression/Parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ describe('parser', function () {
assert.strictEqual(result, 5)
})

it('should evaluate a list with expressions', function () {
const parser = new Parser()

const result = parser.evaluate(['a = 2', 'a + 3'])
console.log('result', result, math.typeOf(result))
assert.deepStrictEqual(result, [2, 5])
})

it('should get variables from the parsers namespace ', function () {
const parser = new Parser()

Expand Down
8 changes: 4 additions & 4 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ declare namespace math {
interface FactoryFunctionMap {
[key: string]: FactoryFunction<any> | FactoryFunctionMap;
}


/** Available options for parse */
interface ParseOptions {
Expand Down Expand Up @@ -2431,7 +2431,7 @@ declare namespace math {

/**
* Test whether a value is an numeric value. In case of a string,
* true is returned if the string contains a numeric value.
* true is returned if the string contains a numeric value.
* @param x Value to be tested
* @returns Returns true when x is a number, BigNumber, Fraction, Boolean, or a String containing number.
* Returns false for other types.
Expand Down Expand Up @@ -2948,7 +2948,7 @@ declare namespace math {
toJSON(): MathJSON;
formatUnits(): string;
format(options: FormatOptions): string;
splitUnit(parts: ReadonlyArray<string | Unit>): Unit[];
splitUnit(parts: ReadonlyArray<string | Unit>): Unit[];
}

interface CreateUnitOptions {
Expand Down Expand Up @@ -3155,7 +3155,7 @@ declare namespace math {
}

interface Parser {
evaluate(expr: string): any;
evaluate(expr: string | string[]): any;
get(variable: string): any;
getAll(): { [key: string]: any };
set: (variable: string, value: any) => void;
Expand Down

0 comments on commit ed0fe96

Please sign in to comment.