@@ -11,7 +11,7 @@ import moment from "moment";
11
11
// for example '/Date(1198908717056)/' or '/Date(1198908717056-0700)/'
12
12
// code from http://momentjs.com/
13
13
const ASPDateRegex = / ^ \/ ? D a t e \( ( - ? \d + ) / i;
14
-
14
+ const NumericRegex = / ^ \d + $ / ;
15
15
/**
16
16
* Convert an object into another type
17
17
*
@@ -59,26 +59,17 @@ export function convert(object, type) {
59
59
return String ( object ) ;
60
60
61
61
case "Date" :
62
- if ( isNumber ( object ) ) {
63
- return new Date ( object ) ;
62
+ try {
63
+ return convert ( object , "Moment" ) . toDate ( ) ;
64
64
}
65
- if ( object instanceof Date ) {
66
- return new Date ( object . valueOf ( ) ) ;
67
- } else if ( moment . isMoment ( object ) ) {
68
- return new Date ( object . valueOf ( ) ) ;
69
- }
70
- if ( isString ( object ) ) {
71
- match = ASPDateRegex . exec ( object ) ;
72
- if ( match ) {
73
- // object is an ASP date
74
- return new Date ( Number ( match [ 1 ] ) ) ; // parse number
65
+ catch ( e ) {
66
+ if ( e instanceof TypeError ) {
67
+ throw new TypeError (
68
+ "Cannot convert object of type " + getType ( object ) + " to type " + type
69
+ ) ;
75
70
} else {
76
- return moment ( new Date ( object ) ) . toDate ( ) ; // parse string
71
+ throw e ;
77
72
}
78
- } else {
79
- throw new Error (
80
- "Cannot convert object of type " + getType ( object ) + " to type Date"
81
- ) ;
82
73
}
83
74
84
75
case "Moment" :
@@ -95,12 +86,17 @@ export function convert(object, type) {
95
86
if ( match ) {
96
87
// object is an ASP date
97
88
return moment ( Number ( match [ 1 ] ) ) ; // parse number
98
- } else {
99
- return moment ( object ) ; // parse string
89
+ }
90
+ match = NumericRegex . exec ( object ) ;
91
+
92
+ if ( match ) {
93
+ return moment ( Number ( object ) ) ;
100
94
}
95
+
96
+ return moment ( object ) ; // parse string
101
97
} else {
102
- throw new Error (
103
- "Cannot convert object of type " + getType ( object ) + " to type Date"
98
+ throw new TypeError (
99
+ "Cannot convert object of type " + getType ( object ) + " to type " + type
104
100
) ;
105
101
}
106
102
0 commit comments