Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit20e1b2a

Browse files
authored
feat: support case sensitive search (#17)
* feat: support cases sensitive search* revert package.json
1 parentc15d0f2 commit20e1b2a

File tree

4 files changed

+68
-43
lines changed

4 files changed

+68
-43
lines changed

‎__tests__/Fuzzy.test.ts‎

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,50 @@ describe("Test Fuzzy", () => {
77
it("should return correct results for a query",()=>{
88
constquery="apple";
99
constresults=fuzzy.search(query);
10-
expect(results).toMatchSnapshot();
10+
expect(results).toMatchInlineSnapshot(`
11+
[
12+
{
13+
"distance": 0,
14+
"text": "apple",
15+
},
16+
{
17+
"distance": 4,
18+
"text": "pineapple",
19+
},
20+
{
21+
"distance": 5,
22+
"text": "orange",
23+
},
24+
{
25+
"distance": 5,
26+
"text": "banana",
27+
},
28+
]
29+
`);
30+
});
31+
32+
it('should return correct results when "caseSensitive" option is true',()=>{
33+
constfuzzy=newFuzzy(list,{caseSensitive:true});
34+
constquery='Apple';
35+
constresults=fuzzy.search(query);
36+
37+
// The Distance is updated since the query is caseSensitive
38+
expect(results).toMatchInlineSnapshot(`
39+
[
40+
{
41+
"distance": 1,
42+
"text": "apple",
43+
},
44+
{
45+
"distance": 5,
46+
"text": "pineapple",
47+
},
48+
{
49+
"distance": 5,
50+
"text": "orange",
51+
},
52+
]
53+
`);
1154
});
1255

1356
it("should return empty array for no matches",()=>{
@@ -28,7 +71,17 @@ describe("Test Fuzzy", () => {
2871
it("should set this.list to an empty array if list is undefined",()=>{
2972
// Simulate absence of list
3073
constbadFuzzy=newFuzzy(undefinedasany);
31-
expect(badFuzzy).toMatchSnapshot();
74+
expect(badFuzzy).toMatchInlineSnapshot(`
75+
Fuzzy {
76+
"list": [],
77+
"options": {
78+
"caseSensitive": false,
79+
"includeMatches": false,
80+
"includeScore": false,
81+
},
82+
"search": [Function],
83+
}
84+
`)
3285
});
3386

3487
it("should include score if includeScore option is true",()=>{
@@ -60,5 +113,4 @@ describe("Test Fuzzy", () => {
60113
]
61114
`)
62115
})
63-
64116
});

‎__tests__/__snapshots__/Fuzzy.test.ts.snap‎

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -80,35 +80,3 @@ exports[`Test Fuzzy > should include matches if includeMatches option is true 1`
8080
},
8181
]
8282
`;
83-
84-
exports[`Test Fuzzy > should return correct results for a query 1`]=`
85-
[
86-
{
87-
"distance":0,
88-
"text":"apple",
89-
},
90-
{
91-
"distance":4,
92-
"text":"pineapple",
93-
},
94-
{
95-
"distance":5,
96-
"text":"orange",
97-
},
98-
{
99-
"distance":5,
100-
"text":"banana",
101-
},
102-
]
103-
`;
104-
105-
exports[`Test Fuzzy > should set this.list to an empty array if list is undefined 1`]=`
106-
Fuzzy{
107-
"list": [],
108-
"options": {
109-
"includeMatches":false,
110-
"includeScore":false,
111-
},
112-
"search": [Function],
113-
}
114-
`;

‎playground/index.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ import { countries } from "./countries";
55

66
functionperformSearch(){
77
constquery=document
8-
.getElementById("searchInput")!
9-
//@ts-ignore
10-
.value!.toLowerCase();
11-
constfuzzy=newFuzzy(countries,{includeMatches:true,includeScore:true});
8+
.getElementById("searchInput")!.value;
9+
constfuzzy=newFuzzy(countries,{includeMatches:true,includeScore:true});
1210
constresults=fuzzy.search(query);
1311
console.debug("RESULTS = ",results);
1412
displayResults(results);

‎src/Fuzzy.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ interface Options {
1313
* Whether to include the score in the result.
1414
*/
1515
includeScore?:boolean;
16+
/**
17+
* Whether the search is case-sensitive.
18+
*/
19+
caseSensitive?:boolean;
1620
}
1721

1822
exporttypeSingleResult={
@@ -50,6 +54,7 @@ class Fuzzy {
5054
this.options=options||{
5155
includeMatches:false,
5256
includeScore:false,
57+
caseSensitive:false,
5358
};
5459
}
5560

@@ -60,15 +65,17 @@ class Fuzzy {
6065
*/
6166
publicsearch=(query:string):Result=>{
6267
constresult:(SingleResult&{score:number})[]=[];
68+
constupdateQuery=this.options.caseSensitive ?query :query.toLowerCase();
6369
for(leti=0;i<this.list.length;i++){
70+
constitem=this.options.caseSensitive ?this.list[i] :this.list[i].toLowerCase();
6471
constmatrix=levenshteinFullMatrixSearch(
65-
query.toLowerCase(),
66-
this.list[i].toLowerCase()
72+
updateQuery,
73+
item,
6774
);
6875
constmatches=getMatchingIndices(
6976
matrix,
70-
query.toLowerCase(),
71-
this.list[i].toLowerCase()
77+
updateQuery,
78+
item
7279
);
7380
consttarget=this.list[i];
7481
constdistance=matrix[query.length][target.length];

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp