1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.createNumber = void 0;
|
7 | var _factory = require("../utils/factory.js");
|
8 | var _collection = require("../utils/collection.js");
|
9 | var name = 'number';
|
10 | var dependencies = ['typed'];
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function 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 |
|
40 |
|
41 |
|
42 |
|
43 | function 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 | }
|
56 | var createNumber = (0, _factory.factory)(name, dependencies, function (_ref) {
|
57 | var typed = _ref.typed;
|
58 | |
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
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 |
|
100 |
|
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 |
|
110 |
|
111 | if (num > Math.pow(2, size) - 1) {
|
112 |
|
113 | throw new SyntaxError("String \"".concat(x, "\" is out of range"));
|
114 | }
|
115 |
|
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 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | number.fromJSON = function (json) {
|
150 | return parseFloat(json.value);
|
151 | };
|
152 | return number;
|
153 | });
|
154 | exports.createNumber = createNumber; |
\ | No newline at end of file |