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

fix(dateparser): do not parse if no format specified #2162

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/dateparser/dateparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ angular.module('ui.bootstrap.dateparser', [])
}
};

this.createParser = function(format) {
function createParser(format) {
var map = [], regex = format.split('');

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

this.parse = function(input, format) {
if ( !angular.isString(input) ) {
if ( !angular.isString(input) || !format ) {
return input;
}

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

if ( !this.parsers[format] ) {
this.parsers[format] = this.createParser(format);
this.parsers[format] = createParser(format);
}

var parser = this.parsers[format],
Expand Down
10 changes: 10 additions & 0 deletions src/dateparser/test/dateparser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,14 @@ describe('date parser', function () {
expect(dateParser.parse('November 31, 2013', 'MMMM d, yyyy')).toBeUndefined();
});
});

it('should not parse non-string inputs', function() {
expect(dateParser.parse(123456, 'dd.MM.yyyy')).toBe(123456);
var date = new Date();
expect(dateParser.parse(date, 'dd.MM.yyyy')).toBe(date);
});

it('should not parse if no format is specified', function() {
expect(dateParser.parse('21.08.1951', '')).toBe('21.08.1951');
});
});