Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Commit

Permalink
fix(dateparser): add type and validity check
Browse files Browse the repository at this point in the history
- Add type and validity check to ensure proper date object is created
- Make code style uniform in scripts
- Add warning if the string is not a valid date

Closes #3701
Closes #3759
Closes #3933
Fixes #3609
Fixes #3713
Fixes #3736
Fixes #3875
Fixes #3937
Fixes #3976
  • Loading branch information
wesleycho committed Jul 31, 2015
1 parent 713c848 commit 4f1e03f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/dateparser/dateparser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
angular.module('ui.bootstrap.dateparser', [])

.service('dateParser', ['$locale', 'orderByFilter', function($locale, orderByFilter) {
.service('dateParser', ['$log', '$locale', 'orderByFilter', function($log, $locale, orderByFilter) {
// Pulled from https://github.com/mbostock/d3/blob/master/src/format/requote.js
var SPECIAL_CHARACTERS_REGEXP = /[\\\^\$\*\+\?\|\[\]\(\)\.\{\}]/g;

Expand Down Expand Up @@ -125,7 +125,7 @@ angular.module('ui.bootstrap.dateparser', [])

if ( results && results.length ) {
var fields, dt;
if (baseDate) {
if (angular.isDate(baseDate) && !isNaN(baseDate.getTime())) {
fields = {
year: baseDate.getFullYear(),
month: baseDate.getMonth(),
Expand All @@ -136,6 +136,9 @@ angular.module('ui.bootstrap.dateparser', [])
milliseconds: baseDate.getMilliseconds()
};
} else {
if (baseDate) {
$log.warn('dateparser:', 'baseDate is not a valid date');
}
fields = { year: 1900, month: 0, date: 1, hours: 0, minutes: 0, seconds: 0, milliseconds: 0 };
}

Expand Down
8 changes: 8 additions & 0 deletions src/dateparser/test/dateparser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ describe('date parser', function () {
expect(dateParser.parse('31-04-2013', 'dd-MM-yyyy')).toBeUndefined();
expect(dateParser.parse('November 31, 2013', 'MMMM d, yyyy')).toBeUndefined();
});

it('should work when base date is a string', function() {
expect(dateParser.parse('01-02-2034', 'dd-MM-yyyy', '05-06-2078')).toEqual(new Date(2034, 1, 1));
});

it('should work when base date is an invalid date', function() {
expect(dateParser.parse('30-12-2015', 'dd-MM-yyyy', new Date('foo'))).toEqual(new Date(2015, 11, 30));
});
});

it('should not parse non-string inputs', function() {
Expand Down

0 comments on commit 4f1e03f

Please sign in to comment.