mathjs 14.5.2 → 14.6.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.
Files changed (34) hide show
  1. package/HISTORY.md +19 -0
  2. package/lib/browser/math.js +1 -1
  3. package/lib/browser/math.js.LICENSE.txt +2 -2
  4. package/lib/browser/math.js.map +1 -1
  5. package/lib/cjs/entry/dependenciesAny/dependenciesToBest.generated.js +17 -0
  6. package/lib/cjs/entry/dependenciesAny/dependenciesUnitClass.generated.js +2 -0
  7. package/lib/cjs/entry/dependenciesAny.generated.js +7 -0
  8. package/lib/cjs/entry/impureFunctionsAny.generated.js +1 -0
  9. package/lib/cjs/entry/pureFunctionsAny.generated.js +7 -3
  10. package/lib/cjs/expression/embeddedDocs/embeddedDocs.js +2 -0
  11. package/lib/cjs/expression/embeddedDocs/function/units/toBest.js +14 -0
  12. package/lib/cjs/expression/parse.js +1 -1
  13. package/lib/cjs/factoriesAny.js +7 -0
  14. package/lib/cjs/function/arithmetic/sign.js +1 -1
  15. package/lib/cjs/function/unit/toBest.js +53 -0
  16. package/lib/cjs/header.js +2 -2
  17. package/lib/cjs/type/unit/Unit.js +106 -19
  18. package/lib/cjs/version.js +1 -1
  19. package/lib/esm/entry/dependenciesAny/dependenciesToBest.generated.js +10 -0
  20. package/lib/esm/entry/dependenciesAny/dependenciesUnitClass.generated.js +2 -0
  21. package/lib/esm/entry/dependenciesAny.generated.js +1 -0
  22. package/lib/esm/entry/impureFunctionsAny.generated.js +2 -1
  23. package/lib/esm/entry/pureFunctionsAny.generated.js +6 -2
  24. package/lib/esm/expression/embeddedDocs/embeddedDocs.js +2 -0
  25. package/lib/esm/expression/embeddedDocs/function/units/toBest.js +8 -0
  26. package/lib/esm/expression/parse.js +1 -1
  27. package/lib/esm/factoriesAny.js +1 -0
  28. package/lib/esm/function/arithmetic/sign.js +1 -1
  29. package/lib/esm/function/unit/toBest.js +47 -0
  30. package/lib/esm/type/unit/Unit.js +106 -19
  31. package/lib/esm/version.js +1 -1
  32. package/package.json +16 -16
  33. package/types/EXPLANATION.md +1 -1
  34. package/types/index.d.ts +69 -23
@@ -0,0 +1,47 @@
1
+ import { factory } from '../../utils/factory.js';
2
+ var name = 'toBest';
3
+ var dependencies = ['typed'];
4
+ export var createToBest = /* #__PURE__ */factory(name, dependencies, _ref => {
5
+ var {
6
+ typed
7
+ } = _ref;
8
+ /**
9
+ * Converts a unit to the most appropriate display unit.
10
+ * When no preferred units are provided, the function automatically find the best prefix.
11
+ * When preferred units are provided, it converts to
12
+ * the unit that gives a value closest to 1.
13
+ *
14
+ * Syntax:
15
+ *
16
+ * math.toBest(unit)
17
+ * math.toBest(unit, unitList)
18
+ * math.toBest(unit, unitList, options)
19
+ *
20
+ * Where:
21
+ * - `unitList` is an optional array of preferred target units as string or Unit.
22
+ * - `options` is an optional object with options, formed as follows:
23
+ * - `offset`: number | BigNumber
24
+ *
25
+ * Examples:
26
+ *
27
+ * math.unit(0.05, 'm').toBest(['cm', 'mm']) // returns Unit 5 cm
28
+ * math.unit(2 / 3, 'cm').toBest() // returns Unit 0.6666666666666666 cm
29
+ * math.unit(10, 'm').toBest(['mm', 'km'], { offset: 1.5 }) // returns Unit 10000 mm
30
+ *
31
+ * See also:
32
+ *
33
+ * unit, to, format
34
+ *
35
+ * @param {Unit} x The unit to be converted
36
+ * @param {Array<string>} [unitList=[]] Optional array of preferred target units
37
+ * @param {Object} [options] Optional options object
38
+ * @return {Unit} Value converted to the best matching unit
39
+ */
40
+ return typed(name, {
41
+ Unit: x => x.toBest(),
42
+ 'Unit, string': (x, unitList) => x.toBest(unitList.split(',')),
43
+ 'Unit, string, Object': (x, unitList, options) => x.toBest(unitList.split(','), options),
44
+ 'Unit, Array': (x, unitList) => x.toBest(unitList),
45
+ 'Unit, Array, Object': (x, unitList, options) => x.toBest(unitList, options)
46
+ });
47
+ });
@@ -9,7 +9,7 @@ import { endsWith } from '../../utils/string.js';
9
9
  import { clone, hasOwnProperty } from '../../utils/object.js';
10
10
  import { createBigNumberPi as createPi } from '../../utils/bignumber/constants.js';
11
11
  var name = 'Unit';
12
- var dependencies = ['?on', 'config', 'addScalar', 'subtractScalar', 'multiplyScalar', 'divideScalar', 'pow', 'abs', 'fix', 'round', 'equal', 'isNumeric', 'format', 'number', 'Complex', 'BigNumber', 'Fraction'];
12
+ var dependencies = ['?on', 'config', 'addScalar', 'subtractScalar', 'multiplyScalar', 'divideScalar', 'pow', 'abs', 'fix', 'round', 'equal', 'isNumeric', 'format', 'toBest', 'number', 'Complex', 'BigNumber', 'Fraction'];
13
13
  export var createUnitClass = /* #__PURE__ */factory(name, dependencies, _ref => {
14
14
  var {
15
15
  on,
@@ -25,6 +25,7 @@ export var createUnitClass = /* #__PURE__ */factory(name, dependencies, _ref =>
25
25
  equal,
26
26
  isNumeric,
27
27
  format,
28
+ toBest,
28
29
  number: _number,
29
30
  Complex,
30
31
  BigNumber: _BigNumber,
@@ -1041,6 +1042,54 @@ export var createUnitClass = /* #__PURE__ */factory(name, dependencies, _ref =>
1041
1042
  return str;
1042
1043
  };
1043
1044
 
1045
+ /**
1046
+ * Get a unit, with optional formatting options.
1047
+ * @memberof Unit
1048
+ * @param {string[] | Unit[]} [units] Array of units strings or valueLess Unit objects in wich choose the best one
1049
+ * @param {Object} [options] Options for parsing the unit. See parseUnit for details.
1050
+ *
1051
+ * @return {Unit} Returns a new Unit with the given value and unit.
1052
+ */
1053
+ Unit.prototype.toBest = function () {
1054
+ var unitList = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
1055
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1056
+ if (unitList && !Array.isArray(unitList)) {
1057
+ throw new Error('Invalid unit type. Expected string or Unit.');
1058
+ }
1059
+ var startPrefixes = this.units[0].unit.prefixes;
1060
+ if (unitList && unitList.length > 0) {
1061
+ var unitObjects = unitList.map(u => {
1062
+ var unit = null;
1063
+ if (typeof u === 'string') {
1064
+ unit = Unit.parse(u);
1065
+ if (!unit) {
1066
+ throw new Error('Invalid unit type. Expected compatible string or Unit.');
1067
+ }
1068
+ } else if (!isUnit(u)) {
1069
+ throw new Error('Invalid unit type. Expected compatible string or Unit.');
1070
+ }
1071
+ if (unit === null) {
1072
+ unit = u.clone();
1073
+ }
1074
+ try {
1075
+ this.to(unit.formatUnits());
1076
+ return unit;
1077
+ } catch (e) {
1078
+ throw new Error('Invalid unit type. Expected compatible string or Unit.');
1079
+ }
1080
+ });
1081
+ var prefixes = unitObjects.map(el => el.units[0].prefix);
1082
+ this.units[0].unit.prefixes = prefixes.reduce((acc, prefix) => {
1083
+ acc[prefix.name] = prefix;
1084
+ return acc;
1085
+ }, {});
1086
+ this.units[0].prefix = prefixes[0];
1087
+ }
1088
+ var result = formatBest(this, options).simp;
1089
+ this.units[0].unit.prefixes = startPrefixes;
1090
+ result.fixPrefix = true;
1091
+ return result;
1092
+ };
1044
1093
  /**
1045
1094
  * Get a string representation of the Unit, with optional formatting options.
1046
1095
  * @memberof Unit
@@ -1051,11 +1100,54 @@ export var createUnitClass = /* #__PURE__ */factory(name, dependencies, _ref =>
1051
1100
  * @return {string}
1052
1101
  */
1053
1102
  Unit.prototype.format = function (options) {
1103
+ var {
1104
+ simp,
1105
+ valueStr,
1106
+ unitStr
1107
+ } = formatBest(this, options);
1108
+ var str = valueStr;
1109
+ if (simp.value && isComplex(simp.value)) {
1110
+ str = '(' + str + ')'; // Surround complex values with ( ) to enable better parsing
1111
+ }
1112
+ if (unitStr.length > 0 && str.length > 0) {
1113
+ str += ' ';
1114
+ }
1115
+ str += unitStr;
1116
+ return str;
1117
+ };
1118
+
1119
+ /**
1120
+ * Helper function to normalize a unit for conversion and formatting
1121
+ * @param {Unit} unit The unit to be normalized
1122
+ * @return {Object} Object with normalized unit and value
1123
+ * @private
1124
+ */
1125
+ function formatBest(unit) {
1126
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
1054
1127
  // Simplfy the unit list, unless it is valueless or was created directly in the
1055
1128
  // constructor or as the result of to or toSI
1056
- var simp = this.skipAutomaticSimplification || this.value === null ? this.clone() : this.simplify();
1129
+ var simp = unit.skipAutomaticSimplification || unit.value === null ? unit.clone() : unit.simplify();
1057
1130
 
1058
1131
  // Apply some custom logic for handling VA and VAR. The goal is to express the value of the unit as a real value, if possible. Otherwise, use a real-valued unit instead of a complex-valued one.
1132
+ handleVAandVARUnits(simp);
1133
+ // Now apply the best prefix
1134
+ // Units must have only one unit and not have the fixPrefix flag set
1135
+ applyBestPrefixIfNeeded(simp, options.offset);
1136
+ var value = simp._denormalize(simp.value);
1137
+ var valueStr = simp.value !== null ? format(value, options || {}) : '';
1138
+ var unitStr = simp.formatUnits();
1139
+ return {
1140
+ simp,
1141
+ valueStr,
1142
+ unitStr
1143
+ };
1144
+ }
1145
+
1146
+ /**
1147
+ * Helper to handle VA and VAR units
1148
+ * @param {Unit} simp The unit to be normalized
1149
+ */
1150
+ function handleVAandVARUnits(simp) {
1059
1151
  var isImaginary = false;
1060
1152
  if (typeof simp.value !== 'undefined' && simp.value !== null && isComplex(simp.value)) {
1061
1153
  // TODO: Make this better, for example, use relative magnitude of re and im rather than absolute
@@ -1072,37 +1164,32 @@ export var createUnitClass = /* #__PURE__ */factory(name, dependencies, _ref =>
1072
1164
  }
1073
1165
  }
1074
1166
  }
1167
+ }
1075
1168
 
1076
- // Now apply the best prefix
1077
- // Units must have only one unit and not have the fixPrefix flag set
1169
+ /**
1170
+ * Helper to apply the best prefix if needed
1171
+ * @param {Unit} simp The unit to be normalized
1172
+ */
1173
+ function applyBestPrefixIfNeeded(simp, offset) {
1078
1174
  if (simp.units.length === 1 && !simp.fixPrefix) {
1079
1175
  // Units must have integer powers, otherwise the prefix will change the
1080
1176
  // outputted value by not-an-integer-power-of-ten
1081
1177
  if (Math.abs(simp.units[0].power - Math.round(simp.units[0].power)) < 1e-14) {
1082
1178
  // Apply the best prefix
1083
- simp.units[0].prefix = simp._bestPrefix();
1179
+ simp.units[0].prefix = simp._bestPrefix(offset);
1084
1180
  }
1085
1181
  }
1086
- var value = simp._denormalize(simp.value);
1087
- var str = simp.value !== null ? format(value, options || {}) : '';
1088
- var unitStr = simp.formatUnits();
1089
- if (simp.value && isComplex(simp.value)) {
1090
- str = '(' + str + ')'; // Surround complex values with ( ) to enable better parsing
1091
- }
1092
- if (unitStr.length > 0 && str.length > 0) {
1093
- str += ' ';
1094
- }
1095
- str += unitStr;
1096
- return str;
1097
- };
1182
+ }
1098
1183
 
1099
1184
  /**
1100
1185
  * Calculate the best prefix using current value.
1101
1186
  * @memberof Unit
1102
1187
  * @returns {Object} prefix
1188
+ * @param {number} [offset] Optional offset for the best prefix calculation (default 1.2)
1103
1189
  * @private
1104
1190
  */
1105
1191
  Unit.prototype._bestPrefix = function () {
1192
+ var offset = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1.2;
1106
1193
  if (this.units.length !== 1) {
1107
1194
  throw new Error('Can only compute the best prefix for single units with integer powers, like kg, s^2, N^-1, and so forth!');
1108
1195
  }
@@ -1125,7 +1212,7 @@ export var createUnitClass = /* #__PURE__ */factory(name, dependencies, _ref =>
1125
1212
  return bestPrefix;
1126
1213
  }
1127
1214
  var power = this.units[0].power;
1128
- var bestDiff = Math.log(absValue / Math.pow(bestPrefix.value * absUnitValue, power)) / Math.LN10 - 1.2;
1215
+ var bestDiff = Math.log(absValue / Math.pow(bestPrefix.value * absUnitValue, power)) / Math.LN10 - offset;
1129
1216
  if (bestDiff > -2.200001 && bestDiff < 1.800001) return bestPrefix; // Allow the original prefix
1130
1217
  bestDiff = Math.abs(bestDiff);
1131
1218
  var prefixes = this.units[0].unit.prefixes;
@@ -1133,7 +1220,7 @@ export var createUnitClass = /* #__PURE__ */factory(name, dependencies, _ref =>
1133
1220
  if (hasOwnProperty(prefixes, p)) {
1134
1221
  var prefix = prefixes[p];
1135
1222
  if (prefix.scientific) {
1136
- var diff = Math.abs(Math.log(absValue / Math.pow(prefix.value * absUnitValue, power)) / Math.LN10 - 1.2);
1223
+ var diff = Math.abs(Math.log(absValue / Math.pow(prefix.value * absUnitValue, power)) / Math.LN10 - offset);
1137
1224
  if (diff < bestDiff || diff === bestDiff && prefix.name.length < bestPrefix.name.length) {
1138
1225
  // choose the prefix with the smallest diff, or if equal, choose the one
1139
1226
  // with the shortest name (can happen with SHORTLONG for example)
@@ -1,3 +1,3 @@
1
- export var version = '14.5.2';
1
+ export var version = '14.6.0';
2
2
  // Note: This file is automatically generated when building math.js.
3
3
  // Changes made in this file will be overwritten.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mathjs",
3
- "version": "14.5.2",
3
+ "version": "14.6.0",
4
4
  "description": "Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with different data types like numbers, big numbers, complex numbers, fractions, units, and matrices.",
5
5
  "author": "Jos de Jong <wjosdejong@gmail.com> (https://github.com/josdejong)",
6
6
  "homepage": "https://mathjs.org",
@@ -36,37 +36,37 @@
36
36
  "typed-function": "^4.2.1"
37
37
  },
38
38
  "devDependencies": {
39
- "@babel/core": "7.27.3",
39
+ "@babel/core": "7.28.0",
40
40
  "@babel/plugin-transform-object-assign": "7.27.1",
41
41
  "@babel/plugin-transform-optional-catch-binding": "7.27.1",
42
- "@babel/plugin-transform-runtime": "7.27.3",
43
- "@babel/preset-env": "7.27.2",
42
+ "@babel/plugin-transform-runtime": "7.28.0",
43
+ "@babel/preset-env": "7.28.0",
44
44
  "@babel/register": "7.27.1",
45
45
  "@types/assert": "1.5.11",
46
46
  "@types/mocha": "10.0.10",
47
- "@typescript-eslint/eslint-plugin": "8.33.0",
48
- "@typescript-eslint/parser": "8.33.0",
47
+ "@typescript-eslint/eslint-plugin": "8.38.0",
48
+ "@typescript-eslint/parser": "8.38.0",
49
49
  "assert": "2.1.0",
50
50
  "babel-loader": "10.0.0",
51
51
  "c8": "10.1.3",
52
52
  "codecov": "3.8.3",
53
- "core-js": "3.42.0",
53
+ "core-js": "3.44.0",
54
54
  "del": "8.0.0",
55
55
  "dtslint": "4.2.1",
56
56
  "eigen": "0.2.2",
57
57
  "eslint": "8.57.1",
58
58
  "eslint-config-prettier": "9.1.0",
59
59
  "eslint-config-standard": "17.1.0",
60
- "eslint-plugin-import": "2.31.0",
60
+ "eslint-plugin-import": "2.32.0",
61
61
  "eslint-plugin-mocha": "10.5.0",
62
62
  "eslint-plugin-n": "16.6.2",
63
- "eslint-plugin-prettier": "5.4.0",
63
+ "eslint-plugin-prettier": "5.5.3",
64
64
  "eslint-plugin-promise": "6.6.0",
65
- "expect-type": "1.2.1",
65
+ "expect-type": "1.2.2",
66
66
  "expr-eval": "2.0.2",
67
67
  "fancy-log": "2.0.0",
68
- "glob": "11.0.2",
69
- "gulp": "5.0.0",
68
+ "glob": "11.0.3",
69
+ "gulp": "5.0.1",
70
70
  "gulp-babel": "8.0.0",
71
71
  "handlebars": "4.7.8",
72
72
  "jsep": "1.4.0",
@@ -78,7 +78,7 @@
78
78
  "karma-webdriver-launcher": "1.0.8",
79
79
  "karma-webpack": "5.0.1",
80
80
  "mkdirp": "3.0.1",
81
- "mocha": "11.5.0",
81
+ "mocha": "11.7.1",
82
82
  "mocha-junit-reporter": "2.2.1",
83
83
  "ndarray": "1.0.19",
84
84
  "ndarray-determinant": "1.0.0",
@@ -86,14 +86,14 @@
86
86
  "ndarray-ops": "1.2.2",
87
87
  "ndarray-pack": "1.2.1",
88
88
  "numericjs": "1.2.6",
89
- "prettier": "3.5.3",
89
+ "prettier": "3.6.2",
90
90
  "process": "0.11.10",
91
- "sinon": "20.0.0",
91
+ "sinon": "21.0.0",
92
92
  "sylvester": "0.0.21",
93
93
  "tinybench": "4.0.1",
94
94
  "ts-node": "10.9.2",
95
95
  "typescript": "5.8.3",
96
- "webpack": "5.99.9",
96
+ "webpack": "5.100.2",
97
97
  "zeros": "1.0.0"
98
98
  },
99
99
  "type": "module",
@@ -20,7 +20,7 @@ Maintaining the TypeScript types is done manually. When adding a function, one h
20
20
  3. Add a static definition inside `export const {...} : MathJsInstance`
21
21
  4. Add a dependencies definition inside `export const {...} : Record<string, FactoryFunctionMap>`
22
22
 
23
- For exampe for the function `add`, we can have the following definitions:
23
+ For example for the function `add`, we can have the following definitions:
24
24
 
25
25
  ```ts
26
26
  // instance
package/types/index.d.ts CHANGED
@@ -28,7 +28,12 @@ export type MatrixFromFunctionCallback<T extends MathScalarType> = (
28
28
  ) => T
29
29
 
30
30
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
31
- export type FactoryFunction<T> = (scope: any) => T
31
+ export type FactoryFunction<T> = (scope: MathScope) => T
32
+
33
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
34
+ export type MathScope<TValue = any> =
35
+ | Record<string, TValue>
36
+ | MapLike<string, TValue>
32
37
 
33
38
  // FactoryFunctionMap can be nested; all nested objects will be flattened
34
39
  export interface FactoryFunctionMap {
@@ -862,8 +867,8 @@ export interface MathJsInstance extends MathJsFactory {
862
867
  * @param unit The unit to be created
863
868
  * @returns The created unit
864
869
  */
865
- unit(value: MathNumericType, unit: string): Unit
866
- unit(value: MathCollection, unit: string): Unit[]
870
+ unit(value: MathNumericType, unit?: string): Unit
871
+ unit(value: MathCollection): Unit[]
867
872
 
868
873
  /*************************************************************************
869
874
  * Expression functions
@@ -891,12 +896,12 @@ export interface MathJsInstance extends MathJsFactory {
891
896
  */
892
897
  evaluate(
893
898
  expr: MathExpression | Matrix,
894
- scope?: object
899
+ scope?: MathScope
895
900
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
896
901
  ): any
897
902
  evaluate(
898
903
  expr: MathExpression[],
899
- scope?: object
904
+ scope?: MathScope
900
905
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
901
906
  ): any[]
902
907
 
@@ -1071,14 +1076,14 @@ export interface MathJsInstance extends MathJsFactory {
1071
1076
  * @param scope Scope to read/write variables
1072
1077
  */
1073
1078
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
1074
- resolve(node: MathNode | string, scope?: Record<string, any>): MathNode
1079
+ resolve(node: MathNode | string, scope?: MathScope): MathNode
1075
1080
  resolve(
1076
1081
  node: (MathNode | string)[],
1077
1082
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
1078
- scope?: Record<string, any>
1083
+ scope?: MathScope
1079
1084
  ): MathNode[]
1080
1085
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
1081
- resolve(node: Matrix, scope?: Record<string, any>): Matrix
1086
+ resolve(node: Matrix, scope?: MathScope): Matrix
1082
1087
 
1083
1088
  /**
1084
1089
  * Calculate the Sparse Matrix LU decomposition with full pivoting.
@@ -1131,9 +1136,9 @@ export interface MathJsInstance extends MathJsFactory {
1131
1136
  * @returns Sum of x and y
1132
1137
  */
1133
1138
  add<T extends MathType>(x: T, y: T): T
1134
- add<T extends MathType>(...values: T[]): T
1139
+ add<T extends MathType>(x: T, y: T, ...values: T[]): T
1135
1140
  add(x: MathType, y: MathType): MathType
1136
- add(...values: MathType[]): MathType
1141
+ add(x: MathType, y: MathType, ...values: MathType[]): MathType
1137
1142
 
1138
1143
  /**
1139
1144
  * Calculate the cubic root of a value.
@@ -1460,9 +1465,8 @@ export interface MathJsInstance extends MathJsFactory {
1460
1465
  multiply<T extends MathArray>(x: T, y: T): MathScalarType
1461
1466
  multiply(x: Unit, y: Unit): Unit
1462
1467
  multiply(x: number, y: number): number
1463
- multiply(x: MathType, y: MathType): MathType
1464
- multiply<T extends MathType>(...values: T[]): T
1465
- multiply(...values: MathType[]): MathType
1468
+ multiply(x: MathType, y: MathType, ...values: MathType[]): MathType
1469
+ multiply<T extends MathType>(x: T, y: T, ...values: T[]): T
1466
1470
 
1467
1471
  /**
1468
1472
  * Calculate the norm of a number, vector or matrix. The second
@@ -3484,6 +3488,18 @@ export interface MathJsInstance extends MathJsFactory {
3484
3488
  */
3485
3489
  to(x: Unit | MathCollection, unit: Unit | string): Unit | MathCollection
3486
3490
 
3491
+ /**
3492
+ * Converts a unit to the most appropriate display unit.
3493
+ * When no preferred units are provided, the function automatically find the best prefix.
3494
+ * When preferred units are provided, it converts to
3495
+ * the unit that gives a value closest to 1.
3496
+ * @param preferredUnits - Optional preferred target units
3497
+ * @param options - Optional options object
3498
+ * @returns Unit with optimized prefix/unit
3499
+ */
3500
+ toBest(): Unit
3501
+ toBest(units: string[] | Unit[], options: object): Unit
3502
+
3487
3503
  /*************************************************************************
3488
3504
  * Utils
3489
3505
  ************************************************************************/
@@ -3830,6 +3846,7 @@ export const {
3830
3846
  formatDependencies,
3831
3847
  printDependencies,
3832
3848
  toDependencies,
3849
+ toBestDependencies,
3833
3850
  isPrimeDependencies,
3834
3851
  numericDependencies,
3835
3852
  divideScalarDependencies,
@@ -4172,7 +4189,9 @@ export interface Unit {
4172
4189
  divide(unit: Unit): Unit | number
4173
4190
  pow(unit: Unit): Unit
4174
4191
  abs(unit: Unit): Unit
4175
- to(unit: string): Unit
4192
+ to(unit: string | Unit): Unit
4193
+ toBest(): Unit
4194
+ toBest(units?: string[] | Unit[], options?: object): Unit
4176
4195
  toNumber(unit?: string): number
4177
4196
  toNumeric(unit?: string): number | Fraction | BigNumber
4178
4197
  toSI(): Unit
@@ -4286,10 +4305,14 @@ export interface Simplify {
4286
4305
  (
4287
4306
  expr: MathNode | string,
4288
4307
  rules: SimplifyRule[],
4289
- scope?: object,
4308
+ scope?: MathScope,
4309
+ options?: SimplifyOptions
4310
+ ): MathNode
4311
+ (
4312
+ expr: MathNode | string,
4313
+ scope: MathScope,
4290
4314
  options?: SimplifyOptions
4291
4315
  ): MathNode
4292
- (expr: MathNode | string, scope: object, options?: SimplifyOptions): MathNode
4293
4316
 
4294
4317
  rules: SimplifyRule[]
4295
4318
  }
@@ -4316,7 +4339,7 @@ export interface ObjectWrappingMap<T extends string | number | symbol, U> {
4316
4339
 
4317
4340
  export interface EvalFunction {
4318
4341
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
4319
- evaluate(scope?: any): any
4342
+ evaluate(scope?: MathScope): any
4320
4343
  }
4321
4344
 
4322
4345
  // ResultSet type and helper
@@ -4354,7 +4377,7 @@ export interface MathNode {
4354
4377
  * node.compile().evaluate(scope). Example:
4355
4378
  */
4356
4379
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
4357
- evaluate(expr?: any): any
4380
+ evaluate(scope?: MathScope): any
4358
4381
  /**
4359
4382
  * Test whether this node equals an other node. Does a deep comparison
4360
4383
  * of the values of both nodes.
@@ -4818,7 +4841,7 @@ export interface MathJsChain<TValue> {
4818
4841
  */
4819
4842
  unit(this: MathJsChain<string>, unit?: string): MathJsChain<Unit>
4820
4843
  unit(this: MathJsChain<MathNumericType>, unit?: string): MathJsChain<Unit>
4821
- unit(this: MathJsChain<MathCollection>, unit?: string): MathJsChain<Unit[]>
4844
+ unit(this: MathJsChain<MathCollection>): MathJsChain<Unit[]>
4822
4845
 
4823
4846
  /*************************************************************************
4824
4847
  * Expression functions
@@ -4837,12 +4860,12 @@ export interface MathJsChain<TValue> {
4837
4860
  */
4838
4861
  evaluate(
4839
4862
  this: MathJsChain<MathExpression | Matrix>,
4840
- scope?: object
4863
+ scope?: MathScope
4841
4864
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
4842
4865
  ): MathJsChain<any>
4843
4866
  evaluate(
4844
4867
  this: MathJsChain<MathExpression[]>,
4845
- scope?: object
4868
+ scope?: MathScope
4846
4869
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
4847
4870
  ): MathJsChain<any[]>
4848
4871
 
@@ -4879,12 +4902,12 @@ export interface MathJsChain<TValue> {
4879
4902
  resolve(
4880
4903
  this: MathJsChain<MathNode>,
4881
4904
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
4882
- scope?: Record<string, any>
4905
+ scope?: MathScope
4883
4906
  ): MathJsChain<MathNode>
4884
4907
  resolve(
4885
4908
  this: MathJsChain<MathNode[]>,
4886
4909
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
4887
- scope?: Record<string, any>
4910
+ scope?: MathScope
4888
4911
  ): MathJsChain<MathNode[]>
4889
4912
 
4890
4913
  /*************************************************************************
@@ -6983,6 +7006,21 @@ export interface MathJsChain<TValue> {
6983
7006
  unit: Unit | string
6984
7007
  ): MathJsChain<Unit | MathCollection>
6985
7008
 
7009
+ /**
7010
+ * Converts a unit to the most appropriate display unit.
7011
+ * When no preferred units are provided, the function automatically find the best prefix.
7012
+ * When preferred units are provided, it converts to
7013
+ * the unit that gives a value closest to 1.
7014
+ * @param preferredUnits - Optional preferred target units
7015
+ * @param options - Optional options object
7016
+ */
7017
+ toBest(this: MathJsChain<Unit>): MathJsChain<Unit>
7018
+ toBest(
7019
+ this: MathJsChain<Unit>,
7020
+ units: string[] | Unit[],
7021
+ options: object
7022
+ ): MathJsChain<Unit>
7023
+
6986
7024
  /*************************************************************************
6987
7025
  * Utils functions
6988
7026
  ************************************************************************/
@@ -7081,6 +7119,13 @@ export interface ImportObject {
7081
7119
  [key: string]: any
7082
7120
  }
7083
7121
 
7122
+ export interface MapLike<TKey = string, TValue = unknown> {
7123
+ get(key: TKey): TValue
7124
+ set(key: TKey, value: TValue): MapLike<TKey, TValue>
7125
+ has(key: TKey): boolean
7126
+ keys(): IterableIterator<TKey> | TKey[]
7127
+ }
7128
+
7084
7129
  export const {
7085
7130
  // config // Don't export config: no config available in the static instance
7086
7131
 
@@ -7365,6 +7410,7 @@ export const {
7365
7410
 
7366
7411
  // unit functions
7367
7412
  to,
7413
+ toBest,
7368
7414
 
7369
7415
  // util functions
7370
7416
  isNumber,