Skip to content

Commit

Permalink
fix(dfts-helper): read time use trim instead of strip whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Dafnik committed Jan 2, 2024
1 parent 4249b3e commit 1b2eb30
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions libs/dfts-helper/src/lib/helper/string/read-time/read-time.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { s_stripWhitespace } from '../strip-whitespace';
import { n_humanizeTime } from '../../number/humanize-time';
import { s_countWords } from '../count-words/count-words';

Expand All @@ -11,16 +10,16 @@ const IMAGE_READ_TIME = 12; // in seconds
* @param {number} [imageCounter=0] - The number of images in the text.
* @returns {string} - The estimated time it takes to read the text, in the format "Xm" where X is the number of minutes.
*/
export const s_readTime = (text: string, imageCounter = 0): string =>
n_humanizeTime(imageReadTime(imageCounter) + calculateWordsReadTime(s_stripWhitespace(text)));
export const s_readTime = (text: string, imageCounter: number = 0): string =>
n_humanizeTime(imageReadTime(imageCounter) + calculateWordsReadTime(text.trim()));

/**
* Calculates the estimated time it takes to read text based on the number of words and a given words per minute rate.
* @param {string} text - The text to be read.
* @param {number} [wordsPerMin=WORDS_PER_MIN] - The words per minute rate to use for calculating read time.
* @returns {number} - The estimated time it takes to read the text in minutes.
*/
export const calculateWordsReadTime = (text: string, wordsPerMin = WORDS_PER_MIN): number =>
export const calculateWordsReadTime = (text: string, wordsPerMin: number = WORDS_PER_MIN): number =>
s_countWords(removeOtherLanguageCharacters(text)) / wordsPerMin;

/**
Expand All @@ -37,5 +36,5 @@ export const removeOtherLanguageCharacters = (text: string): string =>
* @param {number} [customImageTime=IMAGE_READ_TIME] - The time per image to use for calculating read time.
* @returns {number} - The estimated time it takes to read the images in minutes.
*/
export const imageReadTime = (count: number, customImageTime = IMAGE_READ_TIME): number =>
export const imageReadTime = (count: number, customImageTime: number = IMAGE_READ_TIME): number =>
count > 10 ? ((count / 2) * (customImageTime + 3) + (count - 10) * 3) / 60 : ((count / 2) * (2 * customImageTime + (1 - count))) / 60;

0 comments on commit 1b2eb30

Please sign in to comment.