UNPKG

239 kBTypeScriptView Raw
1import { Decimal } from 'decimal.js'
2import { Fraction } from 'fraction.js'
3
4export { Fraction }
5
6export as namespace math
7
8export type NoLiteralType<T> = T extends number
9 ? number
10 : T extends string
11 ? string
12 : T extends boolean
13 ? boolean
14 : T
15
16export type MathNumericType = number | BigNumber | bigint | Fraction | Complex
17export type MathScalarType = MathNumericType | Unit
18export type MathGeneric<T extends MathScalarType = MathNumericType> = T
19export type MathArray<T = MathGeneric> = T[] | Array<MathArray<T>>
20export type MathCollection<T = MathGeneric> = MathArray<T> | Matrix<T>
21export type MathType = MathScalarType | MathCollection
22export type MathExpression = string | string[] | MathCollection
23
24// eslint-disable-next-line @typescript-eslint/no-explicit-any
25export type FactoryFunction<T> = (scope: any) => T
26
27// FactoryFunctionMap can be nested; all nested objects will be flattened
28export interface FactoryFunctionMap {
29 // eslint-disable-next-line @typescript-eslint/no-explicit-any
30 [key: string]: FactoryFunction<any> | FactoryFunctionMap
31}
32
33/** Available options for parse */
34export interface ParseOptions {
35 /** a set of custom nodes */
36 nodes?: Record<string, MathNode>
37}
38/**
39 * Parse an expression. Returns a node tree, which can be evaluated by
40 * invoking node.evaluate().
41 *
42 * Note the evaluating arbitrary expressions may involve security risks,
43 * see [https://mathjs.org/docs/expressions/security.html](https://mathjs.org/docs/expressions/security.html) for more information.
44 *
45 * Syntax:
46 *
47 * math.parse(expr)
48 * math.parse(expr, options)
49 * math.parse([expr1, expr2, expr3, ...])
50 * math.parse([expr1, expr2, expr3, ...], options)
51 *
52 * Example:
53 *
54 * const node1 = math.parse('sqrt(3^2 + 4^2)')
55 * node1.compile().evaluate() // 5
56 *
57 * let scope = {a:3, b:4}
58 * const node2 = math.parse('a * b') // 12
59 * const code2 = node2.compile()
60 * code2.evaluate(scope) // 12
61 * scope.a = 5
62 * code2.evaluate(scope) // 20
63 *
64 * const nodes = math.parse(['a = 3', 'b = 4', 'a * b'])
65 * nodes[2].compile().evaluate() // 12
66 *
67 * See also:
68 *
69 * evaluate, compile
70 */
71export interface ParseFunction {
72 /**
73 * Parse an expression. Returns a node tree, which can be evaluated by
74 * invoking node.evaluate();
75 *
76 * @param expr Expression to be parsed
77 * @param options Available options
78 * @returns A node
79 */
80 (expr: MathExpression, options?: ParseOptions): MathNode
81
82 /**
83 * Parse an expression. Returns a node tree, which can be evaluated by
84 * invoking node.evaluate();
85 *
86 * @param exprs Expressions to be parsed
87 * @param options Available options
88 * @returns An array of nodes
89 */
90 (exprs: MathExpression[], options?: ParseOptions): MathNode[]
91
92 /**
93 * Checks whether the current character `c` is a valid alpha character:
94 *
95 * - A latin letter (upper or lower case) Ascii: a-z, A-Z
96 * - An underscore Ascii: _
97 * - A dollar sign Ascii: $
98 * - A latin letter with accents Unicode: \u00C0 - \u02AF
99 * - A greek letter Unicode: \u0370 - \u03FF
100 * - A mathematical alphanumeric symbol Unicode: \u{1D400} - \u{1D7FF} excluding invalid code points
101 *
102 * The previous and next characters are needed to determine whether
103 * this character is part of a unicode surrogate pair.
104 *
105 * @param c Current character in the expression
106 * @param cPrev Previous character
107 * @param cNext Next character
108 */
109 isAlpha(c: string, cPrev: string, cNext: string): boolean
110 /**
111 * Test whether a character is a valid latin, greek, or letter-like character
112 *
113 * @param c
114 */
115 isValidLatinOrGreek(c: string): boolean
116 /**
117 * Test whether two given 16 bit characters form a surrogate pair of a
118 * unicode math symbol.
119 *
120 * https://unicode-table.com/en/
121 * https://www.wikiwand.com/en/Mathematical_operators_and_symbols_in_Unicode
122 *
123 * Note: In ES6 will be unicode aware:
124 * https://stackoverflow.com/questions/280712/javascript-unicode-regexes
125 * https://mathiasbynens.be/notes/es6-unicode-regex
126 *
127 * @param high
128 * @param low
129 */
130 isValidMathSymbol(high: string, low: string): boolean
131 /**
132 * Check whether given character c is a white space character: space, tab, or enter
133 *
134 * @param c
135 * @param nestingLevel
136 */
137 isWhitespace(c: string, nestingLevel: number): boolean
138 /**
139 * Test whether the character c is a decimal mark (dot).
140 * This is the case when it's not the start of a delimiter '.*', './', or '.^'
141 *
142 * @param c
143 * @param cNext
144 */
145 isDecimalMark(c: string, cNext: string): boolean
146 /**
147 * checks if the given char c is a digit or dot
148 *
149 * @param c a string with one character
150 */
151 isDigitDot(c: string): boolean
152 /**
153 * checks if the given char c is a digit
154 *
155 * @param c a string with one character
156 */
157 isDigit(c: string): boolean
158 /**
159 * checks if the given char c is a hex digit
160 *
161 * @param c a string with one character
162 */
163 isHexDigit(c: string): boolean
164}
165
166export interface NodeCtor {
167 new (): MathNode
168}
169
170export interface AccessorNode<TObject extends MathNode = MathNode>
171 extends MathNode {
172 type: 'AccessorNode'
173 isAccessorNode: true
174 object: TObject
175 index: IndexNode
176 name: string
177}
178export interface AccessorNodeCtor {
179 new <TObject extends MathNode = MathNode>(
180 object: TObject,
181 index: IndexNode
182 ): AccessorNode<TObject>
183}
184
185export interface ArrayNode<TItems extends MathNode[] = MathNode[]>
186 extends MathNode {
187 type: 'ArrayNode'
188 isArrayNode: true
189 items: [...TItems]
190}
191export interface ArrayNodeCtor {
192 new <TItems extends MathNode[] = MathNode[]>(
193 items: [...TItems]
194 ): ArrayNode<TItems>
195