UNPKG

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