self.onmessage = async (event) => {
if (event.data.type === "api") {
const { endpoint, serachValue, selectedDictionary } = event.data
let response
if (serachValue) {
response = await fetch(`${endpoint}/getWords?name=${serachValue}&selecteddictionary=${selectedDictionary}`);
} else {
response = await fetch(`${endpoint}/getData?name=${serachValue}&selecteddictionary=${selectedDictionary}`);
}
const data = await response.json();
self.postMessage({ data });
}
if (event.data.type === "filterwords") {
const { data, serachValue, prefixValue, containsValue, suffixValue, exculdeValue, includeValue, lengthValue, sortingFilter, dictonary } = event.data
let newWordsLength = 0;
for (let i = 15; i > 0; i--) {
let newdata = data.filter((item) => item.length === i);
if (prefixValue) {
newdata = newdata.filter((item2) =>
item2.startsWith(prefixValue.toLowerCase())
);
}
if (containsValue) {
newdata = newdata.filter((item) =>
item.includes(containsValue.toLowerCase())
);
}
if (suffixValue) {
newdata = newdata.filter((item) =>
item.endsWith(suffixValue.toLowerCase())
);
}
if (exculdeValue) {
let data = [];
newdata.map((item) => {
let check = false;
for (let e = 0; e < exculdeValue.length; e++) {
const element = exculdeValue[e].toLowerCase();
if (item.includes(element)) {
check = true;
break;
} else {
check = false;
}
}
if (check === false) {
data.push(item);
}
});
newdata = data;
}
if (includeValue) {
let data = [];
newdata.map((item) => {
let check = false;
for (let e = 0; e < includeValue.length; e++) {
const element = includeValue[e].toLowerCase();
if (!item.includes(element)) {
check = true;
break;
} else {
check = false;
}
}
if (check === false) {
data.push(item);
}
});
newdata = data;
}
if (lengthValue) {
newdata = newdata.filter((item) => item.length == lengthValue);
}
if (newdata.length === 0) {
self.postMessage({
type: "filterwords",
prefixValue: prefixValue,
containsValue: containsValue,
suffixValue: suffixValue,
exculdeValue: exculdeValue,
includeValue: includeValue,
lengthValue: lengthValue,
newdata: newdata,
i: i,
newWordsLength: newWordsLength,
});
} else {
newWordsLength += newdata.length;
newdata = sortWords(serachValue, sortingFilter, newdata, dictonary);
console.log(newdata);
let result = newdata.map((item) => {
let itemHtml = "";
let ScrabbleLetterScore = ScrabbleScore();
let sum = 0
if (serachValue) {
var text1 = serachValue.replace("?", "");
var text2 = item;
var text3 = item;
let chars = text1.split("");
let indexs = [];
chars.map((i) => {
let findIndexes = findIndex(text3, i);
if (findIndexes.length > 0) {
text3 = text3.split("");
text3[findIndexes] = "$";
text3 = text3.join("");
indexs = [...indexs, ...findIndexes];
}
});
let n1 = false
text2.split("").map((itemValue, index) => {
let check = indexs.find((i) => i === index);
if (check !== undefined) {
itemHtml += `${itemValue}`;
n1 = true
} else {
itemHtml += `${itemValue}`;
}
});
if (n1 == true) {
let parsedText = itemHtml.replace(/]*>.*?<\/span>/g, '');
sum = 0
for (let i = 0; i < parsedText.length; i++) {
sum += ScrabbleLetterScore[parsedText[i]] || 0; // for unknown characters
}
}
} else {
itemHtml = item
sum = 0
for (let i = 0; i < itemHtml.length; i++) {
sum += ScrabbleLetterScore[itemHtml[i]] || 0; // for unknown characters
}
}
if (item.length === 1) {
ok = false;
newWordsLength = newWordsLength - 1;
} else {
item = item.toLowerCase();
return `
${itemHtml}
${sum}
`;
}
});
result = result.slice(0, 98)
self.postMessage({
type: "filterwords",
prefixValue: prefixValue,
containsValue: containsValue,
suffixValue: suffixValue,
exculdeValue: exculdeValue,
includeValue: includeValue,
lengthValue: lengthValue,
newdata: newdata,
i: i,
newWordsLength: newWordsLength,
result,
});
}
}
}
};
function sortWords(serachValue, sortingFilter, data, dictonary) {
if (sortingFilter == 0) {
return data;
} else if (sortingFilter == 1) {
return data.sort();
} else if (sortingFilter == 2) {
return data.reverse();
} else if (sortingFilter == 3) {
var tempArr = [];
var newArray = [];
data.map((item) => {
let newWordsLength = 0;
if (item.length === 1) {
ok = false;
newWordsLength = newWordsLength - 1;
} else {
let ScrabbleLetterScore = ScrabbleScore(dictonary);
let points = 0;
item = item.toLowerCase();
let itemHtml = "";
let sum = 0
if (serachValue) {
var text1 = serachValue.replace("?", "");
var text2 = item;
var text3 = item;
let chars = text1.split("");
let indexs = [];
chars.map((i) => {
let findIndexes = findIndex(text3, i);
if (findIndexes.length > 0) {
text3 = text3.split("");
text3[findIndexes] = "$";
text3 = text3.join("");
indexs = [...indexs, ...findIndexes];
}
});
let n1 = false
text2.split("").map((itemValue, index) => {
let check = indexs.find((i) => i === index);
if (check !== undefined) {
itemHtml += `${itemValue}`;
n1 = true
} else {
itemHtml += `${itemValue}`;
}
});
if (n1 == true) {
let parsedText = itemHtml.replace(/]*>.*?<\/span>/g, '');
sum = 0
for (let i = 0; i < parsedText.length; i++) {
sum += ScrabbleLetterScore[parsedText[i]] || 0; // for unknown characters
}
}
}else{
itemHtml = item
sum = 0
for (let i = 0; i < itemHtml.length; i++) {
sum += ScrabbleLetterScore[itemHtml[i]] || 0; // for unknown characters
}
}
const value = {
words: item,
points: sum,
};
newArray.push(value);
}
});
newArray.sort(function (a, b) {
return b.points - a.points;
});
newArray.map((ele) => {
tempArr.push(ele.words);
});
return tempArr;
}
}
// Scrabble Point Array
const ScrabbleScore = (dictonary) => {
let twl06_sowpods = {
a: 1,
e: 1,
i: 1,
o: 1,
u: 1,
l: 1,
n: 1,
r: 1,
s: 1,
t: 1,
d: 2,
g: 2,
b: 3,
c: 3,
m: 3,
p: 3,
f: 4,
h: 4,
v: 4,
w: 4,
y: 4,
k: 5,
j: 8,
x: 8,
q: 10,
z: 10,
};
let wwfScore = {
a: 1,
b: 4,
c: 4,
d: 2,
e: 1,
f: 4,
g: 3,
h: 3,
i: 1,
j: 10,
k: 5,
l: 2,
m: 4,
n: 2,
o: 1,
p: 4,
q: 10,
r: 1,
s: 1,
t: 1,
u: 2,
v: 5,
w: 4,
x: 8,
y: 3,
z: 10,
};
if (dictonary === "wwf") {
return wwfScore;
} else {
return twl06_sowpods;
}
};
function findIndex(str, char) {
const strLength = str.length;
const indexes = [];
let newStr = str;
while (newStr && newStr.indexOf(char) > -1) {
indexes.push(newStr.indexOf(char) + strLength - newStr.length);
newStr = newStr.substring(newStr.indexOf(char) + 5);
newStr = newStr.substring(newStr.indexOf(char) + 5);
}
return indexes;
}