CSSCalculator.js 4.6 KB

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