1 | "use strict";
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports["default"] = math;
|
5 | var _defaultSymbols = _interopRequireDefault(require("./presets/defaultSymbols"));
|
6 | var _errors = _interopRequireDefault(require("../internalHelpers/_errors"));
|
7 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
8 | function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
9 | var unitRegExp = /((?!\w)a|na|hc|mc|dg|me[r]?|xe|ni(?![a-zA-Z])|mm|cp|tp|xp|q(?!s)|hv|xamv|nimv|wv|sm|s(?!\D|$)|ged|darg?|nrut)/g;
|
10 |
|
11 |
|
12 | function mergeSymbolMaps(additionalSymbols) {
|
13 | var symbolMap = {};
|
14 | symbolMap.symbols = additionalSymbols ? _extends({}, _defaultSymbols["default"].symbols, additionalSymbols.symbols) : _extends({}, _defaultSymbols["default"].symbols);
|
15 | return symbolMap;
|
16 | }
|
17 | function exec(operators, values) {
|
18 | var _ref;
|
19 | var op = operators.pop();
|
20 | values.push(op.f.apply(op, (_ref = []).concat.apply(_ref, values.splice(-op.argCount))));
|
21 | return op.precedence;
|
22 | }
|
23 | function calculate(expression, additionalSymbols) {
|
24 | var symbolMap = mergeSymbolMaps(additionalSymbols);
|
25 | var match;
|
26 | var operators = [symbolMap.symbols['('].prefix];
|
27 | var values = [];
|
28 | var pattern = new RegExp(
|
29 | "\\d+(?:\\.\\d+)?|" +
|
30 |
|
31 | Object.keys(symbolMap.symbols).map(function (key) {
|
32 | return symbolMap.symbols[key];
|
33 | })
|
34 |
|
35 |
|
36 | .sort(function (a, b) {
|
37 | return b.symbol.length - a.symbol.length;
|
38 | })
|
39 |
|
40 | .map(function (val) {
|
41 | return val.regSymbol;
|
42 | }).join('|') + "|(\\S)", 'g');
|
43 | pattern.lastIndex = 0;
|
44 |
|
45 | var afterValue = false;
|
46 | do {
|
47 | match = pattern.exec(expression);
|
48 | var _ref2 = match || [')', undefined],
|
49 | token = _ref2[0],
|
50 | bad = _ref2[1];
|
51 | var notNumber = symbolMap.symbols[token];
|
52 | var notNewValue = notNumber && !notNumber.prefix && !notNumber.func;
|
53 | var notAfterValue = !notNumber || !notNumber.postfix && !notNumber.infix;
|
54 |
|
55 |
|
56 | if (bad || (afterValue ? notAfterValue : notNewValue)) {
|
57 | throw new _errors["default"](37, match ? match.index : expression.length, expression);
|
58 | }
|
59 | if (afterValue) {
|
60 |
|
61 | var curr = notNumber.postfix || notNumber.infix;
|
62 | do {
|
63 | var prev = operators[operators.length - 1];
|
64 | if ((curr.precedence - prev.precedence || prev.rightToLeft) > 0) break;
|
65 |
|
66 | } while (exec(operators, values));
|
67 | afterValue = curr.notation === 'postfix';
|
68 | if (curr.symbol !== ')') {
|
69 | operators.push(curr);
|
70 |
|
71 | if (afterValue) exec(operators, values);
|
72 | }
|
73 | } else if (notNumber) {
|
74 |
|
75 | operators.push(notNumber.prefix || notNumber.func);
|
76 | if (notNumber.func) {
|
77 |
|
78 | match = pattern.exec(expression);
|
79 | if (!match || match[0] !== '(') {
|
80 | throw new _errors["default"](38, match ? match.index : expression.length, expression);
|
81 | }
|
82 | }
|
83 | } else {
|
84 |
|
85 | values.push(+token);
|
86 | afterValue = true;
|
87 | }
|
88 | } while (match && operators.length);
|
89 | if (operators.length) {
|
90 | throw new _errors["default"](39, match ? match.index : expression.length, expression);
|
91 | } else if (match) {
|
92 | throw new _errors["default"](40, match ? match.index : expression.length, expression);
|
93 | } else {
|
94 | return values.pop();
|
95 | }
|
96 | }
|
97 | function reverseString(str) {
|
98 | return str.split('').reverse().join('');
|
99 | }
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | function math(formula, additionalSymbols) {
|
131 | var reversedFormula = reverseString(formula);
|
132 | var formulaMatch = reversedFormula.match(unitRegExp);
|
133 |
|
134 |
|
135 | if (formulaMatch && !formulaMatch.every(function (unit) {
|
136 | return unit === formulaMatch[0];
|
137 | })) {
|
138 | throw new _errors["default"](41);
|
139 | }
|
140 | var cleanFormula = reverseString(reversedFormula.replace(unitRegExp, ''));
|
141 | return "" + calculate(cleanFormula, additionalSymbols) + (formulaMatch ? reverseString(formulaMatch[0]) : '');
|
142 | }
|
143 | module.exports = exports.default; |
\ | No newline at end of file |