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

Commit

Permalink
feat(dateparser): add support for literals
Browse files Browse the repository at this point in the history
- Add support for string literals via single quotes

Closes #3914
Closes #4426
  • Loading branch information
tobigun authored and wesleycho committed Oct 31, 2015
1 parent 809ecdb commit 1c79888
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
29 changes: 29 additions & 0 deletions src/dateparser/dateparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,35 @@ angular.module('ui.bootstrap.dateparser', [])
function createParser(format) {
var map = [], regex = format.split('');

// check for literal values
var quoteIndex = format.indexOf('\'');
if (quoteIndex > -1) {
var inLiteral = false;
format = format.split('');
for (var i = quoteIndex; i < format.length; i++) {
if (inLiteral) {
if (format[i] === '\'') {
if (i + 1 < format.length && format[i+1] === '\'') { // escaped single quote
format[i+1] = '$';
regex[i+1] = '';
} else { // end of literal
regex[i] = '';
inLiteral = false;
}
}
format[i] = '$';
} else {
if (format[i] === '\'') { // start of literal
format[i] = '$';
regex[i] = '';
inLiteral = true;
}
}
}

format = format.join('');
}

angular.forEach(formatCodeToRegex, function(data, code) {
var index = format.indexOf(code);

Expand Down
22 changes: 22 additions & 0 deletions src/dateparser/test/dateparser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,28 @@ describe('date parser', function() {
});
});

describe('with value literals', function() {
it('should work with multiple literals', function() {
expect(dateParser.parse('29 de January de 2013', 'd \'de\' MMMM \'de\' y')).toEqual(new Date(2013, 0, 29));
});

it('should work with escaped single quote', function() {
expect(dateParser.parse('22.March.15 12 o\'clock', 'd.MMMM.yy h \'o\'\'clock\'')).toEqual(new Date(2015, 2, 22, 12));
});

it('should work with only a single quote', function() {
expect(dateParser.parse('22.March.15 \'', 'd.MMMM.yy \'\'\'')).toEqual(new Date(2015, 2, 22));
});

it('should work with trailing literal', function() {
expect(dateParser.parse('year 2013', '\'year\' y')).toEqual(new Date(2013, 0, 1));
});

it('should work without whitespace', function() {
expect(dateParser.parse('year:2013', '\'year:\'y')).toEqual(new Date(2013, 0, 1));
});
});

describe('with edge case', function() {
it('should not work for invalid number of days in February', function() {
expectParse('29.02.2013', 'dd.MM.yyyy', undefined);
Expand Down
8 changes: 4 additions & 4 deletions src/datepicker/docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ All settings can be provided as attributes in the `uib-datepicker` or globally c
* `date-disabled (date, mode)`
_(Default: null)_ :
An optional expression to disable visible options based on passing date and current mode _(day|month|year)_.

* `custom-class (date, mode)`
_(Default: null)_ :
An optional expression to add classes based on passing date and current mode _(day|month|year)_.
Expand Down Expand Up @@ -79,7 +79,7 @@ All settings can be provided as attributes in the `uib-datepicker` or globally c

* `year-range`
_(Default: 20)_ :
Number of years displayed in year selection.
Number of years displayed in year selection.

* `shortcut-propagation`
_(Default: false)_ :
Expand All @@ -97,7 +97,7 @@ Specific settings for the `uib-datepicker-popup`, that can globally configured t

* `uib-datepicker-popup`
_(Default: 'yyyy-MM-dd')_ :
The format for displayed dates.
The format for displayed dates. This string can take string literals by surrounding the value with single quotes, i.e. `yyyy-MM-dd h 'o\'clock'`

* `show-button-bar`
_(Default: true)_ :
Expand Down Expand Up @@ -154,4 +154,4 @@ Depending on datepicker's current mode, the date may refer either to day, month
* `Enter`/`Space`: Select date.
* `Ctrl`+`Up`: Move to an upper mode.
* `Ctrl`+`Down`: Move to a lower mode.
* `Esc`: Will close popup, and move focus to the input.
* `Esc`: Will close popup, and move focus to the input.

0 comments on commit 1c79888

Please sign in to comment.