@chakra-ui/styled-system
Version:
Style function for css-in-js building component libraries
102 lines (82 loc) • 2.94 kB
JavaScript
/**
* Thank you @markdalgleish for this piece of art!
*/
import { isObject } from "@chakra-ui/utils";
function resolveReference(operand) {
if (isObject(operand) && operand.reference) {
return operand.reference;
}
return String(operand);
}
var toExpression = function toExpression(operator) {
for (var _len = arguments.length, operands = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
operands[_key - 1] = arguments[_key];
}
return operands.map(resolveReference).join(" " + operator + " ").replace(/calc/g, "");
};
var _add = function add() {
for (var _len2 = arguments.length, operands = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
operands[_key2] = arguments[_key2];
}
return "calc(" + toExpression("+", ...operands) + ")";
};
var _subtract = function subtract() {
for (var _len3 = arguments.length, operands = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
operands[_key3] = arguments[_key3];
}
return "calc(" + toExpression("-", ...operands) + ")";
};
var _multiply = function multiply() {
for (var _len4 = arguments.length, operands = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
operands[_key4] = arguments[_key4];
}
return "calc(" + toExpression("*", ...operands) + ")";
};
var _divide = function divide() {
for (var _len5 = arguments.length, operands = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
operands[_key5] = arguments[_key5];
}
return "calc(" + toExpression("/", ...operands) + ")";
};
var _negate = x => {
var value = resolveReference(x);
if (value != null && !Number.isNaN(parseFloat(value))) {
return String(value).startsWith("-") ? String(value).slice(1) : "-" + value;
}
return _multiply(value, -1);
};
export var calc = Object.assign(x => ({
add: function add() {
for (var _len6 = arguments.length, operands = new Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
operands[_key6] = arguments[_key6];
}
return calc(_add(x, ...operands));
},
subtract: function subtract() {
for (var _len7 = arguments.length, operands = new Array(_len7), _key7 = 0; _key7 < _len7; _key7++) {
operands[_key7] = arguments[_key7];
}
return calc(_subtract(x, ...operands));
},
multiply: function multiply() {
for (var _len8 = arguments.length, operands = new Array(_len8), _key8 = 0; _key8 < _len8; _key8++) {
operands[_key8] = arguments[_key8];
}
return calc(_multiply(x, ...operands));
},
divide: function divide() {
for (var _len9 = arguments.length, operands = new Array(_len9), _key9 = 0; _key9 < _len9; _key9++) {
operands[_key9] = arguments[_key9];
}
return calc(_divide(x, ...operands));
},
negate: () => calc(_negate(x)),
toString: () => x.toString()
}), {
add: _add,
subtract: _subtract,
multiply: _multiply,
divide: _divide,
negate: _negate
});
//# sourceMappingURL=calc.js.map