@@ -1,3 +1,4 @@
|
||||
export * from "./object";
|
||||
export * from "./string";
|
||||
export * from "./cookie";
|
||||
export * from "./stopwatch";
|
||||
|
||||
44
packages/common/src/stopwatch.ts
Normal file
44
packages/common/src/stopwatch.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import dayjs from "dayjs";
|
||||
import relativeTime from "dayjs/plugin/relativeTime";
|
||||
import updateLocale from "dayjs/plugin/updateLocale";
|
||||
|
||||
dayjs.extend(relativeTime);
|
||||
dayjs.extend(updateLocale);
|
||||
dayjs.updateLocale("en", {
|
||||
relativeTime: {
|
||||
future: "in %s",
|
||||
past: "%s ago",
|
||||
s: "one second",
|
||||
ss: "%d seconds",
|
||||
m: "a minute",
|
||||
mm: "%d minutes",
|
||||
h: "an hour",
|
||||
hh: "%d hours",
|
||||
d: "a day",
|
||||
dd: "%d days",
|
||||
M: "a month",
|
||||
MM: "%d months",
|
||||
y: "a year",
|
||||
yy: "%d years",
|
||||
},
|
||||
});
|
||||
|
||||
export class Stopwatch {
|
||||
private startTime: number;
|
||||
|
||||
constructor() {
|
||||
this.startTime = performance.now();
|
||||
}
|
||||
|
||||
getElapsedInHumanWords() {
|
||||
const difference = performance.now() - this.startTime;
|
||||
if (difference < 1000) {
|
||||
return `${Math.floor(difference)} ms`;
|
||||
}
|
||||
return dayjs().millisecond(this.startTime).fromNow(true);
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.startTime = performance.now();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user