-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
134 lines (119 loc) · 3.17 KB
/
app.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
async function getRedditData() {
const response = await axios.get(
'https://www.reddit.com/r/javascript/hot.json?limit=100'
);
const titles = response.data.children.map(curr => {
return curr.data.title;
});
return titles;
}
function fuzzy_match(text, search) {
/**
* @param {text} Its all the results from API call
* @param {search} Its the search term, which we are searching for
*
* @description Seperate the search term
* [x] Seperate the search term
* [x] Set status to false
* [x] Check for each search word if its there in text
*
*/
if (search.length === 0) return true;
const searchList = search.split(' ');
console.log('searchList: ', searchList);
let status = false;
for (let i = 0; i < searchList.length; i++) {
const searchWord = searchList[i].toLowerCase();
if (text.split(new RegExp(searchWord, 'gi')).length > 1) {
status = true;
break;
}
}
return status;
}
function fuzzy_match_ii(text, search) {
/**
* @param {text} Its all the results from API call
* @param {search} Its the search term, which we are searching for
*
* @description Seperate the search term
* [x] Seperate the search term
* [x] Set status to false
* [x] Check for each search word if its there in text
*
*/
let score = 0;
if (search.length === 0)
return {
score,
status: true
};
const searchList = search.trim().split(' ');
console.log('searchList: ', searchList);
let status = false;
for (let i = 0; i < searchList.length; i++) {
const searchWord = searchList[i];
score += text.split(new RegExp(searchWord, 'gi')).length - 1;
if (score > 0) {
status = true;
}
}
return {
status,
score
};
}
const app = new Vue({
el: '#app',
mounted() {
getRedditData().then(res => {
this.results = res;
this.resultsReadOnly = res;
});
},
data: {
title: 'Fuzzy Search',
results: [],
resultsReadOnly: [],
keyword: ''
},
watch: {
keyword: function(val) {
let filteredResultsWithScore;
if (val.length === 0) {
filteredResultsWithScore = this.resultsReadOnly;
} else {
filteredResultsWithScore = this.resultsReadOnly
.map(result => {
const time_start = performance.now();
const { status, score } = fuzzy_match_ii(result, val);
console.log('Time: ', performance.now() - time_start);
return {
result,
score,
status
};
})
.filter(res => {
const { status } = res;
return status;
})
.sort((a, b) => {
return a.score < b.score ? 1 : -1;
})
.map(ans => {
return ans.result;
});
}
console.log('Final output: ', filteredResultsWithScore);
this.results = filteredResultsWithScore;
}
// keyword: function(val) {
// const filteredResults = this.resultsReadOnly.filter(result => {
// return fuzzy_match(result, val);
// });
// console.log('Answer: ', filteredResults);
// this.results = filteredResults;
// }
}
});