From 42cc3f269bae020ba17b4dcceb4e5afaf671d49b Mon Sep 17 00:00:00 2001 From: Tasos Bekos Date: Wed, 7 May 2014 19:54:46 +0200 Subject: [PATCH] fix(dateparser): do not parse if no format specified Fixes #2159 Fixes #2137 Closes #2162 --- src/dateparser/dateparser.js | 8 ++++---- src/dateparser/test/dateparser.spec.js | 10 ++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/dateparser/dateparser.js b/src/dateparser/dateparser.js index 85fc203352..9d98bff2b7 100644 --- a/src/dateparser/dateparser.js +++ b/src/dateparser/dateparser.js @@ -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) { @@ -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], diff --git a/src/dateparser/test/dateparser.spec.js b/src/dateparser/test/dateparser.spec.js index 4090b41ecd..8bf233f241 100644 --- a/src/dateparser/test/dateparser.spec.js +++ b/src/dateparser/test/dateparser.spec.js @@ -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'); + }); });