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

Commit d9a521a

Browse files
tobigunwesleycho
authored andcommitted
feat(dateparser): reset parsers when $locale.id changes
- Reset parsers when $locale.id changes to take into account changed localization settings Closes #4286 Closes #4425
1 parent 868c0e2 commit d9a521a

File tree

2 files changed

+115
-91
lines changed

2 files changed

+115
-91
lines changed

src/dateparser/dateparser.js

+104-91
Original file line numberDiff line numberDiff line change
@@ -4,101 +4,110 @@ angular.module('ui.bootstrap.dateparser', [])
44
// Pulled from https://github.com/mbostock/d3/blob/master/src/format/requote.js
55
var SPECIAL_CHARACTERS_REGEXP = /[\\\^\$\*\+\?\|\[\]\(\)\.\{\}]/g;
66

7-
this.parsers = {};
8-
9-
var formatCodeToRegex = {
10-
'yyyy': {
11-
regex: '\\d{4}',
12-
apply: function(value) { this.year = +value; }
13-
},
14-
'yy': {
15-
regex: '\\d{2}',
16-
apply: function(value) { this.year = +value + 2000; }
17-
},
18-
'y': {
19-
regex: '\\d{1,4}',
20-
apply: function(value) { this.year = +value; }
21-
},
22-
'MMMM': {
23-
regex: $locale.DATETIME_FORMATS.MONTH.join('|'),
24-
apply: function(value) { this.month = $locale.DATETIME_FORMATS.MONTH.indexOf(value); }
25-
},
26-
'MMM': {
27-
regex: $locale.DATETIME_FORMATS.SHORTMONTH.join('|'),
28-
apply: function(value) { this.month = $locale.DATETIME_FORMATS.SHORTMONTH.indexOf(value); }
29-
},
30-
'MM': {
31-
regex: '0[1-9]|1[0-2]',
32-
apply: function(value) { this.month = value - 1; }
33-
},
34-
'M': {
35-
regex: '[1-9]|1[0-2]',
36-
apply: function(value) { this.month = value - 1; }
37-
},
38-
'dd': {
39-
regex: '[0-2][0-9]{1}|3[0-1]{1}',
40-
apply: function(value) { this.date = +value; }
41-
},
42-
'd': {
43-
regex: '[1-2]?[0-9]{1}|3[0-1]{1}',
44-
apply: function(value) { this.date = +value; }
45-
},
46-
'EEEE': {
47-
regex: $locale.DATETIME_FORMATS.DAY.join('|')
48-
},
49-
'EEE': {
50-
regex: $locale.DATETIME_FORMATS.SHORTDAY.join('|')
51-
},
52-
'HH': {
53-
regex: '(?:0|1)[0-9]|2[0-3]',
54-
apply: function(value) { this.hours = +value; }
55-
},
56-
'hh': {
57-
regex: '0[0-9]|1[0-2]',
58-
apply: function(value) { this.hours = +value; }
59-
},
60-
'H': {
61-
regex: '1?[0-9]|2[0-3]',
62-
apply: function(value) { this.hours = +value; }
63-
},
64-
'h': {
65-
regex: '[0-9]|1[0-2]',
66-
apply: function(value) { this.hours = +value; }
67-
},
68-
'mm': {
69-
regex: '[0-5][0-9]',
70-
apply: function(value) { this.minutes = +value; }
71-
},
72-
'm': {
73-
regex: '[0-9]|[1-5][0-9]',
74-
apply: function(value) { this.minutes = +value; }
75-
},
76-
'sss': {
77-
regex: '[0-9][0-9][0-9]',
78-
apply: function(value) { this.milliseconds = +value; }
79-
},
80-
'ss': {
81-
regex: '[0-5][0-9]',
82-
apply: function(value) { this.seconds = +value; }
83-
},
84-
's': {
85-
regex: '[0-9]|[1-5][0-9]',
86-
apply: function(value) { this.seconds = +value; }
87-
},
88-
'a': {
89-
regex: $locale.DATETIME_FORMATS.AMPMS.join('|'),
90-
apply: function(value) {
91-
if (this.hours === 12) {
92-
this.hours = 0;
93-
}
94-
95-
if (value === 'PM') {
96-
this.hours += 12;
7+
var localeId;
8+
var formatCodeToRegex;
9+
10+
this.init = function() {
11+
localeId = $locale.id;
12+
13+
this.parsers = {};
14+
15+
formatCodeToRegex = {
16+
'yyyy': {
17+
regex: '\\d{4}',
18+
apply: function(value) { this.year = +value; }
19+
},
20+
'yy': {
21+
regex: '\\d{2}',
22+
apply: function(value) { this.year = +value + 2000; }
23+
},
24+
'y': {
25+
regex: '\\d{1,4}',
26+
apply: function(value) { this.year = +value; }
27+
},
28+
'MMMM': {
29+
regex: $locale.DATETIME_FORMATS.MONTH.join('|'),
30+
apply: function(value) { this.month = $locale.DATETIME_FORMATS.MONTH.indexOf(value); }
31+
},
32+
'MMM': {
33+
regex: $locale.DATETIME_FORMATS.SHORTMONTH.join('|'),
34+
apply: function(value) { this.month = $locale.DATETIME_FORMATS.SHORTMONTH.indexOf(value); }
35+
},
36+
'MM': {
37+
regex: '0[1-9]|1[0-2]',
38+
apply: function(value) { this.month = value - 1; }
39+
},
40+
'M': {
41+
regex: '[1-9]|1[0-2]',
42+
apply: function(value) { this.month = value - 1; }
43+
},
44+
'dd': {
45+
regex: '[0-2][0-9]{1}|3[0-1]{1}',
46+
apply: function(value) { this.date = +value; }
47+
},
48+
'd': {
49+
regex: '[1-2]?[0-9]{1}|3[0-1]{1}',
50+
apply: function(value) { this.date = +value; }
51+
},
52+
'EEEE': {
53+
regex: $locale.DATETIME_FORMATS.DAY.join('|')
54+
},
55+
'EEE': {
56+
regex: $locale.DATETIME_FORMATS.SHORTDAY.join('|')
57+
},
58+
'HH': {
59+
regex: '(?:0|1)[0-9]|2[0-3]',
60+
apply: function(value) { this.hours = +value; }
61+
},
62+
'hh': {
63+
regex: '0[0-9]|1[0-2]',
64+
apply: function(value) { this.hours = +value; }
65+
},
66+
'H': {
67+
regex: '1?[0-9]|2[0-3]',
68+
apply: function(value) { this.hours = +value; }
69+
},
70+
'h': {
71+
regex: '[0-9]|1[0-2]',
72+
apply: function(value) { this.hours = +value; }
73+
},
74+
'mm': {
75+
regex: '[0-5][0-9]',
76+
apply: function(value) { this.minutes = +value; }
77+
},
78+
'm': {
79+
regex: '[0-9]|[1-5][0-9]',
80+
apply: function(value) { this.minutes = +value; }
81+
},
82+
'sss': {
83+
regex: '[0-9][0-9][0-9]',
84+
apply: function(value) { this.milliseconds = +value; }
85+
},
86+
'ss': {
87+
regex: '[0-5][0-9]',
88+
apply: function(value) { this.seconds = +value; }
89+
},
90+
's': {
91+
regex: '[0-9]|[1-5][0-9]',
92+
apply: function(value) { this.seconds = +value; }
93+
},
94+
'a': {
95+
regex: $locale.DATETIME_FORMATS.AMPMS.join('|'),
96+
apply: function(value) {
97+
if (this.hours === 12) {
98+
this.hours = 0;
99+
}
100+
101+
if (value === 'PM') {
102+
this.hours += 12;
103+
}
97104
}
98105
}
99-
}
106+
};
100107
};
101108

109+
this.init();
110+
102111
function createParser(format) {
103112
var map = [], regex = format.split('');
104113

@@ -134,6 +143,10 @@ angular.module('ui.bootstrap.dateparser', [])
134143
format = $locale.DATETIME_FORMATS[format] || format;
135144
format = format.replace(SPECIAL_CHARACTERS_REGEXP, '\\$&');
136145

146+
if ($locale.id !== localeId) {
147+
this.init();
148+
}
149+
137150
if (!this.parsers[format]) {
138151
this.parsers[format] = createParser(format);
139152
}

src/dateparser/test/dateparser.spec.js

+11
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,15 @@ describe('date parser', function() {
212212
it('should not parse if no format is specified', function() {
213213
expect(dateParser.parse('21.08.1951', '')).toBe('21.08.1951');
214214
});
215+
216+
it('should reinitialize when locale changes', inject(function($locale) {
217+
spyOn(dateParser, 'init').and.callThrough();
218+
expect($locale.id).toBe('en-us');
219+
220+
$locale.id = 'en-uk';
221+
222+
dateParser.parse('22.March.15.22', 'd.MMMM.yy.s');
223+
224+
expect(dateParser.init).toHaveBeenCalled();
225+
}));
215226
});

0 commit comments

Comments
 (0)