CSSCalculator.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import _typeof from "@babel/runtime/helpers/esm/typeof";
  2. import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
  3. import _createClass from "@babel/runtime/helpers/esm/createClass";
  4. import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
  5. import _inherits from "@babel/runtime/helpers/esm/inherits";
  6. import _createSuper from "@babel/runtime/helpers/esm/createSuper";
  7. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  8. import AbstractCalculator from "./calculator";
  9. var CALC_UNIT = 'CALC_UNIT';
  10. var regexp = new RegExp(CALC_UNIT, 'g');
  11. function unit(value) {
  12. if (typeof value === 'number') {
  13. return "".concat(value).concat(CALC_UNIT);
  14. }
  15. return value;
  16. }
  17. var CSSCalculator = /*#__PURE__*/function (_AbstractCalculator) {
  18. _inherits(CSSCalculator, _AbstractCalculator);
  19. var _super = _createSuper(CSSCalculator);
  20. function CSSCalculator(num, unitlessCssVar) {
  21. var _this;
  22. _classCallCheck(this, CSSCalculator);
  23. _this = _super.call(this);
  24. _defineProperty(_assertThisInitialized(_this), "result", '');
  25. _defineProperty(_assertThisInitialized(_this), "unitlessCssVar", void 0);
  26. _defineProperty(_assertThisInitialized(_this), "lowPriority", void 0);
  27. var numType = _typeof(num);
  28. _this.unitlessCssVar = unitlessCssVar;
  29. if (num instanceof CSSCalculator) {
  30. _this.result = "(".concat(num.result, ")");
  31. } else if (numType === 'number') {
  32. _this.result = unit(num);
  33. } else if (numType === 'string') {
  34. _this.result = num;
  35. }
  36. return _this;
  37. }
  38. _createClass(CSSCalculator, [{
  39. key: "add",
  40. value: function add(num) {
  41. if (num instanceof CSSCalculator) {
  42. this.result = "".concat(this.result, " + ").concat(num.getResult());
  43. } else if (typeof num === 'number' || typeof num === 'string') {
  44. this.result = "".concat(this.result, " + ").concat(unit(num));
  45. }
  46. this.lowPriority = true;
  47. return this;
  48. }
  49. }, {
  50. key: "sub",
  51. value: function sub(num) {
  52. if (num instanceof CSSCalculator) {
  53. this.result = "".concat(this.result, " - ").concat(num.getResult());
  54. } else if (typeof num === 'number' || typeof num === 'string') {
  55. this.result = "".concat(this.result, " - ").concat(unit(num));
  56. }
  57. this.lowPriority = true;
  58. return this;
  59. }
  60. }, {
  61. key: "mul",
  62. value: function mul(num) {
  63. if (this.lowPriority) {
  64. this.result = "(".concat(this.result, ")");
  65. }
  66. if (num instanceof CSSCalculator) {
  67. this.result = "".concat(this.result, " * ").concat(num.getResult(true));
  68. } else if (typeof num === 'number' || typeof num === 'string') {
  69. this.result = "".concat(this.result, " * ").concat(num);
  70. }
  71. this.lowPriority = false;
  72. return this;
  73. }
  74. }, {
  75. key: "div",
  76. value: function div(num) {
  77. if (this.lowPriority) {
  78. this.result = "(".concat(this.result, ")");
  79. }
  80. if (num instanceof CSSCalculator) {
  81. this.result = "".concat(this.result, " / ").concat(num.getResult(true));
  82. } else if (typeof num === 'number' || typeof num === 'string') {
  83. this.result = "".concat(this.result, " / ").concat(num);
  84. }
  85. this.lowPriority = false;
  86. return this;
  87. }
  88. }, {
  89. key: "getResult",
  90. value: function getResult(force) {
  91. return this.lowPriority || force ? "(".concat(this.result, ")") : this.result;
  92. }
  93. }, {
  94. key: "equal",
  95. value: function equal(options) {
  96. var _this2 = this;
  97. var _ref = options || {},
  98. cssUnit = _ref.unit;
  99. var mergedUnit = true;
  100. if (typeof cssUnit === 'boolean') {
  101. mergedUnit = cssUnit;
  102. } else if (Array.from(this.unitlessCssVar).some(function (cssVar) {
  103. return _this2.result.includes(cssVar);
  104. })) {
  105. mergedUnit = false;
  106. }
  107. this.result = this.result.replace(regexp, mergedUnit ? 'px' : '');
  108. if (typeof this.lowPriority !== 'undefined') {
  109. return "calc(".concat(this.result, ")");
  110. }
  111. return this.result;
  112. }
  113. }]);
  114. return CSSCalculator;
  115. }(AbstractCalculator);
  116. export { CSSCalculator as default };