Last active
December 30, 2015 10:09
-
-
Save sillero/7813748 to your computer and use it in GitHub Desktop.
String search + unlatinize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"terms": { | |
"search": "tração", | |
"text": "Concentração prévia tração centrado" | |
}, | |
"found": true, | |
"result": [ | |
{ | |
"str": "Concen", | |
"isMatch": false | |
}, | |
{ | |
"str": "tração", | |
"isMatch": true | |
}, | |
{ | |
"str": " prévia ", | |
"isMatch": false | |
}, | |
{ | |
"str": "tração", | |
"isMatch": true | |
}, | |
{ | |
"str": " centrado", | |
"isMatch": false | |
} | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(window, document, undefined){ | |
var strSearch = function() { | |
var originalSearch = 'tração', | |
search = unlatinize(originalSearch), | |
originalText = 'Concentração prévia tração centrado', | |
text = unlatinize(originalText), | |
searchResult = { | |
terms: { | |
search: originalSearch, | |
text: originalText | |
}, | |
found: !!~text.indexOf(search), | |
result: [] //{str: '...', match: true|false} | |
}, | |
findMatch = function(lastpos){ | |
var result = text.split(search); | |
result.forEach(function(item, i, arr){ | |
searchResult.result.push({ | |
str: originalText.substr(arr.slice(0,i).join('').length + i*search.length, item.length), | |
isMatch: false | |
}); | |
if (i < arr.length-1) { | |
searchResult.result.push({ | |
str: originalSearch, | |
isMatch: true | |
}); | |
} | |
}); | |
return searchResult; | |
}; | |
log(findMatch()); | |
}, | |
log = function(result){ | |
if (document.getElementById('log')) { | |
document.getElementById('log').textContent = JSON.stringify(result, false, ' '); | |
} | |
console.log(result); | |
}, | |
unlatinize = function(text){ | |
var k, l, | |
maps = [ | |
[/[àáâãäå]/g, 'a'], | |
[/[ç]/g, 'c'], | |
[/[èéêë]/g, 'e'], | |
[/[ìíîï]/g, 'i'], | |
[/[ñ]/g, 'n'], | |
[/[òóôõö]/g, 'o'], | |
[/[ùúûü]/g, 'u'] | |
]; | |
for (k = 0, l = maps.length; k < l; k++) { | |
text = text.replace(maps[k][0], maps[k][1]); | |
} | |
return text; | |
}; | |
strSearch(); | |
})(window, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment