@@ -521,6 +521,75 @@ e s`,
521
521
const stringified = stringify ( res ) ;
522
522
assert . equal ( stringified , '{name: RegExp("\'")}' ) ;
523
523
} ) ;
524
+
525
+ it ( 'handles $regex object format (keeps format)' , function ( ) {
526
+ const res = parseFilter (
527
+ '{"name": {"$regex": "pineapple", "$options": "i"}}'
528
+ ) ;
529
+ const stringified = stringify ( res ) ;
530
+ assert . equal (
531
+ stringified ,
532
+ "{name: {$regex: 'pineapple',$options: 'i'}}"
533
+ ) ;
534
+ } ) ;
535
+
536
+ it ( 'handles /regex/ format' , function ( ) {
537
+ const res = {
538
+ name : / p i n e a p p l e / ,
539
+ } ;
540
+ const stringified = stringify ( res ) ;
541
+ assert . equal ( stringified , '{name: RegExp("pineapple")}' ) ;
542
+ } ) ;
543
+ } ) ;
544
+
545
+ context ( 'when provided a BSONRegExp' , function ( ) {
546
+ it ( 'stringifies correctly with options' , function ( ) {
547
+ const res = {
548
+ name : new bson . BSONRegExp ( 'pineapple' , 'i' ) ,
549
+ } ;
550
+ const stringified = stringify ( res ) ;
551
+ assert . equal ( stringified , '{name: RegExp("pineapple", \'i\')}' ) ;
552
+ } ) ;
553
+
554
+ it ( 'stringifies correctly with quotes' , function ( ) {
555
+ const res = {
556
+ name : new bson . BSONRegExp ( '"\'' , 'i' ) ,
557
+ } ;
558
+ const stringified = stringify ( res ) ;
559
+ assert . equal ( stringified , '{name: RegExp("\\"\'", \'i\')}' ) ;
560
+ } ) ;
561
+
562
+ it ( 'stringifies correctly without options' , function ( ) {
563
+ const res = {
564
+ name : new bson . BSONRegExp ( 'pineapple' ) ,
565
+ } ;
566
+ const stringified = stringify ( res ) ;
567
+ assert . equal ( stringified , '{name: RegExp("pineapple")}' ) ;
568
+ } ) ;
569
+
570
+ it ( 'stringifies into BSONRegExp when js RegExp cannot handle an option' , function ( ) {
571
+ const res = {
572
+ name : new bson . BSONRegExp (
573
+ 'pineapple' ,
574
+ 'x' /* x flag is not valid in js but valid in BSONRegExp*/
575
+ ) ,
576
+ } ;
577
+ const stringified = stringify ( res ) ;
578
+ assert . equal ( stringified , '{name: BSONRegExp("pineapple", \'x\')}' ) ;
579
+ } ) ;
580
+
581
+ it ( 'stringifies into BSONRegExp when js RegExp cannot handle the regex' , function ( ) {
582
+ const res = {
583
+ name : new bson . BSONRegExp (
584
+ // Perl Compatible Regular Expressions supported feature that isn't in regular
585
+ // js RegExp: case-insensitive match.
586
+ '(?i)a(?-i)cme' ,
587
+ 'i'
588
+ ) ,
589
+ } ;
590
+ const stringified = stringify ( res ) ;
591
+ assert . equal ( stringified , '{name: BSONRegExp("(?i)a(?-i)cme", \'i\')}' ) ;
592
+ } ) ;
524
593
} ) ;
525
594
526
595
context ( 'when provided a Binary' , function ( ) {
0 commit comments