Skip to content

Commit

Permalink
Merge branch 'typescript_improvements' of https://github.com/LearnSom…
Browse files Browse the repository at this point in the history
…ethingTeam/mathjs into LearnSomethingTeam-typescript_improvements
  • Loading branch information
josdejong committed Oct 2, 2021
2 parents 0afcd44 + 8317eb5 commit fc5c202
Showing 1 changed file with 216 additions and 37 deletions.
253 changes: 216 additions & 37 deletions types/index.d.ts
Expand Up @@ -156,6 +156,168 @@ declare namespace math {
isHexDigit(c: string): boolean;
}

interface AccessorNode extends MathNodeCommon {
type: 'AccessorNode';
isAccessorNode: true;
object: MathNode;
index: IndexNode;
name: string;
}
interface AccessorNodeCtor {
new(object: MathNode, index: IndexNode): AccessorNode;
}

interface ArrayNode extends MathNodeCommon {
type: 'ArrayNode';
isArrayNode: true;
items: MathNode[];
}
interface ArrayNodeCtor {
new(items: MathNode[]): ArrayNode;
}

interface AssignmentNode extends MathNodeCommon {
type: 'AssignmentNode';
isAssignmentNode: true;
object: SymbolNode | AccessorNode;
index: IndexNode | null;
value: MathNode;
name: string;
}
interface AssignmentNodeCtor {
new(object: SymbolNode, value: MathNode): AssignmentNode;
new(object: SymbolNode | AccessorNode, index: IndexNode, value: MathNode): AssignmentNode;
}

interface BlockNode extends MathNodeCommon {
type: 'BlockNode';
isBlockNode: true;
blocks: Array<{node: MathNode, visible: boolean}>;
}
interface BlockNodeCtor {
new(arr: Array<{node: MathNode} | {node: MathNode, visible: boolean}>): BlockNode;
}

interface ConditionalNode extends MathNodeCommon {
type: 'ConditionalNode';
isConditionalNode: boolean;
condition: MathNode;
trueExpr: MathNode;
falseExpr: MathNode;
}
interface ConditionalNodeCtor {
new(condition: MathNode, trueExpr: MathNode, falseExpr: MathNode): ConditionalNode;
}

interface ConstantNode extends MathNodeCommon {
type: 'ConstantNode';
isConstantNode: true;
value: any;
}

interface ConstantNodeCtor {
new(constant: number): ConstantNode;
}

interface FunctionAssignmentNode extends MathNodeCommon {
type: 'FunctionAssignmentNode';
isFunctionAssignmentNode: true;
name: string;
params: string[];
expr: MathNode;
}
interface FunctionAssignmentNodeCtor {
new(name: string, params: string[], expr: MathNode): FunctionAssignmentNode;
}

interface FunctionNode extends MathNodeCommon {
type: 'FunctionNode';
isFunctionNode: true;
fn: SymbolNode;
args: MathNode[];
}
interface FunctionNodeCtor {
new(fn: MathNode | string, args: MathNode[]): FunctionNode;
}

interface IndexNode extends MathNodeCommon {
type: 'IndexNode';
isIndexNode: true;
dimensions: MathNode[];
dotNotation: boolean;
}
interface IndexNodeCtor {
new(dimensions: MathNode[]): IndexNode;
new(dimensions: MathNode[], dotNotation: boolean): IndexNode;
}

interface ObjectNode extends MathNodeCommon {
type: 'ObjectNode';
isObjectNode: true;
properties: Record<string, MathNode>;
}
interface ObjectNodeCtor {
new(properties: Record<string, MathNode>): ObjectNode;
}

interface OperatorNode extends MathNodeCommon {
type: 'OperatorNode';
isOperatorNode: true;
op: string;
fn: string;
args: MathNode[];
implicit: boolean;
isUnary(): boolean;
isBinary(): boolean;
}
interface OperatorNodeCtor {
new(op: string, fn: string, args: MathNode[], implicit?: boolean): OperatorNode;
}

interface ParenthesisNode extends MathNodeCommon {
type: 'ParenthesisNode';
isParenthesisNode: true;
content: MathNode;
}
interface ParenthesisNodeCtor {
new(content: MathNode): ParenthesisNode;
}

interface RangeNode extends MathNodeCommon {
type: 'RangeNode';
isRangeNode: true;
start: MathNode;
end: MathNode;
step: MathNode | null;
}
interface RangeNodeCtor {
new(start: MathNode, end: MathNode, step?: MathNode): RangeNode;
}

interface RelationalNode extends MathNodeCommon {
type: 'RelationalNode';
isRelationalNode: true;
conditionals: string[];
params: MathNode[];
}
interface RelationalNodeCtor {
new(conditionals: string[], params: MathNode[]): RelationalNode;
}

interface SymbolNode extends MathNodeCommon {
type: 'SymbolNode';
isSymbolNode: true;
name: string;
}
interface SymbolNodeCtor {
new(name: string): SymbolNode;
}

type MathNode = AccessorNode | ArrayNode | AssignmentNode | BlockNode | ConditionalNode | ConstantNode |
FunctionAssignmentNode | FunctionNode | IndexNode | ObjectNode | OperatorNode | ParenthesisNode | RangeNode |
RelationalNode | SymbolNode;


type MathJsFunctionName = keyof MathJsStatic;

interface MathJsStatic extends FactoryDependencies {
Expand All @@ -173,6 +335,23 @@ declare namespace math {
SQRT2: number;
tau: number;

// Class-like constructors
AccessorNode: AccessorNodeCtor;
ArrayNode: ArrayNodeCtor;
AssignmentNode: AssignmentNodeCtor;
BlockNode: BlockNodeCtor;
ConditionalNode: ConditionalNodeCtor;
ConstantNode: ConstantNodeCtor;
FunctionAssignmentNode: FunctionAssignmentNodeCtor;
FunctionNode: FunctionNodeCtor;
IndexNode: IndexNodeCtor;
ObjectNode: ObjectNodeCtor;
OperatorNode: OperatorNodeCtor;
ParenthesisNode: ParenthesisNodeCtor;
RangeNode: RangeNodeCtor;
RelationalNode: RelationalNodeCtor;
SymbolNode: SymbolNodeCtor;

/**
* If null were to be included in this interface, it would be
* auto-suggested as an import in VSCode. This causes issues because
Expand Down Expand Up @@ -500,6 +679,7 @@ declare namespace math {
*/
qr(A: Matrix | MathArray): { Q: MathArray | Matrix; R: MathArray | Matrix };

rationalize(expr: MathNode | string, optional?: object | boolean, detailed?: false): MathNode;
/**
* Transform a rationalizable expression in a rational fraction. If
* rational fraction is one variable polynomial then converts the
Expand All @@ -517,7 +697,7 @@ declare namespace math {
optional?: object | boolean,
detailed?: true
): { expression: MathNode | string; variables: string[]; coefficients: MathType[] };
rationalize(expr: MathNode | string, optional?: object | boolean, detailed?: false): MathNode;


/**
* Simplify an expression tree.
Expand All @@ -530,13 +710,7 @@ declare namespace math {
* @param [options] (optional) An object with simplify options
* @returns Returns the simplified form of expr
*/
simplify(
expr: MathNode | string,
rules?: Array<{ l: string; r: string } | string | ((node: MathNode) => MathNode)>,
scope?: object,
options?: SimplifyOptions,
): MathNode;
simplify(expr: MathNode | string, scope?: object, options?: SimplifyOptions): MathNode;
simplify: Simplify;

/**
* Calculate the Sparse Matrix LU decomposition with full pivoting.
Expand Down Expand Up @@ -649,6 +823,7 @@ declare namespace math {
* @returns Quotient, x / y
*/
divide(x: Unit, y: Unit): Unit | number;
divide(x: Unit, y: number): Unit;
divide(x: number, y: number): number;
divide(x: MathType, y: MathType): MathType;

Expand Down Expand Up @@ -941,6 +1116,8 @@ declare namespace math {
* @param y Value to subtract from x
* @returns Subtraction of x and y
*/
subtract(x: number, y: number): number;
subtract(x: Unit, y: Unit): Unit;
subtract(x: MathType, y: MathType): MathType;

/**
Expand Down Expand Up @@ -2540,7 +2717,7 @@ declare namespace math {
* Factory and Dependencies
************************************************************************/
interface FactoryDependencies {
create: (factories: FactoryFunctionMap, config?: ConfigOptions) => Partial<MathJsStatic>;
create: (factories: FactoryFunctionMap, config?: ConfigOptions) => MathJsStatic;
factory: <T>(
name: string,
dependencies: MathJsFunctionName[],
Expand Down Expand Up @@ -2941,8 +3118,8 @@ declare namespace math {
pow(unit: Unit): Unit;
abs(unit: Unit): Unit;
to(unit: string): Unit;
toNumber(unit: string): number;
toNumeric(unit: string): number | Fraction | BigNumber;
toNumber(unit?: string): number;
toNumeric(unit?: string): number | Fraction | BigNumber;
toSI(): Unit;
toString(): string;
toJSON(): MathJSON;
Expand All @@ -2969,6 +3146,26 @@ declare namespace math {
fractionsLimit?: number;
}

type SimplifyRule = { l: string; r: string } | string | ((node: MathNode) => MathNode);

interface Simplify {
(
expr: MathNode | string,
rules?: SimplifyRule[],
scope?: object,
options?: SimplifyOptions,
): MathNode;
(
expr: MathNode | string,
scope?: object,
options?: SimplifyOptions,
): MathNode;

rules: SimplifyRule[];

simplifyCore(expr: MathNode): MathNode;
}

interface UnitDefinition {
definition?: string | Unit;
prefixes?: string;
Expand All @@ -2982,32 +3179,14 @@ declare namespace math {
evaluate(scope?: any): any;
}

interface MathNode {
isNode: boolean;
isAccessorNode?: boolean;
isArrayNode?: boolean;
isAssignmentNode?: boolean;
isBlockNode?: boolean;
isConditionalNode?: boolean;
isConstantNode?: boolean;
isFunctionAssignmentNode?: boolean;
isFunctionNode?: boolean;
isIndexNode?: boolean;
isObjectNode?: boolean;
isOperatorNode?: boolean;
isParenthesisNode?: boolean;
isRangeNode?: boolean;
isRelationalNode?: boolean;
isSymbolNode?: boolean;
interface MathNodeCommon {
isNode: true;
comment: string;
type: 'AccessorNode' | 'ArrayNode' | 'AssignmentNode' | 'BlockNode' | 'ConditionalNode' | 'ConstantNode' |
'FunctionAssignmentNode' | 'FunctionNode' | 'IndexNode' | 'ObjectNode' | 'OperatorNode' | 'ParenthesisNode' |
'RangeNode' | 'RelationalNode' | 'SymbolNode';

isUpdateNode?: boolean;
comment?: string;
content?: MathNode;
op?: string;
fn?: string;
args?: MathNode[];
type: string;
name?: string;
value?: any;

/**
* Create a shallow clone of the node. The node itself is cloned, its
Expand Down Expand Up @@ -3445,7 +3624,7 @@ declare namespace math {
* can be specified as an object, string, or function.
* @param scope Scope to variables
*/
simplify(rules?: Array<{ l: string; r: string } | string | ((node: MathNode) => MathNode)>, scope?: object): MathJsChain;
simplify(rules?: SimplifyRule[], scope?: object): MathJsChain;

/**
* Calculate the Sparse Matrix LU decomposition with full pivoting.
Expand Down

0 comments on commit fc5c202

Please sign in to comment.