123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- import { inBrowser } from "./basic.mjs";
- import { windowWidth, windowHeight } from "./dom.mjs";
- import { isDef, isNumeric } from "./basic.mjs";
- function addUnit(value) {
- if (isDef(value)) {
- return isNumeric(value) ? `${value}px` : String(value);
- }
- return void 0;
- }
- function getSizeStyle(originSize) {
- if (isDef(originSize)) {
- if (Array.isArray(originSize)) {
- return {
- width: addUnit(originSize[0]),
- height: addUnit(originSize[1])
- };
- }
- const size = addUnit(originSize);
- return {
- width: size,
- height: size
- };
- }
- }
- function getZIndexStyle(zIndex) {
- const style = {};
- if (zIndex !== void 0) {
- style.zIndex = +zIndex;
- }
- return style;
- }
- let rootFontSize;
- function getRootFontSize() {
- if (!rootFontSize) {
- const doc = document.documentElement;
- const fontSize = doc.style.fontSize || window.getComputedStyle(doc).fontSize;
- rootFontSize = parseFloat(fontSize);
- }
- return rootFontSize;
- }
- function convertRem(value) {
- value = value.replace(/rem/g, "");
- return +value * getRootFontSize();
- }
- function convertVw(value) {
- value = value.replace(/vw/g, "");
- return +value * windowWidth.value / 100;
- }
- function convertVh(value) {
- value = value.replace(/vh/g, "");
- return +value * windowHeight.value / 100;
- }
- function unitToPx(value) {
- if (typeof value === "number") {
- return value;
- }
- if (inBrowser) {
- if (value.includes("rem")) {
- return convertRem(value);
- }
- if (value.includes("vw")) {
- return convertVw(value);
- }
- if (value.includes("vh")) {
- return convertVh(value);
- }
- }
- return parseFloat(value);
- }
- const camelizeRE = /-(\w)/g;
- const camelize = (str) => str.replace(camelizeRE, (_, c) => c.toUpperCase());
- const kebabCase = (str) => str.replace(/([A-Z])/g, "-$1").toLowerCase().replace(/^-/, "");
- function padZero(num, targetLength = 2) {
- let str = num + "";
- while (str.length < targetLength) {
- str = "0" + str;
- }
- return str;
- }
- const clamp = (num, min, max) => Math.min(Math.max(num, min), max);
- function trimExtraChar(value, char, regExp) {
- const index = value.indexOf(char);
- if (index === -1) {
- return value;
- }
- if (char === "-" && index !== 0) {
- return value.slice(0, index);
- }
- return value.slice(0, index + 1) + value.slice(index).replace(regExp, "");
- }
- function formatNumber(value, allowDot = true, allowMinus = true) {
- if (allowDot) {
- value = trimExtraChar(value, ".", /\./g);
- } else {
- value = value.split(".")[0];
- }
- if (allowMinus) {
- value = trimExtraChar(value, "-", /-/g);
- } else {
- value = value.replace(/-/, "");
- }
- const regExp = allowDot ? /[^-0-9.]/g : /[^-0-9]/g;
- return value.replace(regExp, "");
- }
- function addNumber(num1, num2) {
- const cardinal = 10 ** 10;
- return Math.round((num1 + num2) * cardinal) / cardinal;
- }
- export {
- addNumber,
- addUnit,
- camelize,
- clamp,
- formatNumber,
- getSizeStyle,
- getZIndexStyle,
- kebabCase,
- padZero,
- unitToPx
- };
|