vuux
Version:
Vue3 Nuxt3 Nuxt4 组件库
50 lines (49 loc) • 1.49 kB
JavaScript
import { ref as g, computed as y, watch as b, onMounted as h } from "vue";
const V = (t) => {
const f = typeof window < "u" && typeof document < "u", u = g(t.start);
let c = "", m = NaN;
const w = (e) => !isNaN(parseFloat(e)), N = (e) => e < 0.5 ? 4 * e * e * e : 1 - Math.pow(-2 * e + 2, 3) / 2, x = (e) => {
if (typeof e != "number")
return "0";
if (e === m)
return c;
const a = e.toFixed(t.decimalPlaces).split(".");
let n = a[0];
const l = a.length > 1 ? t.decimal + a[1] : "", i = /(\d+)(\d{3})/;
if (t.useGrouping && t.separator && !w(t.separator))
for (; i.test(n); )
n = n.replace(i, "$1" + t.separator + "$2");
return m = e, c = t.prefix + n + l + t.suffix, c;
}, F = y(() => x(u.value));
let o = null;
const s = () => {
if (!f)
return;
o && (cancelAnimationFrame(o), o = null);
const e = Date.now(), r = e + t.duration, a = u.value, n = t.end - a, l = () => {
const i = Date.now();
if (i < r) {
let d = (i - e) / t.duration;
d = N(d), u.value = a + d * n, o = requestAnimationFrame(l);
} else
u.value = t.end, o = null;
};
l();
};
return b(
() => [t.start, t.end],
(e, r = []) => {
const [a, n] = e, [l, i] = r;
a !== l && (u.value = a), t.autoplay && n !== i && s();
},
{ immediate: !0 }
), h(() => {
t.autoplay && s();
}), {
displayValue: F,
start: s
};
};
export {
V as useStatistic
};