source: node_modules/recharts/lib/util/ReduceCSSCalc.js

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

Added visualizations

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