-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
43 lines (41 loc) · 1.13 KB
/
index.js
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
/**
* search-index module.
* @module term-vector
*/
module.exports = (tokens, ops) => {
ops = Object.assign(
{},
{
ngramLengths: [1]
},
ops
)
const tokenMap = tokens.reduce((acc, cur, i, src) => {
// register ngram of each given length
ops.ngramLengths.forEach((ngl) => {
const ngram = src.slice(i, i + ngl)
if (ngram.length !== ngl) return // at end of token array
cur = JSON.stringify(ngram)
acc[cur] = acc[cur] || []
acc[cur].push(i)
})
return acc
}, {})
// turn token map into array
return Object.keys(tokenMap)
.map((t) => ({
term: JSON.parse(t),
positions: tokenMap[t]
}))
.sort((a, b) => {
for (let i = 0; i < a.term.length && i < b.term.length; i++) {
if (a.term[i] === b.term[i]) {
// these elements are equal, move to next element
} else {
// these elements are not equal so compare them
return a.term[i].localeCompare(b.term[i])
}
}
return 0 // since nothing was returned, both arrays are deeply equal
}) // sort array of variable length arrays alphabetically
}