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

Commit 4f1e03f

Browse files
committedJul 31, 2015
fix(dateparser): add type and validity check
- 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
1 parent 713c848 commit 4f1e03f

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed
 

Diff for: ‎src/dateparser/dateparser.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
angular.module('ui.bootstrap.dateparser', [])
22

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

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

126126
if ( results && results.length ) {
127127
var fields, dt;
128-
if (baseDate) {
128+
if (angular.isDate(baseDate) && !isNaN(baseDate.getTime())) {
129129
fields = {
130130
year: baseDate.getFullYear(),
131131
month: baseDate.getMonth(),
@@ -136,6 +136,9 @@ angular.module('ui.bootstrap.dateparser', [])
136136
milliseconds: baseDate.getMilliseconds()
137137
};
138138
} else {
139+
if (baseDate) {
140+
$log.warn('dateparser:', 'baseDate is not a valid date');
141+
}
139142
fields = { year: 1900, month: 0, date: 1, hours: 0, minutes: 0, seconds: 0, milliseconds: 0 };
140143
}
141144

Diff for: ‎src/dateparser/test/dateparser.spec.js

+8
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ describe('date parser', function () {
162162
expect(dateParser.parse('31-04-2013', 'dd-MM-yyyy')).toBeUndefined();
163163
expect(dateParser.parse('November 31, 2013', 'MMMM d, yyyy')).toBeUndefined();
164164
});
165+
166+
it('should work when base date is a string', function() {
167+
expect(dateParser.parse('01-02-2034', 'dd-MM-yyyy', '05-06-2078')).toEqual(new Date(2034, 1, 1));
168+
});
169+
170+
it('should work when base date is an invalid date', function() {
171+
expect(dateParser.parse('30-12-2015', 'dd-MM-yyyy', new Date('foo'))).toEqual(new Date(2015, 11, 30));
172+
});
165173
});
166174

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

0 commit comments

Comments
 (0)
This repository has been archived.