bc518174
王天杨
提交两个项目文件
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.reduceCSSCalc = reduceCSSCalc;
exports.safeEvaluateExpression = safeEvaluateExpression;
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); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
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."); }
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); }
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; }
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; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
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); }
var MULTIPLY_OR_DIVIDE_REGEX = /(-?\d+(?:\.\d+)?[a-zA-Z%]*)([*/])(-?\d+(?:\.\d+)?[a-zA-Z%]*)/;
var ADD_OR_SUBTRACT_REGEX = /(-?\d+(?:\.\d+)?[a-zA-Z%]*)([+-])(-?\d+(?:\.\d+)?[a-zA-Z%]*)/;
var CSS_LENGTH_UNIT_REGEX = /^px|cm|vh|vw|em|rem|%|mm|in|pt|pc|ex|ch|vmin|vmax|Q$/;
var NUM_SPLIT_REGEX = /(-?\d+(?:\.\d+)?)([a-zA-Z%]+)?/;
var CONVERSION_RATES = {
cm: 96 / 2.54,
mm: 96 / 25.4,
pt: 96 / 72,
pc: 96 / 6,
"in": 96,
Q: 96 / (2.54 * 40),
px: 1
};
var FIXED_CSS_LENGTH_UNITS = Object.keys(CONVERSION_RATES);
var STR_NAN = 'NaN';
function convertToPx(value, unit) {
return value * CONVERSION_RATES[unit];
}
var DecimalCSS = /*#__PURE__*/function () {
function DecimalCSS(num, unit) {
_classCallCheck(this, DecimalCSS);
this.num = num;
this.unit = unit;
this.num = num;
this.unit = unit;
if (Number.isNaN(num)) {
this.unit = '';
}
if (unit !== '' && !CSS_LENGTH_UNIT_REGEX.test(unit)) {
this.num = NaN;
this.unit = '';
}
if (FIXED_CSS_LENGTH_UNITS.includes(unit)) {
this.num = convertToPx(num, unit);
this.unit = 'px';
}
}
return _createClass(DecimalCSS, [{
key: "add",
value: function add(other) {
if (this.unit !== other.unit) {
return new DecimalCSS(NaN, '');
}
return new DecimalCSS(this.num + other.num, this.unit);
}
}, {
key: "subtract",
value: function subtract(other) {
if (this.unit !== other.unit) {
return new DecimalCSS(NaN, '');
}
return new DecimalCSS(this.num - other.num, this.unit);
}
}, {
key: "multiply",
value: function multiply(other) {
if (this.unit !== '' && other.unit !== '' && this.unit !== other.unit) {
return new DecimalCSS(NaN, '');
}
return new DecimalCSS(this.num * other.num, this.unit || other.unit);
}
}, {
key: "divide",
value: function divide(other) {
if (this.unit !== '' && other.unit !== '' && this.unit !== other.unit) {
return new DecimalCSS(NaN, '');
}
return new DecimalCSS(this.num / other.num, this.unit || other.unit);
}
}, {
key: "toString",
value: function toString() {
return "".concat(this.num).concat(this.unit);
}
}, {
key: "isNaN",
value: function isNaN() {
return Number.isNaN(this.num);
}
}], [{
key: "parse",
value: function parse(str) {
var _NUM_SPLIT_REGEX$exec;
var _ref = (_NUM_SPLIT_REGEX$exec = NUM_SPLIT_REGEX.exec(str)) !== null && _NUM_SPLIT_REGEX$exec !== void 0 ? _NUM_SPLIT_REGEX$exec : [],
_ref2 = _slicedToArray(_ref, 3),
numStr = _ref2[1],
unit = _ref2[2];
return new DecimalCSS(parseFloat(numStr), unit !== null && unit !== void 0 ? unit : '');
}
}]);
}();
function calculateArithmetic(expr) {
if (expr.includes(STR_NAN)) {
return STR_NAN;
}
var newExpr = expr;
while (newExpr.includes('*') || newExpr.includes('/')) {
var _MULTIPLY_OR_DIVIDE_R;
var _ref3 = (_MULTIPLY_OR_DIVIDE_R = MULTIPLY_OR_DIVIDE_REGEX.exec(newExpr)) !== null && _MULTIPLY_OR_DIVIDE_R !== void 0 ? _MULTIPLY_OR_DIVIDE_R : [],
_ref4 = _slicedToArray(_ref3, 4),
leftOperand = _ref4[1],
operator = _ref4[2],
rightOperand = _ref4[3];
var lTs = DecimalCSS.parse(leftOperand !== null && leftOperand !== void 0 ? leftOperand : '');
var rTs = DecimalCSS.parse(rightOperand !== null && rightOperand !== void 0 ? rightOperand : '');
var result = operator === '*' ? lTs.multiply(rTs) : lTs.divide(rTs);
if (result.isNaN()) {
return STR_NAN;
}
newExpr = newExpr.replace(MULTIPLY_OR_DIVIDE_REGEX, result.toString());
}
while (newExpr.includes('+') || /.-\d+(?:\.\d+)?/.test(newExpr)) {
var _ADD_OR_SUBTRACT_REGE;
var _ref5 = (_ADD_OR_SUBTRACT_REGE = ADD_OR_SUBTRACT_REGEX.exec(newExpr)) !== null && _ADD_OR_SUBTRACT_REGE !== void 0 ? _ADD_OR_SUBTRACT_REGE : [],
_ref6 = _slicedToArray(_ref5, 4),
_leftOperand = _ref6[1],
_operator = _ref6[2],
_rightOperand = _ref6[3];
var _lTs = DecimalCSS.parse(_leftOperand !== null && _leftOperand !== void 0 ? _leftOperand : '');
var _rTs = DecimalCSS.parse(_rightOperand !== null && _rightOperand !== void 0 ? _rightOperand : '');
var _result = _operator === '+' ? _lTs.add(_rTs) : _lTs.subtract(_rTs);
if (_result.isNaN()) {
return STR_NAN;
}
newExpr = newExpr.replace(ADD_OR_SUBTRACT_REGEX, _result.toString());
}
return newExpr;
}
var PARENTHESES_REGEX = /\(([^()]*)\)/;
function calculateParentheses(expr) {
var newExpr = expr;
while (newExpr.includes('(')) {
var _PARENTHESES_REGEX$ex = PARENTHESES_REGEX.exec(newExpr),
_PARENTHESES_REGEX$ex2 = _slicedToArray(_PARENTHESES_REGEX$ex, 2),
parentheticalExpression = _PARENTHESES_REGEX$ex2[1];
newExpr = newExpr.replace(PARENTHESES_REGEX, calculateArithmetic(parentheticalExpression));
}
return newExpr;
}
function evaluateExpression(expression) {
var newExpr = expression.replace(/\s+/g, '');
newExpr = calculateParentheses(newExpr);
newExpr = calculateArithmetic(newExpr);
return newExpr;
}
function safeEvaluateExpression(expression) {
try {
return evaluateExpression(expression);
} catch (e) {
/* istanbul ignore next */
return STR_NAN;
}
}
function reduceCSSCalc(expression) {
var result = safeEvaluateExpression(expression.slice(5, -1));
if (result === STR_NAN) {
// notify the user
return '';
}
return result;
}
|