Skip to content

Instantly share code, notes, and snippets.

@GabrielShaad
Last active October 2, 2018 13:06
Show Gist options
  • Save GabrielShaad/1edf802963e3df12a60128187ef4227d to your computer and use it in GitHub Desktop.
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.
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