UNPKG

3.15 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.createCreateUnit = void 0;
7
8var _factory = require("../../../utils/factory");
9
10var name = 'createUnit';
11var dependencies = ['typed', 'Unit'];
12var createCreateUnit = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
13 var typed = _ref.typed,
14 Unit = _ref.Unit;
15
16 /**
17 * Create a user-defined unit and register it with the Unit type.
18 *
19 * Syntax:
20 *
21 * math.createUnit({
22 * baseUnit1: {
23 * aliases: [string, ...]
24 * prefixes: object
25 * },
26 * unit2: {
27 * definition: string,
28 * aliases: [string, ...]
29 * prefixes: object,
30 * offset: number
31 * },
32 * unit3: string // Shortcut
33 * })
34 *
35 * // Another shortcut:
36 * math.createUnit(string, unit : string, [object])
37 *
38 * Examples:
39 *
40 * math.createUnit('foo')
41 * math.createUnit('knot', {definition: '0.514444444 m/s', aliases: ['knots', 'kt', 'kts']})
42 * math.createUnit('mph', '1 mile/hour')
43 *
44 * @param {string} name The name of the new unit. Must be unique. Example: 'knot'
45 * @param {string, Unit} definition Definition of the unit in terms of existing units. For example, '0.514444444 m / s'.
46 * @param {Object} options (optional) An object containing any of the following properties:
47 * - `prefixes {string}` "none", "short", "long", "binary_short", or "binary_long". The default is "none".
48 * - `aliases {Array}` Array of strings. Example: ['knots', 'kt', 'kts']
49 * - `offset {Numeric}` An offset to apply when converting from the unit. For example, the offset for celsius is 273.15. Default is 0.
50 *
51 * See also:
52 *
53 * unit
54 *
55 * @return {Unit} The new unit
56 */
57 return typed(name, {
58 // General function signature. First parameter is an object where each property is the definition of a new unit. The object keys are the unit names and the values are the definitions. The values can be objects, strings, or Units. If a property is an empty object or an empty string, a new base unit is created. The second parameter is the options.
59 'Object, Object': function ObjectObject(obj, options) {
60 return Unit.createUnit(obj, options);
61 },
62 // Same as above but without the options.
63 Object: function Object(obj) {
64 return Unit.createUnit(obj, {});
65 },
66 // Shortcut method for creating one unit.
67 'string, Unit | string | Object, Object': function stringUnitStringObjectObject(name, def, options) {
68 var obj = {};
69 obj[name] = def;
70 return Unit.createUnit(obj, options);
71 },
72 // Same as above but without the options.
73 'string, Unit | string | Object': function stringUnitStringObject(name, def) {
74 var obj = {};
75 obj[name] = def;
76 return Unit.createUnit(obj, {});
77 },
78 // Without a definition, creates a base unit.
79 string: function string(name) {
80 var obj = {};
81 obj[name] = {};
82 return Unit.createUnit(obj, {});
83 }
84 });
85});
86exports.createCreateUnit = createCreateUnit;
\No newline at end of file