Skip to content

Commit f09ca45

Browse files
authored
Merge pull request #2564 from StoDevX/modularize-search
Modularize keywordSearch function
2 parents a34848c + c4d8918 commit f09ca45

File tree

3 files changed

+12
-18
lines changed

3 files changed

+12
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@
102102
"events": "2.0.0",
103103
"get-urls": "7.2.0",
104104
"glamorous-native": "1.4.0",
105-
"hamming": "0.0.2",
106105
"html-entities": "1.2.1",
107106
"htmlparser2": "3.9.2",
107+
"keyword-search": "0.1.1",
108108
"lodash": "4.17.9",
109109
"moment": "2.22.1",
110110
"moment-timezone": "0.5.16",

source/views/sis/course-search/search.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {RecentItemsList} from '../components/recent-search/list'
3030
import {Separator} from '../../components/separator'
3131
import {buildFilters} from './lib/build-filters'
3232
import type {FilterComboType} from './lib/format-filter-combo'
33-
import hamming from 'hamming'
33+
import keywordSearch from 'keyword-search'
3434

3535
const PROMPT_TEXT =
3636
'We need to download the courses from the server. This will take a few seconds.'
@@ -72,18 +72,6 @@ type State = {
7272
query: string,
7373
}
7474

75-
function keywordSearch(query: string, item: string) {
76-
const keywords = query.split(' ')
77-
const itemWords = item.split(' ')
78-
return keywords.every(keyword =>
79-
itemWords.some(
80-
word =>
81-
keyword.length <= word.length &&
82-
hamming(keyword, word.slice(0, keyword.length)) < 2,
83-
),
84-
)
85-
}
86-
8775
function executeSearch(args: {
8876
text: string,
8977
filters: FilterType[],
@@ -100,10 +88,10 @@ function executeSearch(args: {
10088

10189
const results = filteredCourses.filter(
10290
course =>
103-
keywordSearch(query, course.name.toLowerCase()) ||
104-
keywordSearch(query, (course.title || '').toLowerCase()) ||
91+
keywordSearch(query, course.name.toLowerCase(), 1) ||
92+
keywordSearch(query, (course.title || '').toLowerCase(), 1) ||
10593
(course.instructors || []).some(name =>
106-
keywordSearch(query, name.toLowerCase()),
94+
keywordSearch(query, name.toLowerCase(), 1),
10795
) ||
10896
deptNum(course)
10997
.toLowerCase()

yarn.lock

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3880,6 +3880,12 @@ [email protected]:
38803880
version "2.1.0"
38813881
resolved "https://registry.yarnpkg.com/junk/-/junk-2.1.0.tgz#f431b4b7f072dc500a5f10ce7f4ec71930e70134"
38823882

3883+
3884+
version "0.1.1"
3885+
resolved "https://registry.yarnpkg.com/keyword-search/-/keyword-search-0.1.1.tgz#f7615e739024de410a92dd0071ff20b8ae01956f"
3886+
dependencies:
3887+
hamming "0.0.2"
3888+
38833889
kind-of@^3.0.2:
38843890
version "3.2.2"
38853891
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64"
@@ -6394,7 +6400,7 @@ vinyl@^0.5.0:
63946400
clone-stats "^0.0.1"
63956401
replace-ext "0.0.1"
63966402

6397-
"vm2@github:patriksimek/vm2#custom_files":
6403+
vm2@patriksimek/vm2#custom_files:
63986404
version "3.5.0"
63996405
resolved "https://codeload.github.com/patriksimek/vm2/tar.gz/7e82f90ac705fc44fad044147cb0df09b4c79a57"
64006406

0 commit comments

Comments
 (0)