UNPKG

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