UNPKG

1.7 kBJavaScriptView Raw
1import { factory } from '../../../utils/factory.js';
2import { deepMap } from '../../../utils/collection.js';
3var name = 'unit';
4var dependencies = ['typed', 'Unit']; // This function is named createUnitFunction to prevent a naming conflict with createUnit
5
6export var createUnitFunction = /* #__PURE__ */factory(name, dependencies, _ref => {
7 var {
8 typed,
9 Unit
10 } = _ref;
11
12 /**
13 * Create a unit. Depending on the passed arguments, the function
14 * will create and return a new math.Unit object.
15 * When a matrix is provided, all elements will be converted to units.
16 *
17 * Syntax:
18 *
19 * math.unit(unit : string)
20 * math.unit(value : number, unit : string)
21 *
22 * Examples:
23 *
24 * const a = math.unit(5, 'cm') // returns Unit 50 mm
25 * const b = math.unit('23 kg') // returns Unit 23 kg
26 * a.to('m') // returns Unit 0.05 m
27 *
28 * See also:
29 *
30 * bignumber, boolean, complex, index, matrix, number, string, createUnit
31 *
32 * @param {* | Array | Matrix} args A number and unit.
33 * @return {Unit | Array | Matrix} The created unit
34 */
35 return typed(name, {
36 Unit: function Unit(x) {
37 return x.clone();
38 },
39 string: function string(x) {
40 if (Unit.isValuelessUnit(x)) {
41 return new Unit(null, x); // a pure unit
42 }
43
44 return Unit.parse(x, {
45 allowNoUnits: true
46 }); // a unit with value, like '5cm'
47 },
48 'number | BigNumber | Fraction | Complex, string': function numberBigNumberFractionComplexString(value, unit) {
49 return new Unit(value, unit);
50 },
51 'Array | Matrix': function ArrayMatrix(x) {
52 return deepMap(x, this);
53 }
54 });
55});
\No newline at end of file