feat(dateparser): change template literal from ' to `
- Changes from ' to ` for template literals - Adds documentation about template literals BREAKING CHANGE: This changes template literals from the single quote `'` to the backtick character `` ` `` for more natural usage with certain strings such as `o'clock`. Closes #4880 Closes #4936 Closes #4938
This commit is contained in:
@@ -179,16 +179,16 @@ angular.module('ui.bootstrap.dateparser', [])
|
||||
var map = [], regex = format.split('');
|
||||
|
||||
// check for literal values
|
||||
var quoteIndex = format.indexOf('\'');
|
||||
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] = '';
|
||||
if (format[i] === '`') {
|
||||
if (i + 1 < format.length && format[i + 1] === '\`') { // escaped backtick
|
||||
format[i + 1] = '$';
|
||||
regex[i + 1] = '';
|
||||
} else { // end of literal
|
||||
regex[i] = '';
|
||||
inLiteral = false;
|
||||
@@ -196,7 +196,7 @@ angular.module('ui.bootstrap.dateparser', [])
|
||||
}
|
||||
format[i] = '$';
|
||||
} else {
|
||||
if (format[i] === '\'') { // start of literal
|
||||
if (format[i] === '`') { // start of literal
|
||||
format[i] = '$';
|
||||
regex[i] = '';
|
||||
inLiteral = true;
|
||||
|
||||
@@ -140,3 +140,5 @@ Certain format codes support i18n. Check this [guide](https://docs.angularjs.org
|
||||
\* The ones marked with `Leading 0`, needs a leading 0 for values less than 10. Exception being milliseconds which needs it for values under 100.
|
||||
|
||||
\** It also supports `fullDate|longDate|medium|mediumDate|mediumTime|short|shortDate|shortTime` as the format for parsing.
|
||||
|
||||
\*** It supports template literals as a string between the backtick `\`` character, i.e. `\`The Date is\` MM/DD/YYYY`. If one wants the literal backtick character, one must use `\`\`\`\``.
|
||||
|
||||
@@ -310,23 +310,20 @@ 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));
|
||||
expect(dateParser.parse('29 de January de 2013', 'd `de` MMMM `de` y')).toEqual(new Date(2013, 0, 29));
|
||||
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 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 only a backtick', 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));
|
||||
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));
|
||||
expect(dateParser.parse('year:2013', '`year:`y')).toEqual(new Date(2013, 0, 1));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user