1
0
forked from PGL/Clyde

added levenshtein distance to search input

This commit is contained in:
Bartha Maxime 2024-04-07 15:51:53 +02:00
parent ec2b975467
commit d31547c4cc

View File

@ -12,6 +12,14 @@ const statsOf = ref("");
const statsBy = ref(""); const statsBy = ref("");
let chart; let chart;
const articleList = [
"First Article",
"The composition of the light",
"The composition of the sand",
"Fluctuation in universe beavers",
"The Potato War"
]
const jsonMockViewsByYears= [ const jsonMockViewsByYears= [
{label: "2004", y:4}, {label: "2004", y:4},
{label: "2005", y:99}, {label: "2005", y:99},
@ -20,25 +28,38 @@ const jsonMockViewsByYears= [
{label: "2011", y:1666}, {label: "2011", y:1666},
] ]
function inputKeyUp() { function searchInList(list, searchInput){
let filter, ul, li, a, txtValue; if (searchInput === "")
filter = input.value.toUpperCase(); return list
if (document.getElementById("myUL") != null) { let retList =[]
ul = document.getElementById("myUL"); for (let i = 0; i < list.length; i++) {
li = ul.getElementsByTagName("li"); console.log(list[i] + " et " + searchInput + " vaut " + lDistance(list[i], searchInput) )
if (lDistance(list[i], searchInput) < 6){
retList.push(list[i])
}
}
return retList
}
// Loop through all list items, and hide those who don't match the search query function lDistance(s,t){
for (let i = 0; i < li.length; i++) { if (!s.length) return t.length;
a = li[i].getElementsByTagName("a")[0]; if (!t.length) return s.length;
txtValue = a.textContent || a.innerText; const arr = [];
if (txtValue.toUpperCase().indexOf(filter) > -1) { for (let i = 0; i <= t.length; i++) {
li[i].style.display = ""; arr[i] = [i];
} else { for (let j = 1; j <= s.length; j++) {
li[i].style.display = "none"; arr[i][j] =
} i === 0
} ? j
: Math.min(
arr[i - 1][j] + 1,
arr[i][j - 1] + 1,
arr[i - 1][j - 1] + (s[j - 1] === t[i - 1] ? 0 : 1)
);
} }
} }
return arr[t.length][s.length];
};
const options = reactive({ const options = reactive({
backgroundColor:null, backgroundColor:null,
@ -113,23 +134,9 @@ function update(){
</div> </div>
</div> </div>
<div id="articles"> <div id="articles">
<input <input type="text" id="search-input" placeholder="search articles" v-model="input"/>
type="text" <ul id="articlesUL">
id="search-input" <li v-for="n in searchInList(articleList,input)">{{n}}</li>
@keyup="inputKeyUp()"
placeholder="search articles"
v-model="input"
/>
<ul id="myUL">
<li><a href="#">Adele</a></li>
<li><a href="#">Agnes</a></li>
<li><a href="#">Billy</a></li>
<li><a href="#">Bob</a></li>
<li><a href="#">Calvin</a></li>
<li><a href="#">Christina</a></li>
<li><a href="#">Cindy</a></li>
</ul> </ul>
</div> </div>
</div> </div>
@ -189,7 +196,6 @@ function update(){
} }
#articles { #articles {
background-color: orange;
} }
#search-input { #search-input {
@ -200,8 +206,9 @@ function update(){
margin-bottom: 12px; margin-bottom: 12px;
} }
#myUL { #articlesUL {
list-style-type: none; list-style-type: none;
color: white;
padding: 0; padding: 0;
margin: 0; margin: 0;
} }