Skip to content

Instantly share code, notes, and snippets.

@adamgavlak
Last active June 30, 2016 17:22
Show Gist options
  • Save adamgavlak/80464e32322fd47521ed98ba6333dc2a to your computer and use it in GitHub Desktop.
Save adamgavlak/80464e32322fd47521ed98ba6333dc2a to your computer and use it in GitHub Desktop.
Filter input using attribute
// Takes argument and makes sure that filtered object has attribute with that name set to true
app.filter('attribute', function() {
return function(input, argument) {
var output = [];
if (argument == '') {
return input;
}
for (i in input) {
if (input[i][argument] == true) {
output.push(input[i]);
}
}
return output;
}
});
// Multiple arguments can be passed in string, then they are parsed and checked for true/false (if attribute has '!' before its name then it's checked for false)
// Example: "archived !spam"
app.filter('attribute', function() {
return function(input, arguments, logic) {
var output = [];
var parsed_arguments = arguments.split(" ");
logic = logic || 'and';
if (arguments == '') {
return input;
}
for (i in input) {
var allowed_count = 0;
for (j in parsed_arguments) {
if (parsed_arguments[j].charAt(0) === '!') {
var argument = parsed_arguments[j].replace('!', '');
if (input[i][argument] != false) {
allowed_count++;
}
}
else {
if (input[i][parsed_arguments[j]] != true) {
allowed_count++;
}
}
}
if (allowed_count === 0 && logic === 'and') {
output.push(input[i]);
} else if (allowed_count <= 1 && logic === 'or') {
output.push(input[i]);
}
}
return output;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment