@danielsimonjr/mathts-core 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,440 @@
1
+ interface BigNumber {
2
+ isBigNumber: boolean;
3
+ constructor: {
4
+ prototype: {
5
+ isBigNumber: boolean;
6
+ };
7
+ isDecimal?: (x: unknown) => boolean;
8
+ };
9
+ }
10
+ interface Complex {
11
+ re: number;
12
+ im: number;
13
+ }
14
+ interface Fraction {
15
+ n: number;
16
+ d: number;
17
+ }
18
+ interface Unit$1 {
19
+ constructor: {
20
+ prototype: {
21
+ isUnit: boolean;
22
+ };
23
+ };
24
+ }
25
+ interface Matrix {
26
+ isMatrix?: boolean;
27
+ _size?: number[];
28
+ constructor: {
29
+ prototype: {
30
+ isMatrix: boolean;
31
+ };
32
+ };
33
+ }
34
+ interface DenseMatrix extends Matrix {
35
+ isDenseMatrix: boolean;
36
+ }
37
+ interface SparseMatrix extends Matrix {
38
+ isSparseMatrix: boolean;
39
+ }
40
+ interface Range {
41
+ start: number;
42
+ end: number;
43
+ step: number;
44
+ constructor: {
45
+ prototype: {
46
+ isRange: boolean;
47
+ };
48
+ };
49
+ }
50
+ interface IndexDimension {
51
+ _data?: unknown[];
52
+ _size: number[];
53
+ isRange?: boolean;
54
+ start?: number;
55
+ end?: number;
56
+ }
57
+ interface Index {
58
+ _dimensions: (IndexDimension | string)[];
59
+ _sourceSize?: (number | null)[];
60
+ constructor: {
61
+ prototype: {
62
+ isIndex: boolean;
63
+ };
64
+ };
65
+ }
66
+ interface ResultSet {
67
+ entries: unknown[];
68
+ constructor: {
69
+ prototype: {
70
+ isResultSet: boolean;
71
+ };
72
+ };
73
+ }
74
+ interface Help {
75
+ constructor: {
76
+ prototype: {
77
+ isHelp: boolean;
78
+ };
79
+ };
80
+ }
81
+ interface Chain {
82
+ constructor: {
83
+ prototype: {
84
+ isChain: boolean;
85
+ };
86
+ };
87
+ }
88
+ interface Node {
89
+ isNode: boolean;
90
+ constructor: {
91
+ prototype: {
92
+ isNode: boolean;
93
+ };
94
+ };
95
+ }
96
+ interface AccessorNode extends Node {
97
+ isAccessorNode: boolean;
98
+ }
99
+ interface ArrayNode extends Node {
100
+ isArrayNode: boolean;
101
+ }
102
+ interface AssignmentNode extends Node {
103
+ isAssignmentNode: boolean;
104
+ }
105
+ interface BlockNode extends Node {
106
+ isBlockNode: boolean;
107
+ }
108
+ interface ConditionalNode extends Node {
109
+ isConditionalNode: boolean;
110
+ }
111
+ interface ConstantNode extends Node {
112
+ isConstantNode: boolean;
113
+ }
114
+ interface FunctionAssignmentNode extends Node {
115
+ isFunctionAssignmentNode: boolean;
116
+ }
117
+ interface FunctionNode extends Node {
118
+ isFunctionNode: boolean;
119
+ }
120
+ interface IndexNode extends Node {
121
+ isIndexNode: boolean;
122
+ }
123
+ interface ObjectNode extends Node {
124
+ isObjectNode: boolean;
125
+ }
126
+ interface OperatorNode extends Node {
127
+ isOperatorNode: boolean;
128
+ op: string;
129
+ args: Node[];
130
+ }
131
+ interface ParenthesisNode extends Node {
132
+ isParenthesisNode: boolean;
133
+ }
134
+ interface RangeNode extends Node {
135
+ isRangeNode: boolean;
136
+ }
137
+ interface RelationalNode extends Node {
138
+ isRelationalNode: boolean;
139
+ }
140
+ interface SymbolNode extends Node {
141
+ isSymbolNode: boolean;
142
+ }
143
+ interface PartitionedMap {
144
+ a: Map<unknown, unknown>;
145
+ b: Map<unknown, unknown>;
146
+ }
147
+ declare function isNumber(x: unknown): x is number;
148
+ declare function isBigNumber(x: unknown): x is BigNumber;
149
+ declare function isBigInt(x: unknown): x is bigint;
150
+ declare function isComplex(x: unknown): x is Complex;
151
+ declare function isFraction(x: unknown): x is Fraction;
152
+ declare function isUnit(x: unknown): x is Unit$1;
153
+ declare function isString(x: unknown): x is string;
154
+ declare const isArray: (arg: any) => arg is any[];
155
+ declare function isMatrix(x: unknown): x is Matrix;
156
+ /**
157
+ * Test whether a value is a collection: an Array or Matrix
158
+ * @param {*} x
159
+ * @returns {boolean} isCollection
160
+ */
161
+ declare function isCollection(x: unknown): x is unknown[] | Matrix;
162
+ declare function isDenseMatrix(x: unknown): x is DenseMatrix;
163
+ declare function isSparseMatrix(x: unknown): x is SparseMatrix;
164
+ declare function isRange(x: unknown): x is Range;
165
+ declare function isIndex(x: unknown): x is Index;
166
+ declare function isBoolean(x: unknown): x is boolean;
167
+ declare function isResultSet(x: unknown): x is ResultSet;
168
+ declare function isHelp(x: unknown): x is Help;
169
+ declare function isFunction(x: unknown): x is (...args: unknown[]) => unknown;
170
+ declare function isDate(x: unknown): x is Date;
171
+ declare function isRegExp(x: unknown): x is RegExp;
172
+ declare function isObject(x: unknown): x is Record<string, unknown>;
173
+ /**
174
+ * Returns `true` if the passed object appears to be a Map (i.e. duck typing).
175
+ *
176
+ * Methods looked for are `get`, `set`, `keys` and `has`.
177
+ *
178
+ * @param {Map | object} object
179
+ * @returns
180
+ */
181
+ declare function isMap(object: unknown): object is Map<unknown, unknown>;
182
+ declare function isPartitionedMap(object: unknown): object is PartitionedMap;
183
+ declare function isObjectWrappingMap(object: unknown): boolean;
184
+ declare function isNull(x: unknown): x is null;
185
+ declare function isUndefined(x: unknown): x is undefined;
186
+ declare function isAccessorNode(x: unknown): x is AccessorNode;
187
+ declare function isArrayNode(x: unknown): x is ArrayNode;
188
+ declare function isAssignmentNode(x: unknown): x is AssignmentNode;
189
+ declare function isBlockNode(x: unknown): x is BlockNode;
190
+ declare function isConditionalNode(x: unknown): x is ConditionalNode;
191
+ declare function isConstantNode(x: unknown): x is ConstantNode;
192
+ declare function rule2Node(node: Node): boolean;
193
+ declare function isFunctionAssignmentNode(x: unknown): x is FunctionAssignmentNode;
194
+ declare function isFunctionNode(x: unknown): x is FunctionNode;
195
+ declare function isIndexNode(x: unknown): x is IndexNode;
196
+ declare function isNode(x: unknown): x is Node;
197
+ declare function isObjectNode(x: unknown): x is ObjectNode;
198
+ declare function isOperatorNode(x: unknown): x is OperatorNode;
199
+ declare function isParenthesisNode(x: unknown): x is ParenthesisNode;
200
+ declare function isRangeNode(x: unknown): x is RangeNode;
201
+ declare function isRelationalNode(x: unknown): x is RelationalNode;
202
+ declare function isSymbolNode(x: unknown): x is SymbolNode;
203
+ declare function isChain(x: unknown): x is Chain;
204
+ declare function typeOf(x: unknown): string;
205
+
206
+ /**
207
+ * Shared TypeScript interfaces for the Unit factory (`Unit.ts`).
208
+ *
209
+ * The Unit type is a "function-as-class" (a constructor function whose
210
+ * prototype is built up imperatively) ported from mathjs. These interfaces
211
+ * describe the runtime shapes precisely so `Unit.ts` can be fully type-checked
212
+ * under strict mode without suppression directives or escape-hatch typing.
213
+ *
214
+ * @module @danielsimonjr/mathts-core/types/unit/unit-types
215
+ */
216
+
217
+ /** A complex number value (re/im pair), as produced by the `Complex` type. */
218
+ type ComplexValue = Complex;
219
+ /**
220
+ * Minimal structural view of a BigNumber value: only the instance methods that
221
+ * the Unit factory actually calls when computing angle constants.
222
+ */
223
+ interface BigNumberValue {
224
+ div(other: BigNumberValue | number | string): BigNumberValue;
225
+ times(other: BigNumberValue | number | string): BigNumberValue;
226
+ }
227
+ /** Structural view of a Fraction value (numerator/denominator/sign). */
228
+ interface FractionValue {
229
+ n: number;
230
+ d: number;
231
+ s: number;
232
+ isFraction?: boolean;
233
+ }
234
+ /**
235
+ * Any numeric value a Unit can carry. Mirrors what `isNumeric`/`isComplex`
236
+ * accept at the constructor boundary (number, bigint, boolean, BigNumber,
237
+ * Fraction, Complex). Boolean/bigint flow through unchanged for valueless
238
+ * units and are coerced by the scalar operations otherwise.
239
+ */
240
+ type Numeric = number | bigint | boolean | BigNumberValue | FractionValue | ComplexValue;
241
+ /** A prefix definition such as `{ name: 'k', value: 1e3, scientific: true }`. */
242
+ interface PrefixDef {
243
+ name: string;
244
+ value: number;
245
+ scientific: boolean;
246
+ }
247
+ /** A named table of prefixes (e.g. PREFIXES.SHORT), keyed by prefix name. */
248
+ type PrefixTable = Record<string, PrefixDef>;
249
+ /** A base dimension definition (the 9-element exponent vector + its key). */
250
+ interface BaseUnitDef {
251
+ dimensions: number[];
252
+ key?: string;
253
+ }
254
+ /**
255
+ * A unit definition entry in the UNITS table. `dimensions` and `base.key` are
256
+ * populated after the literal is created (see the module-init loops), hence
257
+ * optional; `value` is `null` for angle units until `calculateAngleValues`
258
+ * runs at module init, before any unit is parsed.
259
+ */
260
+ interface UnitDef {
261
+ name: string;
262
+ base?: BaseUnitDef;
263
+ prefixes?: PrefixTable;
264
+ value: Numeric | null;
265
+ offset: number;
266
+ dimensions?: number[];
267
+ reciprocal?: boolean;
268
+ }
269
+ /** A single component of a Unit's unit list (e.g. the `m` in `m/s^2`). */
270
+ interface UnitComponent {
271
+ unit: UnitDef;
272
+ prefix: PrefixDef;
273
+ power: number;
274
+ }
275
+ /** An entry in a unit system, pairing a unit with a default prefix. */
276
+ interface UnitSystemEntry {
277
+ unit: UnitDef;
278
+ prefix: PrefixDef;
279
+ }
280
+ /** A unit system (e.g. `si`, `cgs`, `us`, `auto`), keyed by base-dimension. */
281
+ type UnitSystem = Record<string, UnitSystemEntry>;
282
+ /** JSON serialization shape produced by `Unit#toJSON`. */
283
+ interface UnitJSON {
284
+ mathjs: 'Unit';
285
+ value: Numeric | null;
286
+ unit: string | null;
287
+ fixPrefix: boolean;
288
+ skipSimp: boolean;
289
+ }
290
+ /** A numeric-type converter (number → BigNumber/Fraction/Complex/number). */
291
+ type ConverterFn = (x: Numeric) => Numeric;
292
+ /** The table of numeric-type converters exposed on `Unit.typeConverters`. */
293
+ interface TypeConverters {
294
+ BigNumber: ConverterFn;
295
+ Fraction: ConverterFn;
296
+ Complex: ConverterFn;
297
+ number: ConverterFn;
298
+ [key: string]: ConverterFn;
299
+ }
300
+ /** Options accepted by `Unit.parse`. */
301
+ interface ParseOptions {
302
+ allowNoUnits?: boolean;
303
+ }
304
+ /** Formatting options accepted by `Unit#format` / `Unit#toBest`. */
305
+ interface UnitFormatOptions {
306
+ offset?: number;
307
+ [key: string]: unknown;
308
+ }
309
+ /** Options accepted by `Unit.createUnit`. */
310
+ interface CreateUnitOptions {
311
+ override?: boolean;
312
+ }
313
+ /** Object form of a `createUnit` / `createUnitSingle` definition. */
314
+ interface CreateUnitDefObject {
315
+ definition?: string | UnitInstance;
316
+ prefixes?: string;
317
+ offset?: number;
318
+ baseName?: string;
319
+ aliases?: string[];
320
+ }
321
+ /** Config object the Unit factory reads (`config` dep + `on('config')`). */
322
+ interface UnitConfig {
323
+ number: string;
324
+ predictable?: boolean;
325
+ }
326
+ /** Constructor (with static `.I`) for Complex values. */
327
+ interface ComplexConstructor {
328
+ readonly I: ComplexValue;
329
+ }
330
+ /** Constructor for BigNumber values. */
331
+ interface BigNumberConstructor {
332
+ new (value: number | string): BigNumberValue;
333
+ }
334
+ /** Constructor for Fraction values. */
335
+ interface FractionConstructor {
336
+ new (value: Numeric | string, denominator?: number): FractionValue;
337
+ }
338
+ /** `subtractScalar` is polymorphic: it also subtracts two Units (in splitUnit). */
339
+ interface SubtractScalar {
340
+ (a: UnitInstance, b: UnitInstance): UnitInstance;
341
+ (a: Numeric, b: Numeric): Numeric;
342
+ }
343
+ /** A binary scalar operation (add/multiply/divide/pow). */
344
+ type ScalarBinaryOp = (a: Numeric, b: Numeric) => Numeric;
345
+ /** A unary scalar operation (abs/fix/round). */
346
+ type ScalarUnaryOp = (x: Numeric) => Numeric;
347
+ /**
348
+ * The dependency object injected into the Unit factory by the factory system.
349
+ * `on` is optional (`?on` in the dependency list). The index signature reflects that
350
+ * this is a factory *scope* (a string-keyed dependency record that may also carry
351
+ * deps beyond the ones the Unit itself reads), and lets it satisfy the `factory`
352
+ * helper's `Record<string, unknown>` constraint.
353
+ */
354
+ interface UnitDependencies {
355
+ [key: string]: unknown;
356
+ on?: (event: string, callback: (curr: UnitConfig, prev: UnitConfig) => void) => void;
357
+ config: UnitConfig;
358
+ addScalar: ScalarBinaryOp;
359
+ subtractScalar: SubtractScalar;
360
+ multiplyScalar: ScalarBinaryOp;
361
+ divideScalar: ScalarBinaryOp;
362
+ pow: ScalarBinaryOp;
363
+ abs: ScalarUnaryOp;
364
+ fix: ScalarUnaryOp;
365
+ round: ScalarUnaryOp;
366
+ equal: (a: unknown, b: unknown) => boolean;
367
+ isNumeric: (x: unknown) => boolean;
368
+ format: (value: unknown, options?: unknown) => string;
369
+ number: (x: unknown) => number;
370
+ Complex: ComplexConstructor;
371
+ BigNumber: BigNumberConstructor;
372
+ Fraction: FractionConstructor;
373
+ }
374
+ /** An instance of Unit (the prototype methods + per-instance fields). */
375
+ interface UnitInstance {
376
+ value: Numeric | null;
377
+ units: UnitComponent[];
378
+ dimensions: number[];
379
+ fixPrefix: boolean;
380
+ skipAutomaticSimplification: boolean;
381
+ type: 'Unit';
382
+ isUnit: boolean;
383
+ constructor: UnitConstructor;
384
+ clone(): UnitInstance;
385
+ valueType(): string;
386
+ _isDerived(): boolean;
387
+ _normalize(value: Numeric | null | undefined): Numeric | null;
388
+ _denormalize(value: Numeric | null, prefixValue?: Numeric): Numeric | null;
389
+ hasBase(base: BaseUnitDef | string | undefined): boolean;
390
+ equalBase(other: {
391
+ dimensions: number[];
392
+ }): boolean;
393
+ equals(other: UnitInstance): boolean;
394
+ multiply(other: UnitInstance | Numeric): UnitInstance | Numeric;
395
+ divideInto(numerator: Numeric): UnitInstance | Numeric;
396
+ divide(other: UnitInstance | Numeric): UnitInstance | Numeric;
397
+ pow(p: number): UnitInstance | Numeric;
398
+ abs(): UnitInstance;
399
+ to(valuelessUnit: string | UnitInstance): UnitInstance;
400
+ toNumber(valuelessUnit?: string | UnitInstance): number;
401
+ toNumeric(valuelessUnit?: string | UnitInstance): Numeric | null;
402
+ toString(): string;
403
+ toJSON(): UnitJSON;
404
+ valueOf(): string;
405
+ simplify(): UnitInstance;
406
+ toSI(): UnitInstance;
407
+ formatUnits(): string;
408
+ toBest(unitList?: Array<string | UnitInstance>, options?: UnitFormatOptions): UnitInstance;
409
+ format(options?: UnitFormatOptions): string;
410
+ _bestPrefix(offset?: number): PrefixDef;
411
+ splitUnit(parts: Array<string | UnitInstance>): UnitInstance[];
412
+ _numberConverter(): ConverterFn;
413
+ }
414
+ /** The Unit constructor function together with its static members. */
415
+ interface UnitConstructor {
416
+ new (value?: Numeric | null, valuelessUnit?: string | UnitInstance): UnitInstance;
417
+ prototype: UnitInstance;
418
+ name: string;
419
+ parse(str: string, options?: ParseOptions): UnitInstance;
420
+ isValuelessUnit(name: string): boolean;
421
+ isValidAlpha(c: string): boolean;
422
+ fromJSON(json: UnitJSON): UnitInstance;
423
+ setUnitSystem(name: string): void;
424
+ getUnitSystem(): string | undefined;
425
+ createUnit(obj: Record<string, unknown>, options?: CreateUnitOptions): UnitInstance | undefined;
426
+ createUnitSingle(name: string, obj?: unknown): UnitInstance;
427
+ deleteUnit(name: string): void;
428
+ _getNumberConverter(type: string): ConverterFn;
429
+ typeConverters: TypeConverters;
430
+ PREFIXES: Record<string, PrefixTable>;
431
+ BASE_DIMENSIONS: string[];
432
+ BASE_UNITS: Record<string, BaseUnitDef>;
433
+ UNIT_SYSTEMS: Record<string, UnitSystem>;
434
+ UNITS: Record<string, UnitDef>;
435
+ }
436
+
437
+ /** The core `Unit` class, pre-wired to core's numeric primitives. */
438
+ declare const Unit: UnitConstructor;
439
+
440
+ export { type UnitComponent as $, type AccessorNode as A, type BaseUnitDef as B, type Chain as C, type DenseMatrix as D, type ParseOptions as E, type Fraction as F, type PartitionedMap as G, type Help as H, type Index as I, type PrefixDef as J, type PrefixTable as K, type RangeNode as L, type Matrix as M, type Node as N, type ObjectNode as O, type ParenthesisNode as P, type RelationalNode as Q, type Range as R, type ResultSet as S, type ScalarBinaryOp as T, Unit as U, type ScalarUnaryOp as V, type SparseMatrix as W, type SubtractScalar as X, type SymbolNode as Y, type TypeConverters as Z, type Unit$1 as _, type UnitInstance as a, type UnitConfig as a0, type UnitDef as a1, type UnitFormatOptions as a2, type UnitJSON as a3, type UnitSystem as a4, type UnitSystemEntry as a5, isAccessorNode as a6, isArray as a7, isArrayNode as a8, isAssignmentNode as a9, isOperatorNode as aA, isParenthesisNode as aB, isPartitionedMap as aC, isRange as aD, isRangeNode as aE, isRegExp as aF, isRelationalNode as aG, isResultSet as aH, isSparseMatrix as aI, isString as aJ, isSymbolNode as aK, isUndefined as aL, rule2Node as aM, typeOf as aN, isBigInt as aa, isBigNumber as ab, isBlockNode as ac, isBoolean as ad, isChain as ae, isCollection as af, isComplex as ag, isConditionalNode as ah, isConstantNode as ai, isDate as aj, isDenseMatrix as ak, isFraction as al, isFunction as am, isFunctionAssignmentNode as an, isFunctionNode as ao, isHelp as ap, isIndex as aq, isIndexNode as ar, isMap as as, isMatrix as at, isNode as au, isNull as av, isNumber as aw, isObject as ax, isObjectNode as ay, isObjectWrappingMap as az, type UnitConstructor as b, type UnitDependencies as c, type ArrayNode as d, type AssignmentNode as e, type BigNumber as f, type BigNumberConstructor as g, type BigNumberValue as h, isUnit as i, type BlockNode as j, type Complex as k, type ComplexConstructor as l, type ComplexValue as m, type ConditionalNode as n, type ConstantNode as o, type ConverterFn as p, type CreateUnitDefObject as q, type CreateUnitOptions as r, type FractionConstructor as s, type FractionValue as t, type FunctionAssignmentNode as u, type FunctionNode as v, type IndexDimension as w, type IndexNode as x, type Numeric as y, type OperatorNode as z };