-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin-slimsearch): improve default querySplitter, close #299
- Loading branch information
1 parent
a11237c
commit 715dde1
Showing
4 changed files
with
39 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
plugins/search/plugin-slimsearch/src/client/utils/querySplitter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const CJK_REGEXP = | ||
/[\u4e00-\u9fff\u3400-\u4dbf\u3040-\u309f\u30a0-\u30ff\uac00-\ud7af]/ | ||
|
||
export const defaultQuerySplitter = (query: string): string[] => | ||
query | ||
.split(/\s+/) | ||
.map((word) => { | ||
if (word.length > 3) { | ||
const chars = word.split('') | ||
|
||
if (chars.every((char) => CJK_REGEXP.test(char))) return chars | ||
} | ||
|
||
return word | ||
}) | ||
.flat() |
18 changes: 18 additions & 0 deletions
18
plugins/search/plugin-slimsearch/tests/querySliptter.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { expect, it } from 'vitest' | ||
import { defaultQuerySplitter } from '../src/client/utils/querySplitter.js' | ||
|
||
it('defaultQuerySplitter', () => { | ||
const testCases: [string, string[]][] = [ | ||
['你好世界', ['你', '好', '世', '界']], | ||
['你好 世界', ['你好', '世界']], | ||
['hello world', ['hello', 'world']], | ||
['hello 你好 world 世界', ['hello', '你好', 'world', '世界']], | ||
['hi hello 你好 世界', ['hi', 'hello', '你好', '世界']], | ||
['', ['']], | ||
] | ||
|
||
for (const [query, expected] of testCases) { | ||
const result = defaultQuerySplitter(query) | ||
expect(result).toEqual(expected) | ||
} | ||
}) |