42 lines
1.3 KiB
JavaScript
42 lines
1.3 KiB
JavaScript
// src/css-calc.ts
|
|
import { isObject } from "@chakra-ui/shared-utils";
|
|
function toRef(operand) {
|
|
if (isObject(operand) && operand.reference) {
|
|
return operand.reference;
|
|
}
|
|
return String(operand);
|
|
}
|
|
var toExpr = (operator, ...operands) => operands.map(toRef).join(` ${operator} `).replace(/calc/g, "");
|
|
var add = (...operands) => `calc(${toExpr("+", ...operands)})`;
|
|
var subtract = (...operands) => `calc(${toExpr("-", ...operands)})`;
|
|
var multiply = (...operands) => `calc(${toExpr("*", ...operands)})`;
|
|
var divide = (...operands) => `calc(${toExpr("/", ...operands)})`;
|
|
var negate = (x) => {
|
|
const value = toRef(x);
|
|
if (value != null && !Number.isNaN(parseFloat(value))) {
|
|
return String(value).startsWith("-") ? String(value).slice(1) : `-${value}`;
|
|
}
|
|
return multiply(value, -1);
|
|
};
|
|
var calc = Object.assign(
|
|
(x) => ({
|
|
add: (...operands) => calc(add(x, ...operands)),
|
|
subtract: (...operands) => calc(subtract(x, ...operands)),
|
|
multiply: (...operands) => calc(multiply(x, ...operands)),
|
|
divide: (...operands) => calc(divide(x, ...operands)),
|
|
negate: () => calc(negate(x)),
|
|
toString: () => x.toString()
|
|
}),
|
|
{
|
|
add,
|
|
subtract,
|
|
multiply,
|
|
divide,
|
|
negate
|
|
}
|
|
);
|
|
|
|
export {
|
|
calc
|
|
};
|
|
//# sourceMappingURL=chunk-XMZHFSTS.mjs.map
|