diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-11-06 18:08:04 +0100 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-11-11 18:15:27 +0100 |
| commit | c9c1c90b30e243563bb4f731da15b3fe657556d2 (patch) | |
| tree | 8263c176b4096e2893b9d9319bfa7edb01fce188 /src/utils/helpers/reading-time.ts | |
| parent | 2771de88f40a5f4ed7480bd8614532dda72deeda (diff) | |
refactor(components): replace Summary component with PostPreview
* rename component to PostPreview because Summary is an HTML element
and it could lead to confusion
* replace `title` and `titleLevel` with `heading` and `headingLvl`
because `title` is a native attribute
* rename `intro` prop to `excerpt`
* extract `cover` from `meta` prop
* rewrite meta type
* extract meta logic into a new component
Diffstat (limited to 'src/utils/helpers/reading-time.ts')
| -rw-r--r-- | src/utils/helpers/reading-time.ts | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/utils/helpers/reading-time.ts b/src/utils/helpers/reading-time.ts new file mode 100644 index 0000000..6cdeba4 --- /dev/null +++ b/src/utils/helpers/reading-time.ts @@ -0,0 +1,46 @@ +export type GetReadingTimeReturn = { + /** + * The reading time rounded to minutes. + */ + inMinutes: () => number; + /** + * The reading time in minutes and seconds. + */ + inMinutesAndSeconds: () => { + minutes: number; + seconds: number; + }; +}; + +/** + * Retrieve the reading time from a words count. + * + * @param {number} wordsCount - The number of words. + * @param {number} [wordsPerMinute] - How many words can we read per minute? + * @returns {GetReadingTimeReturn} Two methods to retrieve the reading time. + */ +export const getReadingTimeFrom = ( + wordsCount: number, + wordsPerMinute = 245 +): GetReadingTimeReturn => { + const ONE_MINUTE_IN_SECONDS = 60; + const wordsPerSecond = wordsPerMinute / ONE_MINUTE_IN_SECONDS; + const estimatedTimeInSeconds = wordsCount / wordsPerSecond; + + return { + inMinutes: () => Math.round(estimatedTimeInSeconds / ONE_MINUTE_IN_SECONDS), + inMinutesAndSeconds: () => { + const estimatedTimeInMinutes = Math.floor( + estimatedTimeInSeconds / ONE_MINUTE_IN_SECONDS + ); + + return { + minutes: estimatedTimeInMinutes, + seconds: Math.round( + estimatedTimeInSeconds - + estimatedTimeInMinutes * ONE_MINUTE_IN_SECONDS + ), + }; + }, + }; +}; |
