diff --git a/lib/index.js b/lib/index.js index 294a455..8be448a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -27,17 +27,17 @@ var formatParsedObject = function(type, input, valid, parsed) { }; /* istanbul ignore next */ -Utils._getCurrentDate = function(locale) { +Utils._getCurrentDate = function(options) { // Makes unit testing possible, by allowing this // function to be mocked. - return SugarDate.create('now', locale); + return SugarDate.create('now', options); }; /* istanbul ignore next */ -Utils._getFutureDate = function(str, locale) { +Utils._getFutureDate = function(str, options) { // Makes unit testing possible, by allowing this // function to be mocked. - return SugarDate.create(str, {future: true, locale: locale}); + return SugarDate.create(str, _.assign({future: true}, options)); }; Utils.activateDateParser = function() { @@ -405,6 +405,8 @@ Utils.cloneTerse = function(input) { * - Utils.parseDateTimeField('may 25th of next year') * - Utils.parseDateTimeField('2014-01-18 09:30:00') * - Utils.parseDateTimeField('2014-01-18 09:30:00 -0400') + * - Utils.parseDateTimeField('2014-01-18 09:30:00', 'en-GB') + * - Utils.parseDateTimeField('2014-01-18 09:30:00', {fromUTC: true, setUTC: true}) * - Utils.parseDateTimeField('in 2 days') * - Utils.parseDateTimeField('5 minutes from now') * - Utils.parseDateTimeField('2014-01-18 09:30:00 -0400 +2d +30m') @@ -417,6 +419,7 @@ Utils.cloneTerse = function(input) { * - -4d -6h will reduce the parsed date by 4 days and 6 hours * * @param {String} field the date string to parse. + * @param {String} locale the locale string OR options object. * @return {Object} an object containing the parsed date, or the passed field if it was not a String. */ Utils.parseDateTimeField = function(field, locale) { @@ -424,6 +427,13 @@ Utils.parseDateTimeField = function(field, locale) { return formatParsedObject('date', field, isValid, parsed); }; + var options = {}; + if (_.isString(locale)) { + options.locale = locale; + } else if (locale) { + options = locale; + } + var parsedDate; if(_.isDate(field)) { @@ -431,7 +441,7 @@ Utils.parseDateTimeField = function(field, locale) { } else if(!_.isString(field)) { // Just create a date from the passed value. - parsedDate = SugarDate.create(field, locale); + parsedDate = SugarDate.create(field, options); } else { // Regex for parsing a offset modifier. @@ -479,8 +489,8 @@ Utils.parseDateTimeField = function(field, locale) { // Otherwise, parse the string to create a date in the future. parsedDate = withoutOffsetModifiers === '' && hasOffsetModifier ? - Utils._getCurrentDate(locale) : - Utils._getFutureDate(withoutOffsetModifiers, locale); + Utils._getCurrentDate(options) : + Utils._getFutureDate(withoutOffsetModifiers, options); if(SugarDate.isValid(parsedDate) && hasOffsetModifier && offsetSecs) { // Apply the offset modifier. diff --git a/spec/specs.js b/spec/specs.js index 70a9a0a..e1e64bf 100644 --- a/spec/specs.js +++ b/spec/specs.js @@ -800,7 +800,7 @@ describe('Utils', function() { it('should parse a datetime string', function() { expectValidDate('tomorrow', epoch); - expect(Utils._getFutureDate).toHaveBeenCalledWith('tomorrow', undefined); + expect(Utils._getFutureDate).toHaveBeenCalledWith('tomorrow', {}); }); it('should return as invalid if no string is passed', function() { @@ -828,7 +828,7 @@ describe('Utils', function() { describe('Locales', function() { it('parses a date withtout locale', function() { Utils.parseDateTimeField('now +1d'); - expect(Utils._getFutureDate).toHaveBeenCalledWith('now', undefined); + expect(Utils._getFutureDate).toHaveBeenCalledWith('now', {}); epoch = SugarDate.create('1/11/2017', {fromUTC: true}); var d = Utils.parseDateTimeField('1/11/2017'); @@ -837,7 +837,7 @@ describe('Utils', function() { it('parses a date with locale', function() { Utils.parseDateTimeField('now +1d', 'en-GB'); - expect(Utils._getFutureDate).toHaveBeenCalledWith('now', 'en-GB'); + expect(Utils._getFutureDate).toHaveBeenCalledWith('now', {locale: 'en-GB'}); epoch = SugarDate.create('1/11/2017', {fromUTC: true}); var d = Utils.parseDateTimeField('11/1/2017'); @@ -848,7 +848,7 @@ describe('Utils', function() { describe('Offset Modifiers', function() { it('should strip offset modifier from string to parse', function() { Utils.parseDateTimeField('now +1d'); - expect(Utils._getFutureDate).toHaveBeenCalledWith('now', undefined); + expect(Utils._getFutureDate).toHaveBeenCalledWith('now', {}); }); describe('Increment', function() {