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("");
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= [
{label: "2004", y:4},
{label: "2005", y:99},
@ -20,26 +28,39 @@ const jsonMockViewsByYears= [
{label: "2011", y:1666},
]
function inputKeyUp() {
let filter, ul, li, a, txtValue;
filter = input.value.toUpperCase();
if (document.getElementById("myUL") != null) {
ul = document.getElementById("myUL");
li = ul.getElementsByTagName("li");
// Loop through all list items, and hide those who don't match the search query
for (let i = 0; i < li.length; i++) {
a = li[i].getElementsByTagName("a")[0];
txtValue = a.textContent || a.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
function searchInList(list, searchInput){
if (searchInput === "")
return list
let retList =[]
for (let i = 0; i < list.length; i++) {
console.log(list[i] + " et " + searchInput + " vaut " + lDistance(list[i], searchInput) )
if (lDistance(list[i], searchInput) < 6){
retList.push(list[i])
}
}
return retList
}
function lDistance(s,t){
if (!s.length) return t.length;
if (!t.length) return s.length;
const arr = [];
for (let i = 0; i <= t.length; i++) {
arr[i] = [i];
for (let j = 1; j <= s.length; j++) {
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({
backgroundColor:null,
theme: "light2",
@ -113,23 +134,9 @@ function update(){
</div>
</div>
<div id="articles">
<input
type="text"
id="search-input"
@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>
<input type="text" id="search-input" placeholder="search articles" v-model="input"/>
<ul id="articlesUL">
<li v-for="n in searchInList(articleList,input)">{{n}}</li>
</ul>
</div>
</div>
@ -189,7 +196,6 @@ function update(){
}
#articles {
background-color: orange;
}
#search-input {
@ -200,8 +206,9 @@ function update(){
margin-bottom: 12px;
}
#myUL {
#articlesUL {
list-style-type: none;
color: white;
padding: 0;
margin: 0;
}