/
example.ts
160 lines (148 loc) · 3.04 KB
/
example.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { FurryIndex } from "./dist";
type BusLine = {
routeShortName: string;
agencyId: number;
cities: string[];
};
type TransitAgency = {
agencyId: number;
agencyName: string;
};
// some example data
const agencies: Record<number, TransitAgency> = {
1: {
agencyId: 1,
agencyName: "Egged",
},
2: {
agencyId: 2,
agencyName: "Dan",
},
3: {
agencyId: 3,
agencyName: "Metropoline",
},
};
const lines: BusLine[] = [
{
routeShortName: "74",
agencyId: 1,
cities: ["Tel Aviv - Yafo", "Rishon LeZion", "Azor"],
},
{
routeShortName: "201",
agencyId: 1,
cities: [
"Tel Aviv - Yafo",
"Azor",
"Rishon LeZion",
"Rehovot",
"Nes Ziona",
],
},
{
routeShortName: "1",
agencyId: 2,
cities: [
"Bat Yam",
"Tel Aviv - Yafo",
"Ramat Gan",
"Bney Brak",
"Petah Tikva",
],
},
{
routeShortName: "25",
agencyId: 2,
cities: ["Holon", "Bat Yam", "Tel Aviv - Yafo"],
},
{
routeShortName: "24",
agencyId: 3,
cities: ["Ramat HaSharon", "Tel Aviv - Yafo"],
},
];
// create an index to search through
const searchIndex = new FurryIndex<BusLine>(
// the data to search through
lines,
// the fields we allow searching through, and their weight in scoring
[
{
get: (line) => line.routeShortName,
weight: 1,
},
{
get: (line) => agencies[line.agencyId]?.agencyName ?? "",
weight: 0.1,
},
{
get: (line) => line.cities,
weight: 0.1,
},
],
// how to sort search results
(a, b) => {
if (a.score === b.score) {
return a.obj.routeShortName.localeCompare(b.obj.routeShortName);
} else {
return a.score - b.score;
}
},
);
// run a search:
const result = searchIndex.search(
// we don't have any cities with "Ramot" in the name,
// but we DO have cities with "Ramat" in the name!
["Ramot"],
// this threshold value was reached through trial and error sorry :<
0.35,
// don't return things that don't match
false,
);
console.log(result);
// value of result:
[
{
furrySearchResult: true,
isMatch: true,
idx: 2,
obj: {
routeShortName: "1",
agencyId: 2,
cities: [
"Bat Yam",
"Tel Aviv - Yafo",
"Ramat Gan",
"Bney Brak",
"Petah Tikva",
],
},
score: 0.8744852722211678,
matches: [
[], // no matches in first field (routeShortName)
[], // no matches in second field (agencyName)
[
// matches in third field (cities)
null, // no matches in first item ("Bat Yam")
null, // no matches in second item ("Tel Aviv - Yafo")
[
// matches in third item ("Ramat Gan")
[0, 3], // characters 0 through 3 ("Rama")
],
],
],
},
{
furrySearchResult: true,
isMatch: true,
idx: 4,
obj: {
routeShortName: "24",
agencyId: 3,
cities: ["Ramat HaSharon", "Tel Aviv - Yafo"],
},
score: 0.8744852722211678,
matches: [[], [], [[0, 3]]],
},
];