Skip to content
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

fix: types static methods and members for Unit class #3230

Merged
Show file tree
Hide file tree
Changes from 4 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
31 changes: 31 additions & 0 deletions test/typescript-tests/testTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
SymbolNode,
MathNodeCommon,
Unit,
UnitPrefix,
Node,
isSymbolNode,
MathScalarType
Expand Down Expand Up @@ -1609,6 +1610,36 @@ Units examples
math.unit('1 m').splitUnit(['ft', 'in'])
}

/**
* Unit static methods and members
*/
{
expectTypeOf(new Unit(15, 'cm')).toMatchTypeOf<Unit>()

const prefixes = Unit.PREFIXES
assert.ok(Object.keys(prefixes).length > 0)
expectTypeOf(Unit.PREFIXES).toMatchTypeOf<Record<string, UnitPrefix>>()

const baseDimensions = Unit.BASE_DIMENSIONS
assert.ok(baseDimensions.length > 0)
expectTypeOf(Unit.BASE_DIMENSIONS).toMatchTypeOf<string[]>()

const baseUnits = Unit.BASE_UNITS
assert.ok(Object.keys(baseUnits).length > 0)

const unitSystems = Unit.UNIT_SYSTEMS
assert.ok(Object.keys(unitSystems).length > 0)

const units = Unit.UNITS
assert.ok(Object.keys(units).length > 0)

expectTypeOf(Unit.isValuelessUnit('cm')).toMatchTypeOf<boolean>()
expectTypeOf(Unit.parse('5cm')).toMatchTypeOf<Unit>()
expectTypeOf(
Unit.fromJSON({ value: 5.2, unit: 'inch' })
).toMatchTypeOf<Unit>()
}

/**
* Example of custom fallback for onUndefinedSymbol & onUndefinedFunction
*/
Expand Down
24 changes: 24 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ export interface MathJsInstance extends MathJsFactory {
RelationalNode: RelationalNodeCtor
SymbolNode: SymbolNodeCtor

Unit: UnitCtor
Matrix: MatrixCtor

/**
Expand Down Expand Up @@ -4014,6 +4015,7 @@ export interface UnitPrefix {
export interface Unit {
valueOf(): string
clone(): Unit
_isDerived(): boolean
josdejong marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line @typescript-eslint/no-explicit-any
hasBase(base: any): boolean
equalBase(unit: Unit): boolean
Expand All @@ -4040,6 +4042,27 @@ export interface Unit {
skipAutomaticSimplification: true
}

export interface UnitStatic {
PREFIXES: Record<string, UnitPrefix>
BASE_DIMENSIONS: string[]
BASE_UNITS: Record<string, { dimensions: number[] }>
UNIT_SYSTEMS: Record<
string,
Record<string, { unit: Unit; prefix: UnitPrefix }>
>
UNITS: Record<string, Unit>
parse(str: string): Unit
isValuelessUnit(name: string): boolean
josdejong marked this conversation as resolved.
Show resolved Hide resolved
fromJSON(json: MathJSON): Unit
}

export interface UnitCtor extends UnitStatic {
new (
value: number | BigNumber | Fraction | Complex | boolean,
name: string
): Unit
}

export interface CreateUnitOptions {
prefixes?: 'none' | 'short' | 'long' | 'binary_short' | 'binary_long'
aliases?: string[]
Expand Down Expand Up @@ -6846,6 +6869,7 @@ export const {
RelationalNode,
SymbolNode,
Matrix,
Unit,

uninitialized,
version,
Expand Down