Skip to content

Commit

Permalink
Added support for options as second param to parseDateTimeField
Browse files Browse the repository at this point in the history
  • Loading branch information
shepherdsam committed Jan 31, 2017
1 parent 2f17691 commit a044b20
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
24 changes: 17 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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')
Expand All @@ -417,21 +419,29 @@ 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) {
var getRtnObject = function(parsed, isValid) {
return formatParsedObject('date', field, isValid, parsed);
};

var options = {};
if (_.isString(locale)) {
options.locale = locale;
} else if (locale) {
options = locale;
}

var parsedDate;

if(_.isDate(field)) {
parsedDate = field;

} 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.
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions spec/specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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() {
Expand Down

0 comments on commit a044b20

Please sign in to comment.