You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.7 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = _default;
  6. var order = {
  7. "*": 0,
  8. "/": 0,
  9. "+": 1,
  10. "-": 1
  11. };
  12. function round(value, prec) {
  13. if (prec !== false) {
  14. var precision = Math.pow(10, prec);
  15. return Math.round(value * precision) / precision;
  16. }
  17. return value;
  18. }
  19. function stringify(node, prec) {
  20. switch (node.type) {
  21. case "MathExpression":
  22. {
  23. var left = node.left,
  24. right = node.right,
  25. op = node.operator;
  26. var str = "";
  27. if (left.type === 'MathExpression' && order[op] < order[left.operator]) str += `(${stringify(left, prec)})`;else str += stringify(left, prec);
  28. str += order[op] ? ` ${node.operator} ` : node.operator;
  29. if (right.type === 'MathExpression' && order[op] < order[right.operator]) str += `(${stringify(right, prec)})`;else str += stringify(right, prec);
  30. return str;
  31. }
  32. case "Value":
  33. return round(node.value, prec);
  34. case 'Function':
  35. return node.value;
  36. default:
  37. return round(node.value, prec) + node.unit;
  38. }
  39. }
  40. function _default(calc, node, originalValue, options, result, item) {
  41. var str = stringify(node, options.precision);
  42. if (node.type === "MathExpression") {
  43. // if calc expression couldn't be resolved to a single value, re-wrap it as
  44. // a calc()
  45. str = `${calc}(${str})`; // if the warnWhenCannotResolve option is on, inform the user that the calc
  46. // expression could not be resolved to a single value
  47. if (options.warnWhenCannotResolve) {
  48. result.warn("Could not reduce expression: " + originalValue, {
  49. plugin: 'postcss-calc',
  50. node: item
  51. });
  52. }
  53. }
  54. return str;
  55. }
  56. module.exports = exports.default;