source: node_modules/recharts/es6/util/ReduceCSSCalc.js@ ba17441

Last change on this file since ba17441 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 5.4 KB
Line 
1var _DecimalCSS;
2function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
3function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
4function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
5import { isNan } from './DataUtils';
6var MULTIPLY_OR_DIVIDE_REGEX = /(-?\d+(?:\.\d+)?[a-zA-Z%]*)([*/])(-?\d+(?:\.\d+)?[a-zA-Z%]*)/;
7var ADD_OR_SUBTRACT_REGEX = /(-?\d+(?:\.\d+)?[a-zA-Z%]*)([+-])(-?\d+(?:\.\d+)?[a-zA-Z%]*)/;
8var CSS_LENGTH_UNIT_REGEX = /^px|cm|vh|vw|em|rem|%|mm|in|pt|pc|ex|ch|vmin|vmax|Q$/;
9var NUM_SPLIT_REGEX = /(-?\d+(?:\.\d+)?)([a-zA-Z%]+)?/;
10var CONVERSION_RATES = {
11 cm: 96 / 2.54,
12 mm: 96 / 25.4,
13 pt: 96 / 72,
14 pc: 96 / 6,
15 in: 96,
16 Q: 96 / (2.54 * 40),
17 px: 1
18};
19var FIXED_CSS_LENGTH_UNITS = ['cm', 'mm', 'pt', 'pc', 'in', 'Q', 'px'];
20function isSupportedUnit(unit) {
21 return FIXED_CSS_LENGTH_UNITS.includes(unit);
22}
23var STR_NAN = 'NaN';
24function convertToPx(value, unit) {
25 return value * CONVERSION_RATES[unit];
26}
27class DecimalCSS {
28 static parse(str) {
29 var _NUM_SPLIT_REGEX$exec;
30 var [, numStr, unit] = (_NUM_SPLIT_REGEX$exec = NUM_SPLIT_REGEX.exec(str)) !== null && _NUM_SPLIT_REGEX$exec !== void 0 ? _NUM_SPLIT_REGEX$exec : [];
31 if (numStr == null) {
32 return DecimalCSS.NaN;
33 }
34 return new DecimalCSS(parseFloat(numStr), unit !== null && unit !== void 0 ? unit : '');
35 }
36 constructor(num, unit) {
37 this.num = num;
38 this.unit = unit;
39 this.num = num;
40 this.unit = unit;
41 if (isNan(num)) {
42 this.unit = '';
43 }
44 if (unit !== '' && !CSS_LENGTH_UNIT_REGEX.test(unit)) {
45 this.num = NaN;
46 this.unit = '';
47 }
48 if (isSupportedUnit(unit)) {
49 this.num = convertToPx(num, unit);
50 this.unit = 'px';
51 }
52 }
53 add(other) {
54 if (this.unit !== other.unit) {
55 return new DecimalCSS(NaN, '');
56 }
57 return new DecimalCSS(this.num + other.num, this.unit);
58 }
59 subtract(other) {
60 if (this.unit !== other.unit) {
61 return new DecimalCSS(NaN, '');
62 }
63 return new DecimalCSS(this.num - other.num, this.unit);
64 }
65 multiply(other) {
66 if (this.unit !== '' && other.unit !== '' && this.unit !== other.unit) {
67 return new DecimalCSS(NaN, '');
68 }
69 return new DecimalCSS(this.num * other.num, this.unit || other.unit);
70 }
71 divide(other) {
72 if (this.unit !== '' && other.unit !== '' && this.unit !== other.unit) {
73 return new DecimalCSS(NaN, '');
74 }
75 return new DecimalCSS(this.num / other.num, this.unit || other.unit);
76 }
77 toString() {
78 return "".concat(this.num).concat(this.unit);
79 }
80 isNaN() {
81 return isNan(this.num);
82 }
83}
84_DecimalCSS = DecimalCSS;
85_defineProperty(DecimalCSS, "NaN", new _DecimalCSS(NaN, ''));
86function calculateArithmetic(expr) {
87 if (expr == null || expr.includes(STR_NAN)) {
88 return STR_NAN;
89 }
90 var newExpr = expr;
91 while (newExpr.includes('*') || newExpr.includes('/')) {
92 var _MULTIPLY_OR_DIVIDE_R;
93 var [, leftOperand, operator, rightOperand] = (_MULTIPLY_OR_DIVIDE_R = MULTIPLY_OR_DIVIDE_REGEX.exec(newExpr)) !== null && _MULTIPLY_OR_DIVIDE_R !== void 0 ? _MULTIPLY_OR_DIVIDE_R : [];
94 var lTs = DecimalCSS.parse(leftOperand !== null && leftOperand !== void 0 ? leftOperand : '');
95 var rTs = DecimalCSS.parse(rightOperand !== null && rightOperand !== void 0 ? rightOperand : '');
96 var result = operator === '*' ? lTs.multiply(rTs) : lTs.divide(rTs);
97 if (result.isNaN()) {
98 return STR_NAN;
99 }
100 newExpr = newExpr.replace(MULTIPLY_OR_DIVIDE_REGEX, result.toString());
101 }
102 while (newExpr.includes('+') || /.-\d+(?:\.\d+)?/.test(newExpr)) {
103 var _ADD_OR_SUBTRACT_REGE;
104 var [, _leftOperand, _operator, _rightOperand] = (_ADD_OR_SUBTRACT_REGE = ADD_OR_SUBTRACT_REGEX.exec(newExpr)) !== null && _ADD_OR_SUBTRACT_REGE !== void 0 ? _ADD_OR_SUBTRACT_REGE : [];
105 var _lTs = DecimalCSS.parse(_leftOperand !== null && _leftOperand !== void 0 ? _leftOperand : '');
106 var _rTs = DecimalCSS.parse(_rightOperand !== null && _rightOperand !== void 0 ? _rightOperand : '');
107 var _result = _operator === '+' ? _lTs.add(_rTs) : _lTs.subtract(_rTs);
108 if (_result.isNaN()) {
109 return STR_NAN;
110 }
111 newExpr = newExpr.replace(ADD_OR_SUBTRACT_REGEX, _result.toString());
112 }
113 return newExpr;
114}
115var PARENTHESES_REGEX = /\(([^()]*)\)/;
116function calculateParentheses(expr) {
117 var newExpr = expr;
118 var match;
119 // eslint-disable-next-line no-cond-assign
120 while ((match = PARENTHESES_REGEX.exec(newExpr)) != null) {
121 var [, parentheticalExpression] = match;
122 newExpr = newExpr.replace(PARENTHESES_REGEX, calculateArithmetic(parentheticalExpression));
123 }
124 return newExpr;
125}
126function evaluateExpression(expression) {
127 var newExpr = expression.replace(/\s+/g, '');
128 newExpr = calculateParentheses(newExpr);
129 newExpr = calculateArithmetic(newExpr);
130 return newExpr;
131}
132export function safeEvaluateExpression(expression) {
133 try {
134 return evaluateExpression(expression);
135 } catch (_unused) {
136 return STR_NAN;
137 }
138}
139export function reduceCSSCalc(expression) {
140 var result = safeEvaluateExpression(expression.slice(5, -1));
141 if (result === STR_NAN) {
142 return '';
143 }
144 return result;
145}
Note: See TracBrowser for help on using the repository browser.