Skip to content

Commit 94e283e

Browse files
committed
Fix contraction counting in countWords function
1 parent 87be4eb commit 94e283e

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

src/utils/common/splitWords.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
* @returns {string[]} An array containing the words extracted from the text.
55
*/
66
export function splitWords(text: string) {
7-
return text.split(/[\s\W]+/).filter(Boolean);
7+
return text.match(/[\w']+(?:'\w+)*/g) || [];
88
}

tests/hooks/useTextAnalyzer.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('useTextAnalyzer', () => {
2323
const { result } = renderHook(() => useTextAnalyzer({ text: text.trim(), searchTerm: 'SeCoNd', ignoreCase: true }));
2424

2525
expect(result.current).toEqual({
26-
wordCount: 80,
26+
wordCount: 78,
2727
charCount: 560,
2828
sentenceCount: 14,
2929
paragraphCount: 7,
@@ -46,7 +46,7 @@ describe('useTextAnalyzer', () => {
4646
);
4747

4848
expect(result.current).toEqual({
49-
wordCount: 80,
49+
wordCount: 78,
5050
charCount: 560,
5151
sentenceCount: 14,
5252
paragraphCount: 7,
@@ -63,7 +63,7 @@ describe('useTextAnalyzer', () => {
6363
const { result } = renderHook(() => useTextAnalyzer({ text, trimText: false }));
6464

6565
expect(result.current).toEqual({
66-
wordCount: 80,
66+
wordCount: 78,
6767
charCount: 566,
6868
sentenceCount: 14,
6969
paragraphCount: 7,
@@ -86,7 +86,7 @@ describe('useTextAnalyzer', () => {
8686
);
8787

8888
expect(result.current).toEqual({
89-
wordCount: 80,
89+
wordCount: 78,
9090
charCount: 560,
9191
sentenceCount: 14,
9292
paragraphCount: 7,

tests/utils/countWords.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,10 @@ describe('countWords', () => {
2828
it('should handle words containing digits', () => {
2929
expect(countWords('This sentence contains numbers like 123 and 456.')).toBe(8);
3030
});
31+
32+
it('should handle contractions as single words', () => {
33+
expect(countWords("don't count this as two words")).toBe(6);
34+
expect(countWords("it's a beautiful day, isn't it?")).toBe(6);
35+
expect(countWords("it'''''s a trap!")).toBe(3);
36+
});
3137
});

0 commit comments

Comments
 (0)