Skip to content

Commit 3800f85

Browse files
committed
Merge branch 'release/0.6.0'
fix #7
2 parents cfe9c1c + c979379 commit 3800f85

File tree

7 files changed

+102
-10
lines changed

7 files changed

+102
-10
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
<a name"0.6.0"></a>
2+
## 0.6.0 (2016-03-06)
3+
4+
5+
#### Bug Fixes
6+
7+
* **bower.json:** fix bower main script config ([884884ee](https://github.com/the-darc/angular-br-filters/commit/884884ee))
8+
9+
10+
#### Features
11+
12+
* **age:** add filter 'age' to calculate the age based on the birthdate ([0884d523](https://github.com/the-darc/angular-br-filters/commit/0884d523))
13+
14+
115
<a name"0.5.0"></a>
216
## 0.5.0 (2015-09-01)
317

bower.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"name": "angular-br-filters",
3-
"version": "0.5.0",
3+
"version": "0.6.0",
44
"homepage": "https://github.com/the-darc/angular-br-filters",
55
"description": "An Angular library of masks applicable to several Brazilian data.",
66
"authors": [
77
"Igor Rafael <[email protected]>",
88
"Daniel Campos <[email protected]>"
99
],
10-
"main": "src/filters.js",
10+
"main": "release/angular-br-filters.js",
1111
"keywords": [
1212
"angular",
1313
"filters",
@@ -25,6 +25,6 @@
2525
"tests"
2626
],
2727
"dependencies": {
28-
"br-masks": "~0.4.0"
28+
"br-masks": "~0.4.1"
2929
}
3030
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-br-filters",
3-
"version": "0.5.0",
3+
"version": "0.6.0",
44
"description": "An Angular library of masks applicable to several Brazilian data.",
55
"main": "src/filters.js",
66
"scripts": {

release/angular-br-filters.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* angular-br-filters
33
* An Angular library of masks applicable to several Brazilian data.
4-
* @version v0.5.0
4+
* @version v0.6.0
55
* @link https://github.com/the-darc/angular-br-filters
66
* @license MIT
77
*/(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
@@ -459,7 +459,7 @@ m.filter('percentage', ['$filter', function($filter) {
459459
thousandsDelimiter = $locale.NUMBER_FORMATS.GROUP_SEP,
460460
currencySym = '';
461461

462-
if(currency === true) {
462+
if (currency === true) {
463463
currencySym = $locale.NUMBER_FORMATS.CURRENCY_SYM + ' ';
464464
} else if (currency) {
465465
currencySym = currency;
@@ -472,6 +472,29 @@ m.filter('percentage', ['$filter', function($filter) {
472472
return function(input) {
473473
return BrM.nfeAccessKey(input);
474474
};
475-
}]);
475+
}])
476+
.filter('age', function() {
477+
/**
478+
* @param value birthdate can be a date object or a time in milliseconds
479+
* return the age based on the birthdate or undefined if value is invalid.
480+
*/
481+
return function calculateAge(value) {
482+
if (!value) {
483+
return undefined;
484+
}
485+
var isDateInstance = (value instanceof Date);
486+
var isValidType = isDateInstance || !isNaN(parseInt(value));
487+
if (!isValidType) {
488+
return undefined;
489+
}
490+
var birthdate = isDateInstance ? value : new Date(value);
491+
if (birthdate > new Date()) {
492+
return undefined;
493+
}
494+
var ageDifMs = Date.now() - birthdate.getTime();
495+
var ageDate = new Date(ageDifMs); // miliseconds from epoch
496+
return Math.abs(ageDate.getUTCFullYear() - 1970);
497+
};
498+
});
476499

477500
},{"br-masks":2}]},{},[3]);

release/angular-br-filters.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/filters.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ m.filter('percentage', ['$filter', function($filter) {
5353
thousandsDelimiter = $locale.NUMBER_FORMATS.GROUP_SEP,
5454
currencySym = '';
5555

56-
if(currency === true) {
56+
if (currency === true) {
5757
currencySym = $locale.NUMBER_FORMATS.CURRENCY_SYM + ' ';
5858
} else if (currency) {
5959
currencySym = currency;
@@ -66,4 +66,27 @@ m.filter('percentage', ['$filter', function($filter) {
6666
return function(input) {
6767
return BrM.nfeAccessKey(input);
6868
};
69-
}]);
69+
}])
70+
.filter('age', function() {
71+
/**
72+
* @param value birthdate can be a date object or a time in milliseconds
73+
* return the age based on the birthdate or undefined if value is invalid.
74+
*/
75+
return function calculateAge(value) {
76+
if (!value) {
77+
return undefined;
78+
}
79+
var isDateInstance = (value instanceof Date);
80+
var isValidType = isDateInstance || !isNaN(parseInt(value));
81+
if (!isValidType) {
82+
return undefined;
83+
}
84+
var birthdate = isDateInstance ? value : new Date(value);
85+
if (birthdate > new Date()) {
86+
return undefined;
87+
}
88+
var ageDifMs = Date.now() - birthdate.getTime();
89+
var ageDate = new Date(ageDifMs); // miliseconds from epoch
90+
return Math.abs(ageDate.getUTCFullYear() - 1970);
91+
};
92+
});

src/filters.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,36 @@ describe('br-filters', function() {
142142
.toBe('3514 0111 7242 5800 0157 5500 1000 6882 1916 3038 6000');
143143
});
144144
});
145+
describe('age', function() {
146+
it('should be undefined if null, undefined or empty', function() {
147+
expect(testFilter('age')(null)).toBe(undefined);
148+
expect(testFilter('age')(undefined)).toBe(undefined);
149+
expect(testFilter('age')('')).toBe(undefined);
150+
});
151+
152+
it('should be undefined if not date or time in milliseconds', function() {
153+
expect(testFilter('age')('not a date')).toBe(undefined);
154+
expect(testFilter('age')(true)).toBe(undefined);
155+
});
156+
157+
it('should be undefined for future birthdate', function() {
158+
var futureYear = new Date();
159+
futureYear.setFullYear(futureYear.getFullYear() + 1);
160+
expect(testFilter('age')(futureYear)).toBe(undefined);
161+
var futureMonth = new Date();
162+
futureMonth.setMonth(futureMonth.getMonth() + 1);
163+
expect(testFilter('age')(futureMonth)).toBe(undefined);
164+
var futureDay = new Date();
165+
futureDay.setDate(futureDay.getDate() + 1);
166+
expect(testFilter('age')(futureDay)).toBe(undefined);
167+
var futureMinute = new Date();
168+
futureMinute.setMinutes(futureMinute.getMinutes() + 1);
169+
expect(testFilter('age')(futureMinute)).toBe(undefined);
170+
});
171+
172+
it('should format date as 27', function() {
173+
expect(testFilter('age')(new Date(1988, 10, 07))).toBe(27);
174+
expect(testFilter('age')(597463200000)).toBe(27);
175+
});
176+
});
145177
});

0 commit comments

Comments
 (0)