UNPKG

4.69 kBJavaScriptView Raw
1import { factory } from '../utils/factory.js';
2import { deepMap } from '../utils/collection.js';
3var name = 'number';
4var dependencies = ['typed'];
5/**
6 * Separates the radix, integer part, and fractional part of a non decimal number string
7 * @param {string} input string to parse
8 * @returns {object} the parts of the string or null if not a valid input
9 */
10
11function getNonDecimalNumberParts(input) {
12 var nonDecimalWithRadixMatch = input.match(/(0[box])([0-9a-fA-F]*)\.([0-9a-fA-F]*)/);
13
14 if (nonDecimalWithRadixMatch) {
15 var radix = {
16 '0b': 2,
17 '0o': 8,
18 '0x': 16
19 }[nonDecimalWithRadixMatch[1]];
20 var integerPart = nonDecimalWithRadixMatch[2];
21 var fractionalPart = nonDecimalWithRadixMatch[3];
22 return {
23 input,
24 radix,
25 integerPart,
26 fractionalPart
27 };
28 } else {
29 return null;
30 }
31}
32/**
33 * Makes a number from a radix, and integer part, and a fractional part
34 * @param {parts} [x] parts of the number string (from getNonDecimalNumberParts)
35 * @returns {number} the number
36 */
37
38
39function makeNumberFromNonDecimalParts(parts) {
40 var n = parseInt(parts.integerPart, parts.radix);
41 var f = 0;
42
43 for (var i = 0; i < parts.fractionalPart.length; i++) {
44 var digitValue = parseInt(parts.fractionalPart[i], parts.radix);
45 f += digitValue / Math.pow(parts.radix, i + 1);
46 }
47
48 var result = n + f;
49
50 if (isNaN(result)) {
51 throw new SyntaxError('String "' + parts.input + '" is no valid number');
52 }
53
54 return result;
55}
56
57export var createNumber = /* #__PURE__ */factory(name, dependencies, _ref => {
58 var {
59 typed
60 } = _ref;
61
62 /**
63 * Create a number or convert a string, boolean, or unit to a number.
64 * When value is a matrix, all elements will be converted to number.
65 *
66 * Syntax:
67 *
68 * math.number(value)
69 * math.number(unit, valuelessUnit)
70 *
71 * Examples:
72 *
73 * math.number(2) // returns number 2
74 * math.number('7.2') // returns number 7.2
75 * math.number(true) // returns number 1
76 * math.number([true, false, true, true]) // returns [1, 0, 1, 1]
77 * math.number(math.unit('52cm'), 'm') // returns 0.52
78 *
79 * See also:
80 *
81 * bignumber, boolean, complex, index, matrix, string, unit
82 *
83 * @param {string | number | BigNumber | Fraction | boolean | Array | Matrix | Unit | null} [value] Value to be converted
84 * @param {Unit | string} [valuelessUnit] A valueless unit, used to convert a unit to a number
85 * @return {number | Array | Matrix} The created number
86 */
87 var number = typed('number', {
88 '': function _() {
89 return 0;
90 },
91 number: function number(x) {
92 return x;
93 },
94 string: function string(x) {
95 if (x === 'NaN') return NaN;
96 var nonDecimalNumberParts = getNonDecimalNumberParts(x);
97
98 if (nonDecimalNumberParts) {
99 return makeNumberFromNonDecimalParts(nonDecimalNumberParts);
100 }
101
102 var size = 0;
103 var wordSizeSuffixMatch = x.match(/(0[box][0-9a-fA-F]*)i([0-9]*)/);
104
105 if (wordSizeSuffixMatch) {
106 // x includes a size suffix like 0xffffi32, so we extract
107 // the suffix and remove it from x
108 size = Number(wordSizeSuffixMatch[2]);
109 x = wordSizeSuffixMatch[1];
110 }
111
112 var num = Number(x);
113
114 if (isNaN(num)) {
115 throw new SyntaxError('String "' + x + '" is no valid number');
116 }
117
118 if (wordSizeSuffixMatch) {
119 // x is a signed bin, oct, or hex literal
120 // num is the value of string x if x is interpreted as unsigned
121 if (num > 2 ** size - 1) {
122 // literal is too large for size suffix
123 throw new SyntaxError("String \"".concat(x, "\" is out of range"));
124 } // check if the bit at index size - 1 is set and if so do the twos complement
125
126
127 if (num >= 2 ** (size - 1)) {
128 num = num - 2 ** size;
129 }
130 }
131
132 return num;
133 },
134 BigNumber: function BigNumber(x) {
135 return x.toNumber();
136 },
137 Fraction: function Fraction(x) {
138 return x.valueOf();
139 },
140 Unit: function Unit(x) {
141 throw new Error('Second argument with valueless unit expected');
142 },
143 null: function _null(x) {
144 return 0;
145 },
146 'Unit, string | Unit': function UnitStringUnit(unit, valuelessUnit) {
147 return unit.toNumber(valuelessUnit);
148 },
149 'Array | Matrix': function ArrayMatrix(x) {
150 return deepMap(x, this);
151 }
152 }); // reviver function to parse a JSON object like:
153 //
154 // {"mathjs":"number","value":"2.3"}
155 //
156 // into a number 2.3
157
158 number.fromJSON = function (json) {
159 return parseFloat(json.value);
160 };
161
162 return number;
163});
\No newline at end of file