UNPKG

3.32 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.createNumber = void 0;
7
8var _factory = require("../utils/factory.js");
9
10var _collection = require("../utils/collection.js");
11
12var name = 'number';
13var dependencies = ['typed'];
14var createNumber = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
15 var typed = _ref.typed;
16
17 /**
18 * Create a number or convert a string, boolean, or unit to a number.
19 * When value is a matrix, all elements will be converted to number.
20 *
21 * Syntax:
22 *
23 * math.number(value)
24 * math.number(unit, valuelessUnit)
25 *
26 * Examples:
27 *
28 * math.number(2) // returns number 2
29 * math.number('7.2') // returns number 7.2
30 * math.number(true) // returns number 1
31 * math.number([true, false, true, true]) // returns [1, 0, 1, 1]
32 * math.number(math.unit('52cm'), 'm') // returns 0.52
33 *
34 * See also:
35 *
36 * bignumber, boolean, complex, index, matrix, string, unit
37 *
38 * @param {string | number | BigNumber | Fraction | boolean | Array | Matrix | Unit | null} [value] Value to be converted
39 * @param {Unit | string} [valuelessUnit] A valueless unit, used to convert a unit to a number
40 * @return {number | Array | Matrix} The created number
41 */
42 var number = typed('number', {
43 '': function _() {
44 return 0;
45 },
46 number: function number(x) {
47 return x;
48 },
49 string: function string(x) {
50 if (x === 'NaN') return NaN;
51 var size = 0;
52 var boxMatch = x.match(/(0[box][0-9a-fA-F]*)i([0-9]*)/);
53
54 if (boxMatch) {
55 // x includes a size suffix like 0xffffi32, so we extract
56 // the suffix and remove it from x
57 size = Number(boxMatch[2]);
58 x = boxMatch[1];
59 }
60
61 var num = Number(x);
62
63 if (isNaN(num)) {
64 throw new SyntaxError('String "' + x + '" is no valid number');
65 }
66
67 if (boxMatch) {
68 // x is a signed bin, oct, or hex literal
69 // num is the value of string x if x is interpreted as unsigned
70 if (num > Math.pow(2, size) - 1) {
71 // literal is too large for size suffix
72 throw new SyntaxError("String \"".concat(x, "\" is out of range"));
73 } // check if the bit at index size - 1 is set and if so do the twos complement
74
75
76 if (num >= Math.pow(2, size - 1)) {
77 num = num - Math.pow(2, size);
78 }
79 }
80
81 return num;
82 },
83 BigNumber: function BigNumber(x) {
84 return x.toNumber();
85 },
86 Fraction: function Fraction(x) {
87 return x.valueOf();
88 },
89 Unit: function Unit(x) {
90 throw new Error('Second argument with valueless unit expected');
91 },
92 "null": function _null(x) {
93 return 0;
94 },
95 'Unit, string | Unit': function UnitStringUnit(unit, valuelessUnit) {
96 return unit.toNumber(valuelessUnit);
97 },
98 'Array | Matrix': function ArrayMatrix(x) {
99 return (0, _collection.deepMap)(x, this);
100 }
101 }); // reviver function to parse a JSON object like:
102 //
103 // {"mathjs":"number","value":"2.3"}
104 //
105 // into a number 2.3
106
107 number.fromJSON = function (json) {
108 return parseFloat(json.value);
109 };
110
111 return number;
112});
113exports.createNumber = createNumber;
\No newline at end of file