@applicaster/zapp-react-native-utils
Version:
Applicaster Zapp React Native utilities package
47 lines (39 loc) • 1.33 kB
text/typescript
import { createLogger } from "../../logger";
const { log_error } = createLogger({
category: "AccessibilityManager",
subsystem: "AppUtils",
});
/**
* Calculates the reading time for a given text based on word count
* @param text - The text to calculate the reading time for (string or number)
* @param wordsPerMinute - Words per minute reading speed (default: 140)
* @param minimumPause - Minimum pause time in milliseconds (default: 500)
* @param announcementDelay - Additional delay for announcement in milliseconds (default: 700)
* @returns The reading time in milliseconds
*/
export function calculateReadingTime(
text: string | number,
wordsPerMinute: number = 140,
minimumPause: number = 500,
announcementDelay: number = 700
): number {
if (typeof text !== "string" && typeof text !== "number") {
log_error(
`Invalid text input for reading time calculation got: ${
typeof text === "symbol" ? String(text) : text
}`
);
return 0;
}
const trimmed = typeof text === "number" ? String(text) : text.trim();
if (!trimmed) {
return 0;
}
const words = trimmed
.split(/(?<=\d)(?=[a-zA-Z])|(?<=[a-zA-Z])(?=\d)|[^\w\s]+|\s+/)
.filter(Boolean).length;
return (
Math.max(minimumPause, (words / wordsPerMinute) * 60 * 1000) +
announcementDelay
);
}