UNPKG

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