1
1
angular . module ( 'ui.bootstrap.dateparser' , [ ] )
2
2
3
- . service ( 'uibDateParser' , [ '$log' , '$locale' , 'orderByFilter' , function ( $log , $locale , orderByFilter ) {
3
+ . service ( 'uibDateParser' , [ '$log' , '$locale' , 'dateFilter' , ' orderByFilter', function ( $log , $locale , dateFilter , orderByFilter ) {
4
4
// Pulled from https://github.com/mbostock/d3/blob/master/src/format/requote.js
5
5
var SPECIAL_CHARACTERS_REGEXP = / [ \\ \^ \$ \* \+ \? \| \[ \] \( \) \. \{ \} ] / g;
6
6
@@ -11,115 +11,164 @@ angular.module('ui.bootstrap.dateparser', [])
11
11
localeId = $locale . id ;
12
12
13
13
this . parsers = { } ;
14
+ this . formatters = { } ;
14
15
15
16
formatCodeToRegex = [
16
17
{
17
18
key : 'yyyy' ,
18
19
regex : '\\d{4}' ,
19
- apply : function ( value ) { this . year = + value ; }
20
+ apply : function ( value ) { this . year = + value ; } ,
21
+ formatter : function ( date ) {
22
+ var _date = new Date ( ) ;
23
+ _date . setFullYear ( Math . abs ( date . getFullYear ( ) ) ) ;
24
+ return dateFilter ( _date , 'yyyy' ) ;
25
+ }
20
26
} ,
21
27
{
22
28
key : 'yy' ,
23
29
regex : '\\d{2}' ,
24
- apply : function ( value ) { this . year = + value + 2000 ; }
30
+ apply : function ( value ) { this . year = + value + 2000 ; } ,
31
+ formatter : function ( date ) {
32
+ var _date = new Date ( ) ;
33
+ _date . setFullYear ( Math . abs ( date . getFullYear ( ) ) ) ;
34
+ return dateFilter ( _date , 'yy' ) ;
35
+ }
25
36
} ,
26
37
{
27
38
key : 'y' ,
28
39
regex : '\\d{1,4}' ,
29
- apply : function ( value ) { this . year = + value ; }
40
+ apply : function ( value ) { this . year = + value ; } ,
41
+ formatter : function ( date ) {
42
+ var _date = new Date ( ) ;
43
+ _date . setFullYear ( Math . abs ( date . getFullYear ( ) ) ) ;
44
+ return dateFilter ( _date , 'y' ) ;
45
+ }
30
46
} ,
31
47
{
32
48
key : 'M!' ,
33
49
regex : '0?[1-9]|1[0-2]' ,
34
- apply : function ( value ) { this . month = value - 1 ; }
50
+ apply : function ( value ) { this . month = value - 1 ; } ,
51
+ formatter : function ( date ) {
52
+ var value = date . getMonth ( ) ;
53
+ if ( / ^ [ 0 - 9 ] $ / . test ( value ) ) {
54
+ return dateFilter ( date , 'MM' ) ;
55
+ }
56
+
57
+ return dateFilter ( date , 'M' ) ;
58
+ }
35
59
} ,
36
60
{
37
61
key : 'MMMM' ,
38
62
regex : $locale . DATETIME_FORMATS . MONTH . join ( '|' ) ,
39
- apply : function ( value ) { this . month = $locale . DATETIME_FORMATS . MONTH . indexOf ( value ) ; }
63
+ apply : function ( value ) { this . month = $locale . DATETIME_FORMATS . MONTH . indexOf ( value ) ; } ,
64
+ formatter : function ( date ) { return dateFilter ( date , 'MMMM' ) ; }
40
65
} ,
41
66
{
42
67
key : 'MMM' ,
43
68
regex : $locale . DATETIME_FORMATS . SHORTMONTH . join ( '|' ) ,
44
- apply : function ( value ) { this . month = $locale . DATETIME_FORMATS . SHORTMONTH . indexOf ( value ) ; }
69
+ apply : function ( value ) { this . month = $locale . DATETIME_FORMATS . SHORTMONTH . indexOf ( value ) ; } ,
70
+ formatter : function ( date ) { return dateFilter ( date , 'MMM' ) ; }
45
71
} ,
46
72
{
47
73
key : 'MM' ,
48
74
regex : '0[1-9]|1[0-2]' ,
49
- apply : function ( value ) { this . month = value - 1 ; }
75
+ apply : function ( value ) { this . month = value - 1 ; } ,
76
+ formatter : function ( date ) { return dateFilter ( date , 'MM' ) ; }
50
77
} ,
51
78
{
52
79
key : 'M' ,
53
80
regex : '[1-9]|1[0-2]' ,
54
- apply : function ( value ) { this . month = value - 1 ; }
81
+ apply : function ( value ) { this . month = value - 1 ; } ,
82
+ formatter : function ( date ) { return dateFilter ( date , 'M' ) ; }
55
83
} ,
56
84
{
57
85
key : 'd!' ,
58
86
regex : '[0-2]?[0-9]{1}|3[0-1]{1}' ,
59
- apply : function ( value ) { this . date = + value ; }
87
+ apply : function ( value ) { this . date = + value ; } ,
88
+ formatter : function ( date ) {
89
+ var value = date . getDate ( ) ;
90
+ if ( / ^ [ 1 - 9 ] $ / . test ( value ) ) {
91
+ return dateFilter ( date , 'dd' ) ;
92
+ }
93
+
94
+ return dateFilter ( date , 'd' ) ;
95
+ }
60
96
} ,
61
97
{
62
98
key : 'dd' ,
63
99
regex : '[0-2][0-9]{1}|3[0-1]{1}' ,
64
- apply : function ( value ) { this . date = + value ; }
100
+ apply : function ( value ) { this . date = + value ; } ,
101
+ formatter : function ( date ) { return dateFilter ( date , 'dd' ) ; }
65
102
} ,
66
103
{
67
104
key : 'd' ,
68
105
regex : '[1-2]?[0-9]{1}|3[0-1]{1}' ,
69
- apply : function ( value ) { this . date = + value ; }
106
+ apply : function ( value ) { this . date = + value ; } ,
107
+ formatter : function ( date ) { return dateFilter ( date , 'd' ) ; }
70
108
} ,
71
109
{
72
110
key : 'EEEE' ,
73
- regex : $locale . DATETIME_FORMATS . DAY . join ( '|' )
111
+ regex : $locale . DATETIME_FORMATS . DAY . join ( '|' ) ,
112
+ formatter : function ( date ) { return dateFilter ( date , 'EEEE' ) ; }
74
113
} ,
75
114
{
76
115
key : 'EEE' ,
77
- regex : $locale . DATETIME_FORMATS . SHORTDAY . join ( '|' )
116
+ regex : $locale . DATETIME_FORMATS . SHORTDAY . join ( '|' ) ,
117
+ formatter : function ( date ) { return dateFilter ( date , 'EEE' ) ; }
78
118
} ,
79
119
{
80
120
key : 'HH' ,
81
121
regex : '(?:0|1)[0-9]|2[0-3]' ,
82
- apply : function ( value ) { this . hours = + value ; }
122
+ apply : function ( value ) { this . hours = + value ; } ,
123
+ formatter : function ( date ) { return dateFilter ( date , 'HH' ) ; }
83
124
} ,
84
125
{
85
126
key : 'hh' ,
86
127
regex : '0[0-9]|1[0-2]' ,
87
- apply : function ( value ) { this . hours = + value ; }
128
+ apply : function ( value ) { this . hours = + value ; } ,
129
+ formatter : function ( date ) { return dateFilter ( date , 'hh' ) ; }
88
130
} ,
89
131
{
90
132
key : 'H' ,
91
133
regex : '1?[0-9]|2[0-3]' ,
92
- apply : function ( value ) { this . hours = + value ; }
134
+ apply : function ( value ) { this . hours = + value ; } ,
135
+ formatter : function ( date ) { return dateFilter ( date , 'H' ) ; }
93
136
} ,
94
137
{
95
138
key : 'h' ,
96
139
regex : '[0-9]|1[0-2]' ,
97
- apply : function ( value ) { this . hours = + value ; }
140
+ apply : function ( value ) { this . hours = + value ; } ,
141
+ formatter : function ( date ) { return dateFilter ( date , 'h' ) ; }
98
142
} ,
99
143
{
100
144
key : 'mm' ,
101
145
regex : '[0-5][0-9]' ,
102
- apply : function ( value ) { this . minutes = + value ; }
146
+ apply : function ( value ) { this . minutes = + value ; } ,
147
+ formatter : function ( date ) { return dateFilter ( date , 'mm' ) ; }
103
148
} ,
104
149
{
105
150
key : 'm' ,
106
151
regex : '[0-9]|[1-5][0-9]' ,
107
- apply : function ( value ) { this . minutes = + value ; }
152
+ apply : function ( value ) { this . minutes = + value ; } ,
153
+ formatter : function ( date ) { return dateFilter ( date , 'm' ) ; }
108
154
} ,
109
155
{
110
156
key : 'sss' ,
111
157
regex : '[0-9][0-9][0-9]' ,
112
- apply : function ( value ) { this . milliseconds = + value ; }
158
+ apply : function ( value ) { this . milliseconds = + value ; } ,
159
+ formatter : function ( date ) { return dateFilter ( date , 'sss' ) ; }
113
160
} ,
114
161
{
115
162
key : 'ss' ,
116
163
regex : '[0-5][0-9]' ,
117
- apply : function ( value ) { this . seconds = + value ; }
164
+ apply : function ( value ) { this . seconds = + value ; } ,
165
+ formatter : function ( date ) { return dateFilter ( date , 'ss' ) ; }
118
166
} ,
119
167
{
120
168
key : 's' ,
121
169
regex : '[0-9]|[1-5][0-9]' ,
122
- apply : function ( value ) { this . seconds = + value ; }
170
+ apply : function ( value ) { this . seconds = + value ; } ,
171
+ formatter : function ( date ) { return dateFilter ( date , 's' ) ; }
123
172
} ,
124
173
{
125
174
key : 'a' ,
@@ -132,7 +181,8 @@ angular.module('ui.bootstrap.dateparser', [])
132
181
if ( value === 'PM' ) {
133
182
this . hours += 12 ;
134
183
}
135
- }
184
+ } ,
185
+ formatter : function ( date ) { return dateFilter ( date , 'a' ) ; }
136
186
} ,
137
187
{
138
188
key : 'Z' ,
@@ -144,38 +194,47 @@ angular.module('ui.bootstrap.dateparser', [])
144
194
minutes = matches [ 3 ] ;
145
195
this . hours += toInt ( sign + hours ) ;
146
196
this . minutes += toInt ( sign + minutes ) ;
197
+ } ,
198
+ formatter : function ( date ) {
199
+ return dateFilter ( date , 'Z' ) ;
147
200
}
148
201
} ,
149
202
{
150
203
key : 'ww' ,
151
- regex : '[0-4][0-9]|5[0-3]'
204
+ regex : '[0-4][0-9]|5[0-3]' ,
205
+ formatter : function ( date ) { return dateFilter ( date , 'ww' ) ; }
152
206
} ,
153
207
{
154
208
key : 'w' ,
155
- regex : '[0-9]|[1-4][0-9]|5[0-3]'
209
+ regex : '[0-9]|[1-4][0-9]|5[0-3]' ,
210
+ formatter : function ( date ) { return dateFilter ( date , 'w' ) ; }
156
211
} ,
157
212
{
158
213
key : 'GGGG' ,
159
- regex : $locale . DATETIME_FORMATS . ERANAMES . join ( '|' ) . replace ( / \s / g, '\\s' )
214
+ regex : $locale . DATETIME_FORMATS . ERANAMES . join ( '|' ) . replace ( / \s / g, '\\s' ) ,
215
+ formatter : function ( date ) { return dateFilter ( date , 'GGGG' ) ; }
160
216
} ,
161
217
{
162
218
key : 'GGG' ,
163
- regex : $locale . DATETIME_FORMATS . ERAS . join ( '|' )
219
+ regex : $locale . DATETIME_FORMATS . ERAS . join ( '|' ) ,
220
+ formatter : function ( date ) { return dateFilter ( date , 'GGG' ) ; }
164
221
} ,
165
222
{
166
223
key : 'GG' ,
167
- regex : $locale . DATETIME_FORMATS . ERAS . join ( '|' )
224
+ regex : $locale . DATETIME_FORMATS . ERAS . join ( '|' ) ,
225
+ formatter : function ( date ) { return dateFilter ( date , 'GG' ) ; }
168
226
} ,
169
227
{
170
228
key : 'G' ,
171
- regex : $locale . DATETIME_FORMATS . ERAS . join ( '|' )
229
+ regex : $locale . DATETIME_FORMATS . ERAS . join ( '|' ) ,
230
+ formatter : function ( date ) { return dateFilter ( date , 'G' ) ; }
172
231
}
173
232
] ;
174
233
} ;
175
234
176
235
this . init ( ) ;
177
236
178
- function createParser ( format ) {
237
+ function createParser ( format , func ) {
179
238
var map = [ ] , regex = format . split ( '' ) ;
180
239
181
240
// check for literal values
@@ -223,7 +282,8 @@ angular.module('ui.bootstrap.dateparser', [])
223
282
224
283
map . push ( {
225
284
index : index ,
226
- apply : data . apply ,
285
+ key : data . key ,
286
+ apply : data [ func ] ,
227
287
matcher : data . regex
228
288
} ) ;
229
289
}
@@ -235,6 +295,41 @@ angular.module('ui.bootstrap.dateparser', [])
235
295
} ;
236
296
}
237
297
298
+ this . filter = function ( date , format ) {
299
+ if ( ! angular . isDate ( date ) || isNaN ( date ) || ! format ) {
300
+ return '' ;
301
+ }
302
+
303
+ format = $locale . DATETIME_FORMATS [ format ] || format ;
304
+
305
+ if ( $locale . id !== localeId ) {
306
+ this . init ( ) ;
307
+ }
308
+
309
+ if ( ! this . formatters [ format ] ) {
310
+ this . formatters [ format ] = createParser ( format , 'formatter' ) ;
311
+ }
312
+
313
+ var parser = this . formatters [ format ] ,
314
+ map = parser . map ;
315
+
316
+ var _format = format ;
317
+
318
+ return map . reduce ( function ( str , mapper , i ) {
319
+ var match = _format . match ( new RegExp ( '(.*)' + mapper . key ) ) ;
320
+ if ( match && angular . isString ( match [ 1 ] ) ) {
321
+ str += match [ 1 ] ;
322
+ _format = _format . replace ( match [ 1 ] + mapper . key , '' ) ;
323
+ }
324
+
325
+ if ( mapper . apply ) {
326
+ return str + mapper . apply . call ( null , date ) ;
327
+ }
328
+
329
+ return str ;
330
+ } , '' ) ;
331
+ } ;
332
+
238
333
this . parse = function ( input , format , baseDate ) {
239
334
if ( ! angular . isString ( input ) || ! format ) {
240
335
return input ;
@@ -248,7 +343,7 @@ angular.module('ui.bootstrap.dateparser', [])
248
343
}
249
344
250
345
if ( ! this . parsers [ format ] ) {
251
- this . parsers [ format ] = createParser ( format ) ;
346
+ this . parsers [ format ] = createParser ( format , 'apply' ) ;
252
347
}
253
348
254
349
var parser = this . parsers [ format ] ,
@@ -336,7 +431,7 @@ angular.module('ui.bootstrap.dateparser', [])
336
431
this . timezoneToOffset = timezoneToOffset ;
337
432
this . addDateMinutes = addDateMinutes ;
338
433
this . convertTimezoneToLocal = convertTimezoneToLocal ;
339
-
434
+
340
435
function toTimezone ( date , timezone ) {
341
436
return date && timezone ? convertTimezoneToLocal ( date , timezone ) : date ;
342
437
}
0 commit comments