Last active
October 2, 2018 13:06
-
-
Save GabrielShaad/1edf802963e3df12a60128187ef4227d to your computer and use it in GitHub Desktop.
Filter list of objects to match normalized string and return an array of matches.
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 _normalizeString(value) { | |
return value | |
.toString() | |
.replace(/á|ã|â/g, 'a') | |
.replace(/é|ê/g, 'e') | |
.replace(/í/g, 'i') | |
.replace(/ó|ô|õ/g, 'o') | |
.replace(/ú/g, 'u') | |
.replace(/ç/g, 'c') | |
.replace(/_/g, ''); | |
} | |
function _filterListByString(searchString, list) { | |
return list.filter(function(item) { | |
return Object.values(item).some(function(property) { | |
var itemAttribute = _normalizeString( | |
property | |
.toString() | |
.toLowerCase() | |
.split(' ') | |
.join('') | |
); | |
return itemAttribute.includes( | |
_normalizeString( | |
searchString | |
.toString() | |
.toLowerCase() | |
.split(' ') | |
.join('') | |
); | |
); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment