Created
September 26, 2014 13:13
-
-
Save gajus/543fc05c544d8bcd7895 to your computer and use it in GitHub Desktop.
This file contains 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
angular | |
.module('customFilters', []) | |
.filter('unique', function () { | |
return function (data, propertyName) { | |
if (angular.isArray(data) && angular.isString(propertyName)) { | |
var results = []; | |
var keys = {}; | |
for (var i = 0; i < data.length; i++) { | |
var val = data[i][propertyName]; | |
if (angular.isUndefined(keys[val])) { | |
keys[val] = true; | |
results.push(val); | |
} | |
} | |
return results; | |
} else { | |
return data; | |
} | |
}; | |
}) | |
.filter('range', function ($filter) { | |
return function (data, page, size) { | |
if (angular.isArray(data) && angular.isNumber(page) && angular.isNumber(size)) { | |
var start_index = (page - 1) * size; | |
if (data.length < start_index) { | |
return []; | |
} else { | |
return $filter('limitTo')(data.splice(start_index), size); | |
} | |
} else { | |
return data; | |
} | |
}; | |
}) | |
.filter('pageCount', function () { | |
return function (data, size) { | |
if (angular.isArray(data)) { | |
var result = []; | |
for (var i = 0; i < Math.ceil(data.length / size) ; i++) { | |
result.push(i); | |
} | |
return result; | |
} else { | |
return data; | |
} | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment