File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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} ) ;
Original file line number Diff line number Diff line change 1- const WORD_COUNT_THRESHOLD = 5 ;
1+ const wordCountThreshold = 5 ;
2+ const segmenter = new Intl . Segmenter ( undefined , { granularity : 'word' } ) ;
23
34export 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}
You can’t perform that action at this time.
0 commit comments