UNPKG

@opentiny/vue-renderless

Version:

An enterprise-class UI component library, support both Vue.js 2 and Vue.js 3, as well as PC and mobile.

80 lines (79 loc) 2.46 kB
import "../chunk-G2ADBYYC.js"; import { isFunction } from "@opentiny/utils"; const isNumber = ({ props }) => () => { return typeof props.value === "number"; }; const getIntegerAndDecimal = ({ props }) => () => { var _a, _b; if (isFunction(props.formatter)) { return props.formatter(props.value); } if (typeof props.value !== "number") { return props.value; } let displayValue = String(props.value).split("."); let integer = (_a = displayValue[0]) == null ? void 0 : _a.replace(/\B(?=(\d{3})+(?!\d))/g, props.groupSeparator); let decimal = (_b = displayValue[1]) == null ? void 0 : _b.padEnd(props.precision, "0"); if (props.precision >= 0) { decimal = decimal == null ? void 0 : decimal.slice(0, props.precision > 0 ? props.precision : 0); } if (!displayValue) { integer = "0"; } if (!decimal && props.precision) { let display = "0"; decimal = display.padEnd(props.precision, "0"); } return [integer, decimal].join(decimal ? "." : ""); }; const animateValue = ({ props, state }) => () => { if (!props.useAnimation) return; let AnimationId = null; const start = props.startValue; const end = Number(props.value); const duration = props.duration; if (Number.isNaN(end)) { state.animatingValue = props.value; return; } const startTime = performance.now(); const cancel = () => { if (AnimationId) { cancelAnimationFrame(AnimationId); AnimationId = null; } }; if (AnimationId) { cancel(); } const getDecimalPlaces = (num) => { if (Math.floor(num) === num) return 0; const decimalPart = num.toString().split(".")[1]; return decimalPart ? decimalPart.length : 0; }; const decimalPlaces = getDecimalPlaces(end); const multiplier = 10 ** decimalPlaces; const animate = (currentTime) => { const elapsed = currentTime - startTime; const progress = Math.min(elapsed / duration, 1); const easeOutExpo = (t) => t === 1 ? 1 : 1 - 2 ** (-10 * t); const current = start + (end - start) * easeOutExpo(progress); state.animatingValue = Math.round(current * multiplier) / multiplier; if (progress < 1) { AnimationId = requestAnimationFrame(animate); } else { state.animatingValue = end; AnimationId = null; } }; AnimationId = requestAnimationFrame(() => { AnimationId = requestAnimationFrame(animate); }); }; export { animateValue, getIntegerAndDecimal, isNumber };