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

Commit 83d1435

Browse files
committed
fix(dateparser): enforce order of regex construction
- Change `formatCodeToRegex` into an array to avoid potential iteration bug where the order the keys are listed in the object matters in the iteration order Closes #4810 Fixes #4808
1 parent 56642ea commit 83d1435

File tree

1 file changed

+51
-28
lines changed

1 file changed

+51
-28
lines changed

src/dateparser/dateparser.js

+51-28
Original file line numberDiff line numberDiff line change
@@ -12,94 +12,117 @@ angular.module('ui.bootstrap.dateparser', [])
1212

1313
this.parsers = {};
1414

15-
formatCodeToRegex = {
16-
'yyyy': {
15+
formatCodeToRegex = [
16+
{
17+
key: 'yyyy',
1718
regex: '\\d{4}',
1819
apply: function(value) { this.year = +value; }
1920
},
20-
'yy': {
21+
{
22+
key: 'yy',
2123
regex: '\\d{2}',
2224
apply: function(value) { this.year = +value + 2000; }
2325
},
24-
'y': {
26+
{
27+
key: 'y',
2528
regex: '\\d{1,4}',
2629
apply: function(value) { this.year = +value; }
2730
},
28-
'M!': {
31+
{
32+
key: 'M!',
2933
regex: '0?[1-9]|1[0-2]',
3034
apply: function(value) { this.month = value - 1; }
3135
},
32-
'MMMM': {
36+
{
37+
key: 'MMMM',
3338
regex: $locale.DATETIME_FORMATS.MONTH.join('|'),
3439
apply: function(value) { this.month = $locale.DATETIME_FORMATS.MONTH.indexOf(value); }
3540
},
36-
'MMM': {
41+
{
42+
key: 'MMM',
3743
regex: $locale.DATETIME_FORMATS.SHORTMONTH.join('|'),
3844
apply: function(value) { this.month = $locale.DATETIME_FORMATS.SHORTMONTH.indexOf(value); }
3945
},
40-
'MM': {
46+
{
47+
key: 'MM',
4148
regex: '0[1-9]|1[0-2]',
4249
apply: function(value) { this.month = value - 1; }
4350
},
44-
'M': {
51+
{
52+
key: 'M',
4553
regex: '[1-9]|1[0-2]',
4654
apply: function(value) { this.month = value - 1; }
4755
},
48-
'd!': {
56+
{
57+
key: 'd!',
4958
regex: '[0-2]?[0-9]{1}|3[0-1]{1}',
5059
apply: function(value) { this.date = +value; }
5160
},
52-
'dd': {
61+
{
62+
key: 'dd',
5363
regex: '[0-2][0-9]{1}|3[0-1]{1}',
5464
apply: function(value) { this.date = +value; }
5565
},
56-
'd': {
66+
{
67+
key: 'd',
5768
regex: '[1-2]?[0-9]{1}|3[0-1]{1}',
5869
apply: function(value) { this.date = +value; }
5970
},
60-
'EEEE': {
71+
{
72+
key: 'EEEE',
6173
regex: $locale.DATETIME_FORMATS.DAY.join('|')
6274
},
63-
'EEE': {
75+
{
76+
key: 'EEE',
6477
regex: $locale.DATETIME_FORMATS.SHORTDAY.join('|')
6578
},
66-
'HH': {
79+
{
80+
key: 'HH',
6781
regex: '(?:0|1)[0-9]|2[0-3]',
6882
apply: function(value) { this.hours = +value; }
6983
},
70-
'hh': {
84+
{
85+
key: 'hh',
7186
regex: '0[0-9]|1[0-2]',
7287
apply: function(value) { this.hours = +value; }
7388
},
74-
'H': {
89+
{
90+
key: 'H',
7591
regex: '1?[0-9]|2[0-3]',
7692
apply: function(value) { this.hours = +value; }
7793
},
78-
'h': {
94+
{
95+
key: 'h',
7996
regex: '[0-9]|1[0-2]',
8097
apply: function(value) { this.hours = +value; }
8198
},
82-
'mm': {
99+
{
100+
key: 'mm',
83101
regex: '[0-5][0-9]',
84102
apply: function(value) { this.minutes = +value; }
85103
},
86-
'm': {
104+
{
105+
key: 'm',
87106
regex: '[0-9]|[1-5][0-9]',
88107
apply: function(value) { this.minutes = +value; }
89108
},
90-
'sss': {
109+
{
110+
key: 'sss',
91111
regex: '[0-9][0-9][0-9]',
92112
apply: function(value) { this.milliseconds = +value; }
93113
},
94-
'ss': {
114+
{
115+
key: 'ss',
95116
regex: '[0-5][0-9]',
96117
apply: function(value) { this.seconds = +value; }
97118
},
98-
's': {
119+
{
120+
key: 's',
99121
regex: '[0-9]|[1-5][0-9]',
100122
apply: function(value) { this.seconds = +value; }
101123
},
102-
'a': {
124+
{
125+
key: 'a',
103126
regex: $locale.DATETIME_FORMATS.AMPMS.join('|'),
104127
apply: function(value) {
105128
if (this.hours === 12) {
@@ -111,7 +134,7 @@ angular.module('ui.bootstrap.dateparser', [])
111134
}
112135
}
113136
}
114-
};
137+
];
115138
};
116139

117140
this.init();
@@ -148,15 +171,15 @@ angular.module('ui.bootstrap.dateparser', [])
148171
format = format.join('');
149172
}
150173

151-
angular.forEach(formatCodeToRegex, function(data, code) {
152-
var index = format.indexOf(code);
174+
angular.forEach(formatCodeToRegex, function(data) {
175+
var index = format.indexOf(data.key);
153176

154177
if (index > -1) {
155178
format = format.split('');
156179

157180
regex[index] = '(' + data.regex + ')';
158181
format[index] = '$'; // Custom symbol to define consumed part of format
159-
for (var i = index + 1, n = index + code.length; i < n; i++) {
182+
for (var i = index + 1, n = index + data.key.length; i < n; i++) {
160183
regex[i] = '';
161184
format[i] = '$';
162185
}

0 commit comments

Comments
 (0)