-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
36 lines (29 loc) · 1.11 KB
/
worker.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
import { parentPort } from 'worker_threads';
import { STOP_LETTERS, transformPunctuation } from './peripherals';
export function generateFulltextIndex(inputString = '') {
inputString = transformPunctuation(inputString);
const words = inputString.split(/\s+/).filter(v => v);
const searchArray = new Set();
for (let i = 0; i < words.length; i++) {
let currentTerm = '',
wordCount = 0;
for (let j = i; j < words.length; j++) {
if (!STOP_LETTERS.has(words[j])) ++wordCount;
if (wordCount < 10) {
if (j !== i) currentTerm += ' ';
for (let x = 0; x < words[j].length; x++) {
currentTerm += words[j][x];
if (!searchArray.has(currentTerm)) searchArray.add(currentTerm);
}
} else break;
}
currentTerm = undefined;
wordCount = undefined;
}
return [...searchArray];
}
if (parentPort)
parentPort.on('message', ({ text }) => {
const indexes = generateFulltextIndex(text);
parentPort.postMessage({ indexes });
});