Files
talks-site/frontend/src/utils/countWords.ts
T

10 lines
356 B
TypeScript

export function countWords(text: string): number {
// Trim leading/trailing whitespace, then split on one-or-more whitespace characters
const words = text.trim().split(/\s+/);
// If the string was empty or only whitespace, split() returns [''], so handle that
if (words.length === 1 && words[0] === "") {
return 0;
}
return words.length;
}