@@ -175,6 +175,7 @@ export function isEmpty(value: any): boolean {
175
175
* Checks if a value is a string.
176
176
*
177
177
* @param {any } object - the value to check
178
+ * @param {any = false } strict - if truthy, also checks JavaScript tyoe
178
179
* @return {boolean } - true if string, false if not
179
180
*/
180
181
export function isString ( value : any ) : value is string {
@@ -184,7 +185,7 @@ export function isString(value: any): value is string {
184
185
/**
185
186
* 'isNumber' utility function
186
187
*
187
- * Checks if a value is a regular number or a numeric string.
188
+ * Checks if a value is a regular number, numeric string, or JavaScript Date .
188
189
*
189
190
* @param {any } object - the value to check
190
191
* @param {any = false } strict - if truthy, also checks JavaScript tyoe
@@ -245,6 +246,11 @@ export function isArray(item: any): boolean {
245
246
Object . prototype . toString . call ( item ) === '[object Array]' ;
246
247
}
247
248
249
+ export function isDate ( item : any ) : boolean {
250
+ return typeof item === 'object' &&
251
+ Object . prototype . toString . call ( item ) === '[object Date]' ;
252
+ }
253
+
248
254
export function isMap ( item : any ) : boolean {
249
255
return typeof item === 'object' &&
250
256
Object . prototype . toString . call ( item ) === '[object Map]' ;
@@ -300,7 +306,7 @@ export function getType(value: any, strict: any = false): SchemaType {
300
306
if ( isBoolean ( value , 'strict' ) ) { return 'boolean' ; }
301
307
if ( isInteger ( value , strict ) ) { return 'integer' ; }
302
308
if ( isNumber ( value , strict ) ) { return 'number' ; }
303
- if ( isString ( value ) ) { return 'string' ; }
309
+ if ( isString ( value ) || ( ! strict && isDate ( value ) ) ) { return 'string' ; }
304
310
return null ;
305
311
}
306
312
@@ -317,7 +323,7 @@ export function getType(value: any, strict: any = false): SchemaType {
317
323
export function isType ( value : PrimitiveValue , type : SchemaPrimitiveType ) : boolean {
318
324
switch ( type ) {
319
325
case 'string' :
320
- return isString ( value ) ;
326
+ return isString ( value ) || isDate ( value ) ;
321
327
case 'number' :
322
328
return isNumber ( value ) ;
323
329
case 'integer' :
@@ -361,12 +367,16 @@ export function isPrimitive(value: any): boolean {
361
367
* If 'strictIntegers' is FALSE (or not set) the type 'integer' is treated
362
368
* exactly the same as 'number', and allows decimals.
363
369
*
364
- * Examples:
365
- * toJavaScriptType('10', 'number') = 10
366
- * toJavaScriptType('10', 'integer') = 10
367
- * toJavaScriptType('10.5', 'number') = 10.5
370
+ * Valid Examples:
371
+ * toJavaScriptType('10', 'number' ) = 10 // '10' is a number
372
+ * toJavaScriptType('10', 'integer') = 10 // '10' is also an integer
373
+ * toJavaScriptType( 10, 'integer') = 10 // 10 is still an integer
374
+ * toJavaScriptType( 10, 'string' ) = '10' // 10 can be made into a string
375
+ * toJavaScriptType('10.5', 'number' ) = 10.5 // '10.5' is a number
376
+ *
377
+ * Invalid Examples:
368
378
* toJavaScriptType('10.5', 'integer') = null // '10.5' is not an integer
369
- * toJavaScriptType(10.5, 'integer') = null // 10.5 is still not an integer
379
+ * toJavaScriptType( 10.5, 'integer') = null // 10.5 is still not an integer
370
380
*
371
381
* @param {PrimitiveValue } value - value to convert
372
382
* @param {SchemaPrimitiveType | SchemaPrimitiveType[] } types - types to convert to
@@ -390,6 +400,15 @@ export function toJavaScriptType(
390
400
}
391
401
if ( inArray ( 'string' , types ) ) {
392
402
if ( isString ( value ) ) { return value ; }
403
+ // If value is a date, and types includes 'string',
404
+ // convert the date to a string
405
+ if ( isDate ( value ) ) { return value . toISOString ( ) . slice ( 0 , 10 ) ; }
406
+ if ( isNumber ( value ) ) { return value . toString ( ) ; }
407
+ }
408
+ // If value is a date, and types includes 'integer' or 'number',
409
+ // but not 'string', convert the date to a number
410
+ if ( isDate ( value ) && ( inArray ( 'integer' , types ) || inArray ( 'number' , types ) ) ) {
411
+ return value . getTime ( ) ;
393
412
}
394
413
if ( inArray ( 'boolean' , types ) ) {
395
414
if ( isBoolean ( value , true ) ) { return true ; }
0 commit comments