I have the following function
$(function(){
function normalizeWord(word) {
var letters = [{
search: 'áäàãâ',
replace: 'a'
}, {
search: 'éëèê',
replace: 'e'
}, {
search: 'íïìî',
replace: 'i'
}, {
search: 'óöòõô',
replace: 'o'
}, {
search: 'úüùû',
replace: 'u'
}, {
search: 'ñ',
replace: 'n'
}, {
search: 'ç',
replace: 'c'
}],
normal;
// Convertimos la palabra a minusculas
word = word.toLowerCase();
normal = word;
// Por cada "letra"
$.each(letters, function(idx, letter) {
var re = new RegExp('[' + letter.search + ']', 'g');
// Reemplazamos el caracter acentuado
normal = normal.replace(re, letter.replace);
});
// Devolvemos un objeto con la palabra original y la normalizada
return {
original: word,
normal: normal
};
}
function normalizeWords(words) {
var response = [];
// Por cada palabra
$.each(words, function(idx, word) {
// Obtenemos la palabra normlalizada
response.push(normalizeWord(word));
});
return response;
}
function sortNormalizedWords(a, b) {
if (a.normal > b.normal) {
return 1;
}
if (a.normal < b.normal) {
return -1;
}
return 0;
}
function sortShortWords(a, b) {
if (a.length > b.length) {
return 1;
}
if (a.length < b.length) {
return -1;
}
return 0;
}
//
var palabras = ["anatomia","Ana","angie","anatómico","anatómica","análisis","analogía","analizar","anabólico","Analuz", "tu", "tú", "túnel"],
// Obtenemos la palabras normalizadas (sin caracteres acentuados)
words = normalizeWords(palabras);
// Ordenamos el arreglo de la A a la Z
words.sort(sortNormalizedWords);
$( "#entrada").on('input', function() {
var key = this.value,
posibles = [],
lucky = false;
// Normalizamos el valor ingresado
key = normalizeWord(key);
// Por cada palabra
$.each(words, function(idx, word) {
// Validamos que contenga el valor ingresado
if (word.normal.indexOf(key.normal) !== -1) {
posibles.push(word.original);
// Si es exactamente la palabra buscada
if (key.original === word.original) {
lucky = word.original;
}
// Si es igual a la palabras normalizada
else if (!lucky && key.normal === word.normal) {
lucky = word.nomral;
}
}
});
// Imprimimos todas las palabras que contienen el valor ingresado
$('#posibles').val(posibles.join(','));
// Si no encontramos la pablabra exacta,
// ordenamos el arreglo de posibles
// y devolvemos la primera
if (!lucky) {
lucky = posibles.sort(sortShortWords)[0];
}
$('#acertada').val(lucky);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Entrada:
<input type="text" id="entrada"><br>
Posibles:
<input type="text" id="posibles" readonly><br>
Mas acertada:
<input type="text" id="acertada" readonly><br>
My problem is writing with initial caps or the whole word .
That is to say that if I write in Input => Your , The result(suggestion) will be your . Note the initial.
What I want is for it to return Tu in the same way, I thought that the word Tu (Notice the initial) should be in the Array() of words but that makes the function very unstable in the sense that for some words it returns the initial in capital letters and for others no no matter how you write them (lowercase or uppercase).
To summarize, what I want is that if I write Pa it returns Pa and if I write pa it returns pa :)
How can I solve the problem?
By changing this line you make that if you enter a text in upper case the first letter, the value of
#acertada
also.For this:
It takes the first letter and capitalizes it, and leaves the rest in lowercase.