@danielsimonjr/mathts-core 0.10.0 → 0.11.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.
@@ -1342,6 +1342,127 @@ function isPath(str) {
1342
1342
  return str.includes(".");
1343
1343
  }
1344
1344
 
1345
+ // src/error/MathjsError.ts
1346
+ var MathjsError = class _MathjsError extends Error {
1347
+ isMathjsError = true;
1348
+ /**
1349
+ * Create a MathjsError
1350
+ * @param message Error message
1351
+ */
1352
+ constructor(message) {
1353
+ super(message);
1354
+ this.name = "MathjsError";
1355
+ const errorCtor = Error;
1356
+ if (errorCtor.captureStackTrace) {
1357
+ errorCtor.captureStackTrace(this, _MathjsError);
1358
+ }
1359
+ }
1360
+ };
1361
+
1362
+ // src/factory.ts
1363
+ function factory(name2, dependencies2, create2, meta) {
1364
+ function assertAndCreate(scope) {
1365
+ const deps = pickShallow(scope, dependencies2.map(stripOptionalNotation));
1366
+ assertDependencies(name2, dependencies2, scope);
1367
+ return create2(deps);
1368
+ }
1369
+ assertAndCreate.isFactory = true;
1370
+ assertAndCreate.fn = name2;
1371
+ assertAndCreate.dependencies = dependencies2.slice().sort();
1372
+ if (meta) {
1373
+ assertAndCreate.meta = meta;
1374
+ }
1375
+ return assertAndCreate;
1376
+ }
1377
+ function sortFactories(factories) {
1378
+ const factoriesByName = {};
1379
+ factories.forEach((factory2) => {
1380
+ const name2 = isFactory(factory2) ? factory2.fn : factory2.name;
1381
+ factoriesByName[name2] = factory2;
1382
+ });
1383
+ const checkedForCycles = /* @__PURE__ */ new Set();
1384
+ function checkCycles(fact, visiting, visited) {
1385
+ if (isFactory(fact)) {
1386
+ const factName = fact.fn;
1387
+ if (visiting.has(factName)) {
1388
+ throw new Error(`Circular dependency detected: ${factName}`);
1389
+ }
1390
+ if (visited.has(factName)) {
1391
+ return;
1392
+ }
1393
+ visiting.add(factName);
1394
+ fact.dependencies.forEach((d) => {
1395
+ const depFactory = factoriesByName[d];
1396
+ if (depFactory) {
1397
+ checkCycles(depFactory, visiting, visited);
1398
+ }
1399
+ });
1400
+ visiting.delete(factName);
1401
+ visited.add(factName);
1402
+ }
1403
+ }
1404
+ factories.forEach((factory2) => {
1405
+ if (isFactory(factory2)) {
1406
+ checkCycles(factory2, /* @__PURE__ */ new Set(), checkedForCycles);
1407
+ }
1408
+ });
1409
+ function containsDependency(factory2, dependency) {
1410
+ if (isFactory(factory2)) {
1411
+ const depName = isFactory(dependency) ? dependency.fn : dependency.name;
1412
+ if (factory2.dependencies.includes(depName)) {
1413
+ return true;
1414
+ }
1415
+ if (factory2.dependencies.some((d) => {
1416
+ const depFactory = factoriesByName[d];
1417
+ return depFactory && containsDependency(depFactory, dependency);
1418
+ })) {
1419
+ return true;
1420
+ }
1421
+ }
1422
+ return false;
1423
+ }
1424
+ const sorted = [];
1425
+ function addFactory(factory2) {
1426
+ let index = 0;
1427
+ while (index < sorted.length && !containsDependency(sorted[index], factory2)) {
1428
+ index++;
1429
+ }
1430
+ sorted.splice(index, 0, factory2);
1431
+ }
1432
+ factories.filter(isFactory).forEach(addFactory);
1433
+ factories.filter((factory2) => !isFactory(factory2)).forEach(addFactory);
1434
+ return sorted;
1435
+ }
1436
+ function create(factories, scope = {}) {
1437
+ sortFactories(factories).forEach((factory2) => {
1438
+ if (isFactory(factory2)) {
1439
+ factory2(scope);
1440
+ }
1441
+ });
1442
+ return scope;
1443
+ }
1444
+ function isFactory(obj) {
1445
+ const f = obj;
1446
+ return typeof obj === "function" && typeof f.fn === "string" && Array.isArray(f.dependencies);
1447
+ }
1448
+ function assertDependencies(name2, dependencies2, scope) {
1449
+ const allDefined = dependencies2.filter((dependency) => !isOptionalDependency(dependency)).every((dependency) => scope[dependency] !== void 0);
1450
+ if (!allDefined) {
1451
+ const missingDependencies = dependencies2.filter(
1452
+ (dependency) => scope[dependency] === void 0
1453
+ );
1454
+ throw new MathjsError(
1455
+ `Cannot create function "${name2}", some dependencies are missing: ${missingDependencies.map((d) => `"${d}"`).join(", ")}.`
1456
+ );
1457
+ }
1458
+ }
1459
+ function isOptionalDependency(dependency) {
1460
+ return dependency[0] === "?";
1461
+ }
1462
+ function stripOptionalNotation(dependency) {
1463
+ return dependency && dependency[0] === "?" ? dependency.slice(1) : dependency;
1464
+ }
1465
+
1345
1466
  // src/types/fraction.ts
1346
1467
  function isFraction2(value) {
1347
1468
  return value instanceof Fraction;
@@ -3199,56 +3320,6 @@ var DimensionMismatchError = class extends Error {
3199
3320
  }
3200
3321
  };
3201
3322
 
3202
- // src/error/MathjsError.ts
3203
- var MathjsError = class _MathjsError extends Error {
3204
- isMathjsError = true;
3205
- /**
3206
- * Create a MathjsError
3207
- * @param message Error message
3208
- */
3209
- constructor(message) {
3210
- super(message);
3211
- this.name = "MathjsError";
3212
- const errorCtor = Error;
3213
- if (errorCtor.captureStackTrace) {
3214
- errorCtor.captureStackTrace(this, _MathjsError);
3215
- }
3216
- }
3217
- };
3218
-
3219
- // src/factory.ts
3220
- function factory(name2, dependencies2, create, meta) {
3221
- function assertAndCreate(scope) {
3222
- const deps = pickShallow(scope, dependencies2.map(stripOptionalNotation));
3223
- assertDependencies(name2, dependencies2, scope);
3224
- return create(deps);
3225
- }
3226
- assertAndCreate.isFactory = true;
3227
- assertAndCreate.fn = name2;
3228
- assertAndCreate.dependencies = dependencies2.slice().sort();
3229
- if (meta) {
3230
- assertAndCreate.meta = meta;
3231
- }
3232
- return assertAndCreate;
3233
- }
3234
- function assertDependencies(name2, dependencies2, scope) {
3235
- const allDefined = dependencies2.filter((dependency) => !isOptionalDependency(dependency)).every((dependency) => scope[dependency] !== void 0);
3236
- if (!allDefined) {
3237
- const missingDependencies = dependencies2.filter(
3238
- (dependency) => scope[dependency] === void 0
3239
- );
3240
- throw new MathjsError(
3241
- `Cannot create function "${name2}", some dependencies are missing: ${missingDependencies.map((d) => `"${d}"`).join(", ")}.`
3242
- );
3243
- }
3244
- }
3245
- function isOptionalDependency(dependency) {
3246
- return dependency[0] === "?";
3247
- }
3248
- function stripOptionalNotation(dependency) {
3249
- return dependency && dependency[0] === "?" ? dependency.slice(1) : dependency;
3250
- }
3251
-
3252
3323
  // src/types/unit/Unit.ts
3253
3324
  function normalizeDegreeSymbols(s) {
3254
3325
  return s.replace(/°C/g, "degC").replace(/°F/g, "degF").replace(/°/g, "deg");
@@ -6335,7 +6406,14 @@ export {
6335
6406
  set,
6336
6407
  pick,
6337
6408
  pickShallow,
6409
+ MathjsError,
6338
6410
  factory,
6411
+ sortFactories,
6412
+ create,
6413
+ isFactory,
6414
+ assertDependencies,
6415
+ isOptionalDependency,
6416
+ stripOptionalNotation,
6339
6417
  isFraction2,
6340
6418
  Fraction,
6341
6419
  FRACTION_ZERO,
@@ -8,6 +8,17 @@ interface FactoryFunction<TResult = unknown> {
8
8
  dependencies: string[];
9
9
  meta?: FactoryMeta;
10
10
  }
11
+ /**
12
+ * Type for legacy factory objects (old-style factories)
13
+ */
14
+ interface LegacyFactory {
15
+ type?: string;
16
+ name: string;
17
+ factory: (...args: unknown[]) => unknown;
18
+ math?: boolean;
19
+ dependencies?: string[];
20
+ meta?: FactoryMeta;
21
+ }
11
22
  /**
12
23
  * Meta information that can be attached to a factory
13
24
  */
@@ -25,6 +36,79 @@ interface FactoryMeta {
25
36
  */
26
37
  [key: string]: unknown;
27
38
  }
39
+ /**
40
+ * Type for dependency names, which can be optional (prefixed with '?')
41
+ */
42
+ type DependencyName = string;
43
+ /**
44
+ * Type for the create callback function
45
+ */
46
+ type CreateFunction<TDeps extends Record<string, unknown>, TResult> = (dependencies: TDeps) => TResult;
47
+ /**
48
+ * Create a factory function, which can be used to inject dependencies.
49
+ *
50
+ * The created functions are memoized, a consecutive call of the factory
51
+ * with the exact same inputs will return the same function instance.
52
+ * The memoized cache is exposed on `factory.cache` and can be cleared
53
+ * if needed.
54
+ *
55
+ * Example:
56
+ *
57
+ * const name = 'log'
58
+ * const dependencies = ['config', 'typed', 'divideScalar', 'Complex']
59
+ *
60
+ * export const createLog = factory(name, dependencies, ({ typed, config, divideScalar, Complex }) => {
61
+ * // ... create the function log here and return it
62
+ * }
63
+ *
64
+ * @param name Name of the function to be created
65
+ * @param dependencies The names of all required dependencies
66
+ * @param create Callback function called with an object with all dependencies
67
+ * @param meta Optional object with meta information that will be attached
68
+ * to the created factory function as property `meta`. For explanation
69
+ * of what meta properties can be specified and what they mean, see
70
+ * docs/core/extension.md.
71
+ * @returns The factory function
72
+ */
73
+ declare function factory<TDeps extends Record<string, unknown> = Record<string, unknown>, TResult = unknown>(name: string, dependencies: DependencyName[], create: CreateFunction<TDeps, TResult>, meta?: FactoryMeta): FactoryFunction<TResult>;
74
+ /**
75
+ * Sort all factories such that when loading in order, the dependencies are resolved.
76
+ *
77
+ * @param factories Array of factory functions or legacy factories
78
+ * @returns Returns a new array with the sorted factories
79
+ */
80
+ declare function sortFactories(factories: Array<FactoryFunction | LegacyFactory>): Array<FactoryFunction | LegacyFactory>;
81
+ declare function create(factories: Array<FactoryFunction | LegacyFactory>, scope?: Record<string, unknown>): Record<string, unknown>;
82
+ /**
83
+ * Test whether an object is a factory. This is the case when it has
84
+ * properties name, dependencies, and a function create.
85
+ * @param obj Any value to test
86
+ * @returns true if obj is a factory function
87
+ */
88
+ declare function isFactory(obj: unknown): obj is FactoryFunction;
89
+ /**
90
+ * Assert that all dependencies of a list with dependencies are available in the provided scope.
91
+ *
92
+ * Will throw an exception when there are dependencies missing.
93
+ *
94
+ * @param name Name for the function to be created. Used to generate a useful error message
95
+ * @param dependencies Array of dependency names
96
+ * @param scope Object containing the available dependencies
97
+ * @throws Error if required dependencies are missing
98
+ */
99
+ declare function assertDependencies(name: string, dependencies: DependencyName[], scope: Record<string, unknown>): void;
100
+ /**
101
+ * Check if a dependency is optional (starts with '?')
102
+ * @param dependency The dependency name to check
103
+ * @returns true if the dependency is optional
104
+ */
105
+ declare function isOptionalDependency(dependency: DependencyName): boolean;
106
+ /**
107
+ * Remove the optional notation '?' from a dependency name
108
+ * @param dependency The dependency name
109
+ * @returns The dependency name without optional notation
110
+ */
111
+ declare function stripOptionalNotation(dependency: DependencyName): string;
28
112
 
29
113
  interface BigNumber {
30
114
  isBigNumber: boolean;
@@ -362,4 +446,4 @@ interface UnitConstructor {
362
446
  /** The core `Unit` class, pre-wired to core's numeric primitives. */
363
447
  declare const Unit: UnitConstructor;
364
448
 
365
- export { isMatrix as $, type UnitJSON as A, type BaseUnitDef as B, type Complex as C, type DenseMatrix as D, type UnitSystem as E, type FactoryFunction as F, type UnitSystemEntry as G, isArray as H, type Index as I, isBigInt as J, isBigNumber as K, isBoolean as L, type Matrix as M, type Numeric as N, isCollection as O, type ParseOptions as P, isComplex as Q, type Range as R, type ScalarBinaryOp as S, type TypeConverters as T, Unit as U, isDate as V, isDenseMatrix as W, isFraction as X, isFunction as Y, isIndex as Z, isMap as _, type UnitInstance as a, isNull as a0, isNumber as a1, isObject as a2, isRange as a3, isRegExp as a4, isSparseMatrix as a5, isString as a6, isUndefined as a7, typeOf as a8, type UnitConstructor as b, type UnitDependencies as c, type BigNumber as d, type BigNumberConstructor as e, type BigNumberValue as f, type ComplexConstructor as g, type ComplexValue as h, isUnit as i, type ConverterFn as j, type CreateUnitDefObject as k, type CreateUnitOptions as l, type Fraction as m, type FractionConstructor as n, type FractionValue as o, type IndexDimension as p, type PrefixDef as q, type PrefixTable as r, type ScalarUnaryOp as s, type SparseMatrix as t, type SubtractScalar as u, type Unit$1 as v, type UnitComponent as w, type UnitConfig as x, type UnitDef as y, type UnitFormatOptions as z };
449
+ 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 };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { F as FactoryFunction$1 } from './index-zKEDY9aj.js';
2
- export { U as Unit, a as UnitInstance, i as isUnitValue } from './index-zKEDY9aj.js';
1
+ import { F as FactoryFunction$1 } from './index-B4LIOfHN.js';
2
+ export { U as Unit, a as UnitInstance, i as isUnitValue } from './index-B4LIOfHN.js';
3
3
  import { TypedInstance, ReferTo, ReferToSelf, TypedFunction, SignatureFunction } from 'typed-function';
4
4
  export { ReferTo, ReferToSelf, SignatureFunction, TypedFunction, TypedInstance, create, default as typed } from 'typed-function';
5
5
 
package/dist/index.js CHANGED
@@ -44,7 +44,7 @@ import {
44
44
  round,
45
45
  sign,
46
46
  subtractScalar
47
- } from "./chunk-2WW76EEF.js";
47
+ } from "./chunk-PALIR4V6.js";
48
48
 
49
49
  // src/types/matrix/Range.ts
50
50
  var name = "Range";