ReduceCSSCalc.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
  2. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
  3. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  4. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  5. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
  6. function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
  7. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  8. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  9. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
  10. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
  11. function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
  12. function _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); }
  13. var MULTIPLY_OR_DIVIDE_REGEX = /(-?\d+(?:\.\d+)?[a-zA-Z%]*)([*/])(-?\d+(?:\.\d+)?[a-zA-Z%]*)/;
  14. var ADD_OR_SUBTRACT_REGEX = /(-?\d+(?:\.\d+)?[a-zA-Z%]*)([+-])(-?\d+(?:\.\d+)?[a-zA-Z%]*)/;
  15. var CSS_LENGTH_UNIT_REGEX = /^px|cm|vh|vw|em|rem|%|mm|in|pt|pc|ex|ch|vmin|vmax|Q$/;
  16. var NUM_SPLIT_REGEX = /(-?\d+(?:\.\d+)?)([a-zA-Z%]+)?/;
  17. var 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. };
  26. var FIXED_CSS_LENGTH_UNITS = Object.keys(CONVERSION_RATES);
  27. var STR_NAN = 'NaN';
  28. function convertToPx(value, unit) {
  29. return value * CONVERSION_RATES[unit];
  30. }
  31. var DecimalCSS = /*#__PURE__*/function () {
  32. function DecimalCSS(num, unit) {
  33. _classCallCheck(this, DecimalCSS);
  34. this.num = num;
  35. this.unit = unit;
  36. this.num = num;
  37. this.unit = unit;
  38. if (Number.isNaN(num)) {
  39. this.unit = '';
  40. }
  41. if (unit !== '' && !CSS_LENGTH_UNIT_REGEX.test(unit)) {
  42. this.num = NaN;
  43. this.unit = '';
  44. }
  45. if (FIXED_CSS_LENGTH_UNITS.includes(unit)) {
  46. this.num = convertToPx(num, unit);
  47. this.unit = 'px';
  48. }
  49. }
  50. return _createClass(DecimalCSS, [{
  51. key: "add",
  52. value: function add(other) {
  53. if (this.unit !== other.unit) {
  54. return new DecimalCSS(NaN, '');
  55. }
  56. return new DecimalCSS(this.num + other.num, this.unit);
  57. }
  58. }, {
  59. key: "subtract",
  60. value: function subtract(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. }, {
  67. key: "multiply",
  68. value: function multiply(other) {
  69. if (this.unit !== '' && other.unit !== '' && this.unit !== other.unit) {
  70. return new DecimalCSS(NaN, '');
  71. }
  72. return new DecimalCSS(this.num * other.num, this.unit || other.unit);
  73. }
  74. }, {
  75. key: "divide",
  76. value: function divide(other) {
  77. if (this.unit !== '' && other.unit !== '' && this.unit !== other.unit) {
  78. return new DecimalCSS(NaN, '');
  79. }
  80. return new DecimalCSS(this.num / other.num, this.unit || other.unit);
  81. }
  82. }, {
  83. key: "toString",
  84. value: function toString() {
  85. return "".concat(this.num).concat(this.unit);
  86. }
  87. }, {
  88. key: "isNaN",
  89. value: function isNaN() {
  90. return Number.isNaN(this.num);
  91. }
  92. }], [{
  93. key: "parse",
  94. value: function parse(str) {
  95. var _NUM_SPLIT_REGEX$exec;
  96. var _ref = (_NUM_SPLIT_REGEX$exec = NUM_SPLIT_REGEX.exec(str)) !== null && _NUM_SPLIT_REGEX$exec !== void 0 ? _NUM_SPLIT_REGEX$exec : [],
  97. _ref2 = _slicedToArray(_ref, 3),
  98. numStr = _ref2[1],
  99. unit = _ref2[2];
  100. return new DecimalCSS(parseFloat(numStr), unit !== null && unit !== void 0 ? unit : '');
  101. }
  102. }]);
  103. }();
  104. function calculateArithmetic(expr) {
  105. if (expr.includes(STR_NAN)) {
  106. return STR_NAN;
  107. }
  108. var newExpr = expr;
  109. while (newExpr.includes('*') || newExpr.includes('/')) {
  110. var _MULTIPLY_OR_DIVIDE_R;
  111. var _ref3 = (_MULTIPLY_OR_DIVIDE_R = MULTIPLY_OR_DIVIDE_REGEX.exec(newExpr)) !== null && _MULTIPLY_OR_DIVIDE_R !== void 0 ? _MULTIPLY_OR_DIVIDE_R : [],
  112. _ref4 = _slicedToArray(_ref3, 4),
  113. leftOperand = _ref4[1],
  114. operator = _ref4[2],
  115. rightOperand = _ref4[3];
  116. var lTs = DecimalCSS.parse(leftOperand !== null && leftOperand !== void 0 ? leftOperand : '');
  117. var rTs = DecimalCSS.parse(rightOperand !== null && rightOperand !== void 0 ? rightOperand : '');
  118. var result = operator === '*' ? lTs.multiply(rTs) : lTs.divide(rTs);
  119. if (result.isNaN()) {
  120. return STR_NAN;
  121. }
  122. newExpr = newExpr.replace(MULTIPLY_OR_DIVIDE_REGEX, result.toString());
  123. }
  124. while (newExpr.includes('+') || /.-\d+(?:\.\d+)?/.test(newExpr)) {
  125. var _ADD_OR_SUBTRACT_REGE;
  126. var _ref5 = (_ADD_OR_SUBTRACT_REGE = ADD_OR_SUBTRACT_REGEX.exec(newExpr)) !== null && _ADD_OR_SUBTRACT_REGE !== void 0 ? _ADD_OR_SUBTRACT_REGE : [],
  127. _ref6 = _slicedToArray(_ref5, 4),
  128. _leftOperand = _ref6[1],
  129. _operator = _ref6[2],
  130. _rightOperand = _ref6[3];
  131. var _lTs = DecimalCSS.parse(_leftOperand !== null && _leftOperand !== void 0 ? _leftOperand : '');
  132. var _rTs = DecimalCSS.parse(_rightOperand !== null && _rightOperand !== void 0 ? _rightOperand : '');
  133. var _result = _operator === '+' ? _lTs.add(_rTs) : _lTs.subtract(_rTs);
  134. if (_result.isNaN()) {
  135. return STR_NAN;
  136. }
  137. newExpr = newExpr.replace(ADD_OR_SUBTRACT_REGEX, _result.toString());
  138. }
  139. return newExpr;
  140. }
  141. var PARENTHESES_REGEX = /\(([^()]*)\)/;
  142. function calculateParentheses(expr) {
  143. var newExpr = expr;
  144. while (newExpr.includes('(')) {
  145. var _PARENTHESES_REGEX$ex = PARENTHESES_REGEX.exec(newExpr),
  146. _PARENTHESES_REGEX$ex2 = _slicedToArray(_PARENTHESES_REGEX$ex, 2),
  147. parentheticalExpression = _PARENTHESES_REGEX$ex2[1];
  148. newExpr = newExpr.replace(PARENTHESES_REGEX, calculateArithmetic(parentheticalExpression));
  149. }
  150. return newExpr;
  151. }
  152. function evaluateExpression(expression) {
  153. var newExpr = expression.replace(/\s+/g, '');
  154. newExpr = calculateParentheses(newExpr);
  155. newExpr = calculateArithmetic(newExpr);
  156. return newExpr;
  157. }
  158. export function safeEvaluateExpression(expression) {
  159. try {
  160. return evaluateExpression(expression);
  161. } catch (e) {
  162. /* istanbul ignore next */
  163. return STR_NAN;
  164. }
  165. }
  166. export function reduceCSSCalc(expression) {
  167. var result = safeEvaluateExpression(expression.slice(5, -1));
  168. if (result === STR_NAN) {
  169. // notify the user
  170. return '';
  171. }
  172. return result;
  173. }