-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
33 lines (28 loc) · 1.01 KB
/
Copy pathindex.ts
File metadata and controls
33 lines (28 loc) · 1.01 KB
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
import { disassemble } from 'hangul-js'
const getChars = (keyword: string) => {
const chars: Record<string, number> = {}
disassemble(keyword).map(
(char) =>
(chars[char] = typeof chars[char] !== 'undefined' ? chars[char] + 1 : 1)
)
return chars
}
export const createSearch = (items: string[]) => {
const itemCounts = items.map((item) => getChars(item))
const search = (keyword: string) => {
const keywordCounts = getChars(disassemble(keyword).join(''))
const founds: [string, number][] = []
itemCounts.map((itemCount, index) => {
let matchCount = 0
Object.entries(itemCount).map(([itemKey, itemValue]) => {
Object.entries(keywordCounts).map(([keywordKey, keywordValue]) => {
if (itemKey === keywordKey && itemValue >= keywordValue)
matchCount += keywordValue
})
})
if (matchCount !== 0) founds.push([items[index], matchCount])
})
return founds.sort((min, max) => max[1] - min[1]).map((found) => found[0])
}
return search
}