1
0
silverbullet/packages/silverbullet-common/util.ts

32 lines
709 B
TypeScript
Raw Normal View History

export function countWords(str: string): number {
2022-03-29 10:13:46 +00:00
const matches = str.match(/[\w\d\'-]+/gi);
return matches ? matches.length : 0;
}
export function readingTime(wordCount: number): number {
// 225 is average word reading speed for adults
return Math.ceil(wordCount / 225);
}
export function safeRun(fn: () => Promise<void>) {
fn().catch((e) => {
console.error(e);
});
}
export function isMacLike() {
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
}
export function throttle(func: () => void, limit: number) {
let timer: any = null;
return function () {
if (!timer) {
timer = setTimeout(() => {
func();
timer = null;
}, limit);
}
};
}