UNPKG

maz-ui

Version:

A standalone components library for Vue.Js 3 & Nuxt.Js 3

1 lines 1.06 kB
import{computed}from"vue";var wordRegex=/\b\w+\b/g;function countWords(text){let words=text.join(`, `).match(wordRegex);return words?words.length:0}function getReadTimeInMinutes(content,velocity=150){let words=countWords([content]);return Math.ceil(words/velocity)}function useReadingTime(options){let velocity=computed(()=>typeof options.velocity==`number`?options.velocity:options.velocity?.value??150);let selector=computed(()=>typeof options.contentSelector==`string`?options.contentSelector:options.contentSelector?.value);let content=computed(()=>{if(typeof options.contentRef?.value==`object`)return options.contentRef.value?.textContent;if(selector.value&&typeof document<`u`){let contentElement=document.querySelector(selector.value);if(contentElement)return contentElement.textContent}return typeof options.content==`string`?options.content:options.content?.value});let duration=computed(()=>getReadTimeInMinutes(content.value,velocity.value));return{content,wordCount:computed(()=>countWords([content.value])),velocity,duration}}export{useReadingTime};