Skip to content

fix: updates parser types #3226

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

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions test/typescript-tests/testTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1119,10 +1119,15 @@ Expressions examples
const _x = parser.get('x')
const f = parser.get('f')
const _y = parser.getAll()
const _z = parser.getAllAsMap()
const _g = f(3, 3)

parser.set('h', 500)
assert.strictEqual(parser.get('h'), 500)
assert.strictEqual(parser.evaluate('h'), 500)
parser.set('hello', (name: string) => `hello, ${name}!`)
parser.remove('h')
assert.strictEqual(parser.get('h'), undefined)
}

// clear defined functions and variables
Expand Down
34 changes: 32 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4292,14 +4292,44 @@ export interface MathNode {
}

export interface Parser {
/**
* Evaluate an expression. Returns the result of the expression.
* @param expr The expression to evaluate
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
evaluate(expr: string | string[]): any
/**
* Retrieve a variable or function from the parser’s scope.
* @param name The name of the variable or function to be retrieved
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
get(variable: string): any
get(name: string): any
/**
* Retrieve an object with all defined variables in the parser’s scope.
* @returns An object with all defined variables
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getAll(): { [key: string]: any }
/**
* Retrieve a map with all defined variables in the parser’s scope.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
set: (variable: string, value: any) => void
getAllAsMap(): Map<string, any>
/**
* Set a variable or function in the parser’s scope.
* @param name The name of the variable or function to be set
* @param value The value of the variable or function to be set
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
set: (name: string, value: any) => void
/**
* Remove a variable or function from the parser’s scope.
* @param name The name of the variable or function to be removed
*/
remove: (name: string) => void
/**
* Completely clear the parser’s scope.
*/
clear: () => void
}

Expand Down