ckgl/node_modules/vant/lib/utils/format.js
2024-12-21 13:52:42 +08:00

140 lines
4.0 KiB
JavaScript

var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
addNumber: () => addNumber,
addUnit: () => addUnit,
camelize: () => camelize,
clamp: () => clamp,
formatNumber: () => formatNumber,
getSizeStyle: () => getSizeStyle,
getZIndexStyle: () => getZIndexStyle,
kebabCase: () => kebabCase,
padZero: () => padZero,
unitToPx: () => unitToPx
});
module.exports = __toCommonJS(stdin_exports);
var import_basic = require("./basic");
var import_dom = require("./dom");
var import_basic2 = require("./basic");
function addUnit(value) {
if ((0, import_basic2.isDef)(value)) {
return (0, import_basic2.isNumeric)(value) ? `${value}px` : String(value);
}
return void 0;
}
function getSizeStyle(originSize) {
if ((0, import_basic2.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 * import_dom.windowWidth.value / 100;
}
function convertVh(value) {
value = value.replace(/vh/g, "");
return +value * import_dom.windowHeight.value / 100;
}
function unitToPx(value) {
if (typeof value === "number") {
return value;
}
if (import_basic.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;
}