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

Commit 1c79888

Browse files
tobigunwesleycho
authored andcommitted
feat(dateparser): add support for literals
- Add support for string literals via single quotes Closes #3914 Closes #4426
1 parent 809ecdb commit 1c79888

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

src/dateparser/dateparser.js

+29
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,35 @@ angular.module('ui.bootstrap.dateparser', [])
111111
function createParser(format) {
112112
var map = [], regex = format.split('');
113113

114+
// check for literal values
115+
var quoteIndex = format.indexOf('\'');
116+
if (quoteIndex > -1) {
117+
var inLiteral = false;
118+
format = format.split('');
119+
for (var i = quoteIndex; i < format.length; i++) {
120+
if (inLiteral) {
121+
if (format[i] === '\'') {
122+
if (i + 1 < format.length && format[i+1] === '\'') { // escaped single quote
123+
format[i+1] = '$';
124+
regex[i+1] = '';
125+
} else { // end of literal
126+
regex[i] = '';
127+
inLiteral = false;
128+
}
129+
}
130+
format[i] = '$';
131+
} else {
132+
if (format[i] === '\'') { // start of literal
133+
format[i] = '$';
134+
regex[i] = '';
135+
inLiteral = true;
136+
}
137+
}
138+
}
139+
140+
format = format.join('');
141+
}
142+
114143
angular.forEach(formatCodeToRegex, function(data, code) {
115144
var index = format.indexOf(code);
116145

src/dateparser/test/dateparser.spec.js

+22
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,28 @@ describe('date parser', function() {
204204
});
205205
});
206206

207+
describe('with value literals', function() {
208+
it('should work with multiple literals', function() {
209+
expect(dateParser.parse('29 de January de 2013', 'd \'de\' MMMM \'de\' y')).toEqual(new Date(2013, 0, 29));
210+
});
211+
212+
it('should work with escaped single quote', function() {
213+
expect(dateParser.parse('22.March.15 12 o\'clock', 'd.MMMM.yy h \'o\'\'clock\'')).toEqual(new Date(2015, 2, 22, 12));
214+
});
215+
216+
it('should work with only a single quote', function() {
217+
expect(dateParser.parse('22.March.15 \'', 'd.MMMM.yy \'\'\'')).toEqual(new Date(2015, 2, 22));
218+
});
219+
220+
it('should work with trailing literal', function() {
221+
expect(dateParser.parse('year 2013', '\'year\' y')).toEqual(new Date(2013, 0, 1));
222+
});
223+
224+
it('should work without whitespace', function() {
225+
expect(dateParser.parse('year:2013', '\'year:\'y')).toEqual(new Date(2013, 0, 1));
226+
});
227+
});
228+
207229
describe('with edge case', function() {
208230
it('should not work for invalid number of days in February', function() {
209231
expectParse('29.02.2013', 'dd.MM.yyyy', undefined);

src/datepicker/docs/readme.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ All settings can be provided as attributes in the `uib-datepicker` or globally c
2828
* `date-disabled (date, mode)`
2929
_(Default: null)_ :
3030
An optional expression to disable visible options based on passing date and current mode _(day|month|year)_.
31-
31+
3232
* `custom-class (date, mode)`
3333
_(Default: null)_ :
3434
An optional expression to add classes based on passing date and current mode _(day|month|year)_.
@@ -79,7 +79,7 @@ All settings can be provided as attributes in the `uib-datepicker` or globally c
7979

8080
* `year-range`
8181
_(Default: 20)_ :
82-
Number of years displayed in year selection.
82+
Number of years displayed in year selection.
8383

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

9898
* `uib-datepicker-popup`
9999
_(Default: 'yyyy-MM-dd')_ :
100-
The format for displayed dates.
100+
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'`
101101

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

0 commit comments

Comments
 (0)