format.mjs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { inBrowser } from "./basic.mjs";
  2. import { windowWidth, windowHeight } from "./dom.mjs";
  3. import { isDef, isNumeric } from "./basic.mjs";
  4. function addUnit(value) {
  5. if (isDef(value)) {
  6. return isNumeric(value) ? `${value}px` : String(value);
  7. }
  8. return void 0;
  9. }
  10. function getSizeStyle(originSize) {
  11. if (isDef(originSize)) {
  12. if (Array.isArray(originSize)) {
  13. return {
  14. width: addUnit(originSize[0]),
  15. height: addUnit(originSize[1])
  16. };
  17. }
  18. const size = addUnit(originSize);
  19. return {
  20. width: size,
  21. height: size
  22. };
  23. }
  24. }
  25. function getZIndexStyle(zIndex) {
  26. const style = {};
  27. if (zIndex !== void 0) {
  28. style.zIndex = +zIndex;
  29. }
  30. return style;
  31. }
  32. let rootFontSize;
  33. function getRootFontSize() {
  34. if (!rootFontSize) {
  35. const doc = document.documentElement;
  36. const fontSize = doc.style.fontSize || window.getComputedStyle(doc).fontSize;
  37. rootFontSize = parseFloat(fontSize);
  38. }
  39. return rootFontSize;
  40. }
  41. function convertRem(value) {
  42. value = value.replace(/rem/g, "");
  43. return +value * getRootFontSize();
  44. }
  45. function convertVw(value) {
  46. value = value.replace(/vw/g, "");
  47. return +value * windowWidth.value / 100;
  48. }
  49. function convertVh(value) {
  50. value = value.replace(/vh/g, "");
  51. return +value * windowHeight.value / 100;
  52. }
  53. function unitToPx(value) {
  54. if (typeof value === "number") {
  55. return value;
  56. }
  57. if (inBrowser) {
  58. if (value.includes("rem")) {
  59. return convertRem(value);
  60. }
  61. if (value.includes("vw")) {
  62. return convertVw(value);
  63. }
  64. if (value.includes("vh")) {
  65. return convertVh(value);
  66. }
  67. }
  68. return parseFloat(value);
  69. }
  70. const camelizeRE = /-(\w)/g;
  71. const camelize = (str) => str.replace(camelizeRE, (_, c) => c.toUpperCase());
  72. const kebabCase = (str) => str.replace(/([A-Z])/g, "-$1").toLowerCase().replace(/^-/, "");
  73. function padZero(num, targetLength = 2) {
  74. let str = num + "";
  75. while (str.length < targetLength) {
  76. str = "0" + str;
  77. }
  78. return str;
  79. }
  80. const clamp = (num, min, max) => Math.min(Math.max(num, min), max);
  81. function trimExtraChar(value, char, regExp) {
  82. const index = value.indexOf(char);
  83. if (index === -1) {
  84. return value;
  85. }
  86. if (char === "-" && index !== 0) {
  87. return value.slice(0, index);
  88. }
  89. return value.slice(0, index + 1) + value.slice(index).replace(regExp, "");
  90. }
  91. function formatNumber(value, allowDot = true, allowMinus = true) {
  92. if (allowDot) {
  93. value = trimExtraChar(value, ".", /\./g);
  94. } else {
  95. value = value.split(".")[0];
  96. }
  97. if (allowMinus) {
  98. value = trimExtraChar(value, "-", /-/g);
  99. } else {
  100. value = value.replace(/-/, "");
  101. }
  102. const regExp = allowDot ? /[^-0-9.]/g : /[^-0-9]/g;
  103. return value.replace(regExp, "");
  104. }
  105. function addNumber(num1, num2) {
  106. const cardinal = 10 ** 10;
  107. return Math.round((num1 + num2) * cardinal) / cardinal;
  108. }
  109. export {
  110. addNumber,
  111. addUnit,
  112. camelize,
  113. clamp,
  114. formatNumber,
  115. getSizeStyle,
  116. getZIndexStyle,
  117. kebabCase,
  118. padZero,
  119. unitToPx
  120. };