A collection of useful filters for AngularJS.
The included filters are:
You can install the latest version of angular-filters
with bower
:
$ bower install angular-filters
You can also fetch the git repo or download a zip of the latest tag.
Then, simply include ./build/angular-filters.js
or ./build/angular-filters.min.js
in your Web app and inject the module frapontillo.ex.filters
in your application.
The bool
filter allows to specify true and false values to show depending on the input value. The second parameter will be returned if and only if the first parameter is true
; the third parameter will be returned if and only if the first parameter is false
.
This filter can be used to print a specific message depending on a boolean value.
Use it as follows:
<p>{{ someBoolValue | bool:'Candies!':'No candies :(' }}</p>
$scope.returnValue = $filter('bool')($scope.someBoolValue, 'Candies!', 'No candies :(');
The default
filter allows to specify a default fallback value if an object is one of the following:
null
undefined
- empty string,
''
Please notice that if a value equals to 0
, the default value won't be returned, as 0
is accepted.
This filter is useful when another filter return is not safe and when you want to display a fallback value.
Use it as follows:
<p>{{ someValue | number:2 | default:'No value is available.' }}</p>
$scope.returnValue = $filter('default')
($filter('number')($scope.someValue, 2), 'No value is available.');
The firstNotNull
filter returns the first element from an array that is neither null
or undefined
. This means it returns all numbers and strings, even if empty. It returns undefined
if all values aren't set or if the array is empty.
Use it as follows:
<p>{{ myValues | firstNotNull }}</p>
$scope.firstValue = $filter('firstNotNull')($scope.myValues);
The lastNotNull
filter returns the last element from an array that is neither null
or undefined
. This means it returns all numbers and strings, even if empty. It returns undefined
if all values aren't set or if the array is empty.
Use it as follows:
<p>{{ myValues | lastNotNull }}</p>
$scope.firstValue = $filter('lastNotNull')($scope.myValues);
The max
filter returns the maximum value from an array that is neither null
or undefined
. It returns undefined
if all values aren't set or if the array is empty.
Use it as follows:
<p>{{ myValues | max }}</p>
$scope.maxValue = $filter('max')($scope.myValues);
The min
filter returns the minimum value from an array that is neither null
or undefined
. It returns undefined
if all values aren't set or if the array is empty.
Use it as follows:
<p>{{ myValues | min }}</p>
$scope.minValue = $filter('min')($scope.myValues);
- Main module renamed to
frapontillo.ex.filters
in order to adhere with the Angular Component Specification draft. - Added
bool
filter. - Upgraded bower information, node packages and Karma test runner.
- Added
firstNotNull
,lastNotNull
,max
,min
. - Test set complete.
- TravisCI is working.
- First release.
default
filter is the only filter at the moment.- Unit testing with grunt, testacular and gruntacular configured.
defaultSpec
test written.
To test and build the distribution files yourself, do the following:
npm install -g grunt-cli karma bower
npm install
bower install
grunt
To simply test, use the karma:local
task. To build without testing first, use the build
task.
If you want to test after every file change, use the karma:dev
grunt task instead.
The MIT License
Copyright (c) 2013 Francesco Pontillo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.