format.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. addNumber: () => addNumber,
  21. addUnit: () => addUnit,
  22. camelize: () => camelize,
  23. clamp: () => clamp,
  24. formatNumber: () => formatNumber,
  25. getSizeStyle: () => getSizeStyle,
  26. getZIndexStyle: () => getZIndexStyle,
  27. kebabCase: () => kebabCase,
  28. padZero: () => padZero,
  29. unitToPx: () => unitToPx
  30. });
  31. module.exports = __toCommonJS(stdin_exports);
  32. var import_basic = require("./basic");
  33. var import_dom = require("./dom");
  34. var import_validate = require("./validate");
  35. function addUnit(value) {
  36. if ((0, import_validate.isDef)(value)) {
  37. return (0, import_validate.isNumeric)(value) ? `${value}px` : String(value);
  38. }
  39. return void 0;
  40. }
  41. function getSizeStyle(originSize) {
  42. if ((0, import_validate.isDef)(originSize)) {
  43. if (Array.isArray(originSize)) {
  44. return {
  45. width: addUnit(originSize[0]),
  46. height: addUnit(originSize[1])
  47. };
  48. }
  49. const size = addUnit(originSize);
  50. return {
  51. width: size,
  52. height: size
  53. };
  54. }
  55. }
  56. function getZIndexStyle(zIndex) {
  57. const style = {};
  58. if (zIndex !== void 0) {
  59. style.zIndex = +zIndex;
  60. }
  61. return style;
  62. }
  63. let rootFontSize;
  64. function getRootFontSize() {
  65. if (!rootFontSize) {
  66. const doc = document.documentElement;
  67. const fontSize = doc.style.fontSize || window.getComputedStyle(doc).fontSize;
  68. rootFontSize = parseFloat(fontSize);
  69. }
  70. return rootFontSize;
  71. }
  72. function convertRem(value) {
  73. value = value.replace(/rem/g, "");
  74. return +value * getRootFontSize();
  75. }
  76. function convertVw(value) {
  77. value = value.replace(/vw/g, "");
  78. return +value * import_dom.windowWidth.value / 100;
  79. }
  80. function convertVh(value) {
  81. value = value.replace(/vh/g, "");
  82. return +value * import_dom.windowHeight.value / 100;
  83. }
  84. function unitToPx(value) {
  85. if (typeof value === "number") {
  86. return value;
  87. }
  88. if (import_basic.inBrowser) {
  89. if (value.includes("rem")) {
  90. return convertRem(value);
  91. }
  92. if (value.includes("vw")) {
  93. return convertVw(value);
  94. }
  95. if (value.includes("vh")) {
  96. return convertVh(value);
  97. }
  98. }
  99. return parseFloat(value);
  100. }
  101. const camelizeRE = /-(\w)/g;
  102. const camelize = (str) => str.replace(camelizeRE, (_, c) => c.toUpperCase());
  103. const kebabCase = (str) => str.replace(/([A-Z])/g, "-$1").toLowerCase().replace(/^-/, "");
  104. function padZero(num, targetLength = 2) {
  105. let str = num + "";
  106. while (str.length < targetLength) {
  107. str = "0" + str;
  108. }
  109. return str;
  110. }
  111. const clamp = (num, min, max) => Math.min(Math.max(num, min), max);
  112. function trimExtraChar(value, char, regExp) {
  113. const index = value.indexOf(char);
  114. if (index === -1) {
  115. return value;
  116. }
  117. if (char === "-" && index !== 0) {
  118. return value.slice(0, index);
  119. }
  120. return value.slice(0, index + 1) + value.slice(index).replace(regExp, "");
  121. }
  122. function formatNumber(value, allowDot = true, allowMinus = true) {
  123. if (allowDot) {
  124. value = trimExtraChar(value, ".", /\./g);
  125. } else {
  126. value = value.split(".")[0];
  127. }
  128. if (allowMinus) {
  129. value = trimExtraChar(value, "-", /-/g);
  130. } else {
  131. value = value.replace(/-/, "");
  132. }
  133. const regExp = allowDot ? /[^-0-9.]/g : /[^-0-9]/g;
  134. return value.replace(regExp, "");
  135. }
  136. function addNumber(num1, num2) {
  137. const cardinal = 10 ** 10;
  138. return Math.round((num1 + num2) * cardinal) / cardinal;
  139. }