Skip to content

Commit ebc5419

Browse files
seferturantrakt-bot[bot]
authored andcommitted
fix(review): fixes review word counting for international languages
Fixes #2110
1 parent c317f87 commit ebc5419

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

projects/client/src/lib/sections/summary/components/comments/_internal/drawers/isReviewValid.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,16 @@ describe('isReviewValid', () => {
7070
it('should return false for fewer than 5 emoji-free words with surrounding emojis', () => {
7171
expect(isReviewValid('😀🎉🔥 one two three four 🌟💯')).toBe(false);
7272
});
73+
74+
it('should count Japanese words correctly without spaces', () => {
75+
expect(isReviewValid('このアニメは本当に素晴らしかった')).toBe(true);
76+
});
77+
78+
it('should return false for too few Japanese characters', () => {
79+
expect(isReviewValid('すごい')).toBe(false);
80+
});
81+
82+
it('should count mixed Japanese and English words correctly', () => {
83+
expect(isReviewValid('この映画は really amazing')).toBe(true);
84+
});
7385
});
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const WORD_COUNT_THRESHOLD = 5;
1+
const wordCountThreshold = 5;
2+
const segmenter = new Intl.Segmenter(undefined, { granularity: 'word' });
23

34
export function isReviewValid(review: string): boolean {
4-
const words = review
5-
.split(/\s+/)
6-
.filter((word) => /[\p{L}\p{N}]/u.test(word));
5+
const words = [...segmenter.segment(review)]
6+
.filter((segment) => segment.isWordLike);
77

8-
return words.length >= WORD_COUNT_THRESHOLD;
8+
return words.length >= wordCountThreshold;
99
}

0 commit comments

Comments
 (0)