Skip to content

Commit 232504b

Browse files
bekosgabegorelick
authored andcommitted
fix(dateparser): do not parse if no format specified
Fixes angular-ui#2159 Fixes angular-ui#2137 Closes angular-ui#2162
1 parent a81d6f9 commit 232504b

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/dateparser/dateparser.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ angular.module('ui.bootstrap.dateparser', [])
4949
}
5050
};
5151

52-
this.createParser = function(format) {
52+
function createParser(format) {
5353
var map = [], regex = format.split('');
5454

5555
angular.forEach(formatCodeToRegex, function(data, code) {
@@ -74,17 +74,17 @@ angular.module('ui.bootstrap.dateparser', [])
7474
regex: new RegExp('^' + regex.join('') + '$'),
7575
map: orderByFilter(map, 'index')
7676
};
77-
};
77+
}
7878

7979
this.parse = function(input, format) {
80-
if ( !angular.isString(input) ) {
80+
if ( !angular.isString(input) || !format ) {
8181
return input;
8282
}
8383

8484
format = $locale.DATETIME_FORMATS[format] || format;
8585

8686
if ( !this.parsers[format] ) {
87-
this.parsers[format] = this.createParser(format);
87+
this.parsers[format] = createParser(format);
8888
}
8989

9090
var parser = this.parsers[format],

src/dateparser/test/dateparser.spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,14 @@ describe('date parser', function () {
9393
expect(dateParser.parse('November 31, 2013', 'MMMM d, yyyy')).toBeUndefined();
9494
});
9595
});
96+
97+
it('should not parse non-string inputs', function() {
98+
expect(dateParser.parse(123456, 'dd.MM.yyyy')).toBe(123456);
99+
var date = new Date();
100+
expect(dateParser.parse(date, 'dd.MM.yyyy')).toBe(date);
101+
});
102+
103+
it('should not parse if no format is specified', function() {
104+
expect(dateParser.parse('21.08.1951', '')).toBe('21.08.1951');
105+
});
96106
});

0 commit comments

Comments
 (0)