Skip to content

Commit 5d67a13

Browse files
committed
Fix case when wordsPerMinute is string
1 parent 2157676 commit 5d67a13

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/useTextAnalyzer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@ function useTextAnalyzer({
1717
wordsPerMinute = WORDS_PER_MINUTE,
1818
}: TextAnalyzerOptions): TextAnalysisResult {
1919
const processedText = trimText ? text.trim() : text;
20+
const validWPM = Number(wordsPerMinute) || WORDS_PER_MINUTE;
2021

2122
const analysisResult = useMemo(() => {
2223
return calculateStats({
2324
text: processedText,
2425
searchTerm,
2526
ignoreCase,
26-
wordsPerMinute,
27+
wordsPerMinute: validWPM,
2728
});
28-
}, [processedText, searchTerm, ignoreCase, wordsPerMinute]);
29+
}, [processedText, searchTerm, ignoreCase, validWPM]);
2930

3031
return analysisResult;
3132
}

tests/hooks/useTextAnalyzer.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,54 @@ describe('useTextAnalyzer', () => {
118118
leastFrequentCharacter: '!',
119119
});
120120
});
121+
122+
it('should analyze text correctly with searchTerm and when wordsPerMinute is string', () => {
123+
const { result } = renderHook(() =>
124+
// @ts-expect-error edge case when wordsPerMinute is a string
125+
useTextAnalyzer({ text: text.trim(), searchTerm: 'SeCoNd', ignoreCase: true, wordsPerMinute: '250' }),
126+
);
127+
128+
expect(result.current).toEqual({
129+
wordCount: 78,
130+
charCount: 560,
131+
sentenceCount: 14,
132+
paragraphCount: 7,
133+
searchFrequency: 2,
134+
readingTime: {
135+
minutes: 0,
136+
seconds: 19,
137+
total: 19,
138+
text: 'less than a minute read',
139+
},
140+
mostFrequentWord: 'paragraph',
141+
leastFrequentWord: 'first',
142+
mostFrequentCharacter: 'a',
143+
leastFrequentCharacter: '!',
144+
});
145+
});
146+
147+
it('should analyze text correctly with searchTerm and when wordsPerMinute is empty string', () => {
148+
const { result } = renderHook(() =>
149+
// @ts-expect-error edge case when wordsPerMinute is a string
150+
useTextAnalyzer({ text: text.trim(), searchTerm: 'SeCoNd', ignoreCase: true, wordsPerMinute: '' }),
151+
);
152+
153+
expect(result.current).toEqual({
154+
wordCount: 78,
155+
charCount: 560,
156+
sentenceCount: 14,
157+
paragraphCount: 7,
158+
searchFrequency: 2,
159+
readingTime: {
160+
minutes: 0,
161+
seconds: 19,
162+
total: 19,
163+
text: 'less than a minute read',
164+
},
165+
mostFrequentWord: 'paragraph',
166+
leastFrequentWord: 'first',
167+
mostFrequentCharacter: 'a',
168+
leastFrequentCharacter: '!',
169+
});
170+
});
121171
});

0 commit comments

Comments
 (0)