/** * Type for a factory function that creates instances */ interface FactoryFunction { (scope: Record): TResult; isFactory: true; fn: string; dependencies: string[]; meta?: FactoryMeta; } /** * Type for legacy factory objects (old-style factories) */ interface LegacyFactory { type?: string; name: string; factory: (...args: unknown[]) => unknown; math?: boolean; dependencies?: string[]; meta?: FactoryMeta; } /** * Meta information that can be attached to a factory */ interface FactoryMeta { /** * If true, the factory will be recreated when config changes */ recreateOnConfigChange?: boolean; /** * If true, this is a lazy factory that should only be created when needed */ lazy?: boolean; /** * Additional custom metadata */ [key: string]: unknown; } /** * Type for dependency names, which can be optional (prefixed with '?') */ type DependencyName = string; /** * Type for the create callback function */ type CreateFunction, TResult> = (dependencies: TDeps) => TResult; /** * Create a factory function, which can be used to inject dependencies. * * The created functions are memoized, a consecutive call of the factory * with the exact same inputs will return the same function instance. * The memoized cache is exposed on `factory.cache` and can be cleared * if needed. * * Example: * * const name = 'log' * const dependencies = ['config', 'typed', 'divideScalar', 'Complex'] * * export const createLog = factory(name, dependencies, ({ typed, config, divideScalar, Complex }) => { * // ... create the function log here and return it * } * * @param name Name of the function to be created * @param dependencies The names of all required dependencies * @param create Callback function called with an object with all dependencies * @param meta Optional object with meta information that will be attached * to the created factory function as property `meta`. For explanation * of what meta properties can be specified and what they mean, see * docs/core/extension.md. * @returns The factory function */ declare function factory = Record, TResult = unknown>(name: string, dependencies: DependencyName[], create: CreateFunction, meta?: FactoryMeta): FactoryFunction; /** * Sort all factories such that when loading in order, the dependencies are resolved. * * @param factories Array of factory functions or legacy factories * @returns Returns a new array with the sorted factories */ declare function sortFactories(factories: Array): Array; declare function create(factories: Array, scope?: Record): Record; /** * Test whether an object is a factory. This is the case when it has * properties name, dependencies, and a function create. * @param obj Any value to test * @returns true if obj is a factory function */ declare function isFactory(obj: unknown): obj is FactoryFunction; /** * Assert that all dependencies of a list with dependencies are available in the provided scope. * * Will throw an exception when there are dependencies missing. * * @param name Name for the function to be created. Used to generate a useful error message * @param dependencies Array of dependency names * @param scope Object containing the available dependencies * @throws Error if required dependencies are missing */ declare function assertDependencies(name: string, dependencies: DependencyName[], scope: Record): void; /** * Check if a dependency is optional (starts with '?') * @param dependency The dependency name to check * @returns true if the dependency is optional */ declare function isOptionalDependency(dependency: DependencyName): boolean; /** * Remove the optional notation '?' from a dependency name * @param dependency The dependency name * @returns The dependency name without optional notation */ declare function stripOptionalNotation(dependency: DependencyName): string; interface BigNumber { isBigNumber: boolean; constructor: { prototype: { isBigNumber: boolean; }; isDecimal?: (x: unknown) => boolean; }; } interface Complex { re: number; im: number; } interface Fraction { n: number; d: number; } interface Unit$1 { constructor: { prototype: { isUnit: boolean; }; }; } interface Matrix { isMatrix?: boolean; _size?: number[]; constructor: { prototype: { isMatrix: boolean; }; }; } interface DenseMatrix extends Matrix { isDenseMatrix: boolean; } interface SparseMatrix extends Matrix { isSparseMatrix: boolean; } interface Range { start: number; end: number; step: number; constructor: { prototype: { isRange: boolean; }; }; } interface IndexDimension { _data?: unknown[]; _size: number[]; isRange?: boolean; start?: number; end?: number; } interface Index { _dimensions: (IndexDimension | string)[]; _sourceSize?: (number | null)[]; constructor: { prototype: { isIndex: boolean; }; }; } declare function isNumber(x: unknown): x is number; declare function isBigNumber(x: unknown): x is BigNumber; declare function isBigInt(x: unknown): x is bigint; declare function isComplex(x: unknown): x is Complex; declare function isFraction(x: unknown): x is Fraction; declare function isUnit(x: unknown): x is Unit$1; declare function isString(x: unknown): x is string; declare const isArray: (arg: any) => arg is any[]; declare function isMatrix(x: unknown): x is Matrix; /** * Test whether a value is a collection: an Array or Matrix * @param {*} x * @returns {boolean} isCollection */ declare function isCollection(x: unknown): x is unknown[] | Matrix; declare function isDenseMatrix(x: unknown): x is DenseMatrix; declare function isSparseMatrix(x: unknown): x is SparseMatrix; declare function isRange(x: unknown): x is Range; declare function isIndex(x: unknown): x is Index; declare function isBoolean(x: unknown): x is boolean; declare function isFunction(x: unknown): x is (...args: unknown[]) => unknown; declare function isDate(x: unknown): x is Date; declare function isRegExp(x: unknown): x is RegExp; declare function isObject(x: unknown): x is Record; /** * Returns `true` if the passed object appears to be a Map (i.e. duck typing). * * Methods looked for are `get`, `set`, `keys` and `has`. * * @param {Map | object} object * @returns */ declare function isMap(object: unknown): object is Map; declare function isNull(x: unknown): x is null; declare function isUndefined(x: unknown): x is undefined; declare function typeOf(x: unknown): string; /** * Shared TypeScript interfaces for the Unit factory (`Unit.ts`). * * The Unit type is a "function-as-class" (a constructor function whose * prototype is built up imperatively) ported from mathjs. These interfaces * describe the runtime shapes precisely so `Unit.ts` can be fully type-checked * under strict mode without suppression directives or escape-hatch typing. * * @module @danielsimonjr/mathts-core/types/unit/unit-types */ /** A complex number value (re/im pair), as produced by the `Complex` type. */ type ComplexValue = Complex; /** * Minimal structural view of a BigNumber value: only the instance methods that * the Unit factory actually calls when computing angle constants. */ interface BigNumberValue { div(other: BigNumberValue | number | string): BigNumberValue; times(other: BigNumberValue | number | string): BigNumberValue; } /** Structural view of a Fraction value (numerator/denominator/sign). */ interface FractionValue { n: number; d: number; s: number; isFraction?: boolean; } /** * Any numeric value a Unit can carry. Mirrors what `isNumeric`/`isComplex` * accept at the constructor boundary (number, bigint, boolean, BigNumber, * Fraction, Complex). Boolean/bigint flow through unchanged for valueless * units and are coerced by the scalar operations otherwise. */ type Numeric = number | bigint | boolean | BigNumberValue | FractionValue | ComplexValue; /** A prefix definition such as `{ name: 'k', value: 1e3, scientific: true }`. */ interface PrefixDef { name: string; value: number; scientific: boolean; } /** A named table of prefixes (e.g. PREFIXES.SHORT), keyed by prefix name. */ type PrefixTable = Record; /** A base dimension definition (the 9-element exponent vector + its key). */ interface BaseUnitDef { dimensions: number[]; key?: string; } /** * A unit definition entry in the UNITS table. `dimensions` and `base.key` are * populated after the literal is created (see the module-init loops), hence * optional; `value` is `null` for angle units until `calculateAngleValues` * runs at module init, before any unit is parsed. */ interface UnitDef { name: string; base?: BaseUnitDef; prefixes?: PrefixTable; value: Numeric | null; offset: number; dimensions?: number[]; reciprocal?: boolean; } /** A single component of a Unit's unit list (e.g. the `m` in `m/s^2`). */ interface UnitComponent { unit: UnitDef; prefix: PrefixDef; power: number; } /** An entry in a unit system, pairing a unit with a default prefix. */ interface UnitSystemEntry { unit: UnitDef; prefix: PrefixDef; } /** A unit system (e.g. `si`, `cgs`, `us`, `auto`), keyed by base-dimension. */ type UnitSystem = Record; /** JSON serialization shape produced by `Unit#toJSON`. */ interface UnitJSON { mathjs: 'Unit'; value: Numeric | null; unit: string | null; fixPrefix: boolean; skipSimp: boolean; } /** A numeric-type converter (number → BigNumber/Fraction/Complex/number). */ type ConverterFn = (x: Numeric) => Numeric; /** The table of numeric-type converters exposed on `Unit.typeConverters`. */ interface TypeConverters { BigNumber: ConverterFn; Fraction: ConverterFn; Complex: ConverterFn; number: ConverterFn; [key: string]: ConverterFn; } /** Options accepted by `Unit.parse`. */ interface ParseOptions { allowNoUnits?: boolean; } /** Formatting options accepted by `Unit#format` / `Unit#toBest`. */ interface UnitFormatOptions { offset?: number; [key: string]: unknown; } /** Options accepted by `Unit.createUnit`. */ interface CreateUnitOptions { override?: boolean; } /** Object form of a `createUnit` / `createUnitSingle` definition. */ interface CreateUnitDefObject { definition?: string | UnitInstance; prefixes?: string; offset?: number; baseName?: string; aliases?: string[]; } /** Config object the Unit factory reads (`config` dep + `on('config')`). */ interface UnitConfig { number: string; predictable?: boolean; } /** Constructor (with static `.I`) for Complex values. */ interface ComplexConstructor { readonly I: ComplexValue; } /** Constructor for BigNumber values. */ interface BigNumberConstructor { new (value: number | string): BigNumberValue; } /** Constructor for Fraction values. */ interface FractionConstructor { new (value: Numeric | string, denominator?: number): FractionValue; } /** `subtractScalar` is polymorphic: it also subtracts two Units (in splitUnit). */ interface SubtractScalar { (a: UnitInstance, b: UnitInstance): UnitInstance; (a: Numeric, b: Numeric): Numeric; } /** A binary scalar operation (add/multiply/divide/pow). */ type ScalarBinaryOp = (a: Numeric, b: Numeric) => Numeric; /** A unary scalar operation (abs/fix/round). */ type ScalarUnaryOp = (x: Numeric) => Numeric; /** * The dependency object injected into the Unit factory by the factory system. * `on` is optional (`?on` in the dependency list). The index signature reflects that * this is a factory *scope* (a string-keyed dependency record that may also carry * deps beyond the ones the Unit itself reads), and lets it satisfy the `factory` * helper's `Record` constraint. */ interface UnitDependencies { [key: string]: unknown; on?: (event: string, callback: (curr: UnitConfig, prev: UnitConfig) => void) => void; config: UnitConfig; addScalar: ScalarBinaryOp; subtractScalar: SubtractScalar; multiplyScalar: ScalarBinaryOp; divideScalar: ScalarBinaryOp; pow: ScalarBinaryOp; abs: ScalarUnaryOp; fix: ScalarUnaryOp; round: ScalarUnaryOp; equal: (a: unknown, b: unknown) => boolean; isNumeric: (x: unknown) => boolean; format: (value: unknown, options?: unknown) => string; number: (x: unknown) => number; Complex: ComplexConstructor; BigNumber: BigNumberConstructor; Fraction: FractionConstructor; } /** An instance of Unit (the prototype methods + per-instance fields). */ interface UnitInstance { value: Numeric | null; units: UnitComponent[]; dimensions: number[]; fixPrefix: boolean; skipAutomaticSimplification: boolean; type: 'Unit'; isUnit: boolean; constructor: UnitConstructor; clone(): UnitInstance; valueType(): string; _isDerived(): boolean; _normalize(value: Numeric | null | undefined): Numeric | null; _denormalize(value: Numeric | null, prefixValue?: Numeric): Numeric | null; hasBase(base: BaseUnitDef | string | undefined): boolean; equalBase(other: { dimensions: number[]; }): boolean; equals(other: UnitInstance): boolean; multiply(other: UnitInstance | Numeric): UnitInstance | Numeric; divideInto(numerator: Numeric): UnitInstance | Numeric; divide(other: UnitInstance | Numeric): UnitInstance | Numeric; pow(p: number): UnitInstance | Numeric; abs(): UnitInstance; to(valuelessUnit: string | UnitInstance): UnitInstance; toNumber(valuelessUnit?: string | UnitInstance): number; toNumeric(valuelessUnit?: string | UnitInstance): Numeric | null; toString(): string; toJSON(): UnitJSON; valueOf(): string; simplify(): UnitInstance; toSI(): UnitInstance; formatUnits(): string; toBest(unitList?: Array, options?: UnitFormatOptions): UnitInstance; format(options?: UnitFormatOptions): string; _bestPrefix(offset?: number): PrefixDef; splitUnit(parts: Array): UnitInstance[]; _numberConverter(): ConverterFn; } /** The Unit constructor function together with its static members. */ interface UnitConstructor { new (value?: Numeric | null, valuelessUnit?: string | UnitInstance): UnitInstance; prototype: UnitInstance; name: string; parse(str: string, options?: ParseOptions): UnitInstance; isValuelessUnit(name: string): boolean; isValidAlpha(c: string): boolean; fromJSON(json: UnitJSON): UnitInstance; setUnitSystem(name: string): void; getUnitSystem(): string | undefined; createUnit(obj: Record, options?: CreateUnitOptions): UnitInstance | undefined; createUnitSingle(name: string, obj?: unknown): UnitInstance; deleteUnit(name: string): void; _getNumberConverter(type: string): ConverterFn; typeConverters: TypeConverters; PREFIXES: Record; BASE_DIMENSIONS: string[]; BASE_UNITS: Record; UNIT_SYSTEMS: Record; UNITS: Record; } /** The core `Unit` class, pre-wired to core's numeric primitives. */ declare const Unit: UnitConstructor; export { isDate as $, type UnitDef as A, type BaseUnitDef as B, type Complex as C, type DenseMatrix as D, type UnitFormatOptions as E, type FactoryFunction as F, type UnitJSON as G, type UnitSystem as H, type Index as I, type UnitSystemEntry as J, assertDependencies as K, type LegacyFactory as L, type Matrix as M, type Numeric as N, create as O, type ParseOptions as P, factory as Q, type Range as R, type ScalarBinaryOp as S, type TypeConverters as T, Unit as U, isArray as V, isBigInt as W, isBigNumber as X, isBoolean as Y, isCollection as Z, isComplex as _, type UnitInstance as a, isDenseMatrix as a0, isFactory as a1, isFraction as a2, isFunction as a3, isIndex as a4, isMap as a5, isMatrix as a6, isNull as a7, isNumber as a8, isObject as a9, isOptionalDependency as aa, isRange as ab, isRegExp as ac, isSparseMatrix as ad, isString as ae, isUndefined as af, sortFactories as ag, stripOptionalNotation as ah, typeOf as ai, type UnitConstructor as b, type UnitDependencies as c, type BigNumber as d, type BigNumberConstructor as e, type ComplexConstructor as f, type ComplexValue as g, type ConverterFn as h, isUnit as i, type CreateFunction as j, type CreateUnitDefObject as k, type CreateUnitOptions as l, type DependencyName as m, type FactoryMeta as n, type Fraction as o, type FractionConstructor as p, type FractionValue as q, type IndexDimension as r, type PrefixDef as s, type PrefixTable as t, type ScalarUnaryOp as u, type SparseMatrix as v, type SubtractScalar as w, type Unit$1 as x, type UnitComponent as y, type UnitConfig as z };