ArrayFilters.js

angular.module('homepage').
// ARRAY filters
filter('reverse', function() {
	return function(items) {
		return items.slice().reverse();
	};
}).
filter('arr2str', function() {
	return function(items) {
		var string = '';
		for(var index in items) {
			var item = items[index];
			if (index !== "0") string += ', ';
			if (item !== undefined && item.length !== 0 && item !== '') {
				string += item;
			}
		}
		return string;
	};
}).
filter('arraysum', function() {
	return function(items, attribute) {
		var sum = 0;
		for(var index in items) {
			var item = items[index];
			if (item !== undefined && item[attribute.name] !== undefined) {
				var value = parseInt(item[attribute.name]);
				if(typeof value  === "number") {
					sum += value;
				}
			}
		}
		return sum;
	};
}).
filter('filterByValue', function () {
	/**
	 * Returns items by type and excludes if type is EXCL
	 * name: Name of the attribute whose value is to be compared
	 * type: INCL/INCLUDE or EXCL/EXCLUDE value whose value matches
	 * value: value of the attribute
	 * { name: String, type: String, value: String}
	 */
	 return function (items, typeinfo) {
	 	var filtered = [];
	 	for (var index in items) {
	 		var item = items[index];
	 		if(typeinfo.type == 'INCL' || typeinfo.type == 'INCLUDE') {
	 			if(item[typeinfo.name] === typeinfo.value) {
	 				filtered.push(items[index]);
	 			}
	 		}
	 		// Default type is 'EXCL' or 'EXCLUDE'
	 		else {
	 			if(item[typeinfo.name] !== typeinfo.value) {
	 				filtered.push(items[index]);
	 			}
	 		}
	 	}
	 	return filtered;
	};
}).
filter('filterByIndex', function () {
	/**
	 * Returns items by type and excludes if exclude is set to 'true'
	 * { exclude: Boolean, index: Number }
	 */
	 return function (items, indexinfo) {
	 	var filtered = [];
	 	// Exclude item at index mentioned from the returned list
	 	for (var index in items) {
	 		if(indexinfo.exclude === true) {
		 		if(index != indexinfo.index) {
		 			filtered.push(items[index]);
		 		}
		 	}
		 	else {
		 		if (index === indexinfo.index) {
		 			filtered.push(items[index]);
		 		}
		 	}
	 	}

	 	return filtered;
	};
});