File tree Expand file tree Collapse file tree 2 files changed +14
-2
lines changed Expand file tree Collapse file tree 2 files changed +14
-2
lines changed Original file line number Diff line number Diff line change @@ -381,6 +381,15 @@ describe('Shell BSON', function () {
381
381
}
382
382
expect . fail ( 'expected error' ) ;
383
383
} ) ;
384
+ it ( 'passes through non-string args to `new Date()`' , function ( ) {
385
+ expect ( shellBson . ISODate ( ) ) . to . be . an . instanceOf ( Date ) ;
386
+ expect ( shellBson . ISODate ( 0 ) . getTime ( ) ) . to . equal ( 0 ) ;
387
+ expect ( shellBson . ISODate ( null ) . getTime ( ) ) . to . equal ( 0 ) ;
388
+ expect ( shellBson . ISODate ( 1234 ) . getTime ( ) ) . to . equal ( 1234 ) ;
389
+ expect ( shellBson . ISODate ( shellBson . ISODate ( 1234 ) ) . getTime ( ) ) . to . equal (
390
+ 1234
391
+ ) ;
392
+ } ) ;
384
393
} ) ;
385
394
describe ( 'BinData' , function ( ) {
386
395
it ( 'expects strings as base 64' , function ( ) {
Original file line number Diff line number Diff line change @@ -261,8 +261,11 @@ export default function constructShellBson(
261
261
} ,
262
262
{ prototype : bson . Long . prototype }
263
263
) ,
264
- ISODate : function ISODate ( input ?: string ) : Date {
265
- if ( ! input ) input = new Date ( ) . toISOString ( ) ;
264
+ ISODate : function ISODate (
265
+ input ?: string | number | Date | undefined
266
+ ) : Date {
267
+ if ( input === undefined ) return new Date ( ) ;
268
+ if ( typeof input !== 'string' ) return new Date ( input ) ;
266
269
const isoDateRegex =
267
270
/ ^ (?< Y > \d { 4 } ) - ? (?< M > \d { 2 } ) - ? (?< D > \d { 2 } ) ( [ T ] (?< h > \d { 2 } ) ( : ? (?< m > \d { 2 } ) ( : ? ( (?< s > \d { 2 } ) ( \. (?< ms > \d + ) ) ? ) ) ? ) ? (?< tz > Z | ( [ + - ] ) ( \d { 2 } ) : ? ( \d { 2 } ) ? ) ? ) ? $ / ;
268
271
const match = isoDateRegex . exec ( input ) ;
You can’t perform that action at this time.
0 commit comments