@@ -771,6 +771,47 @@ describe('andThen', () => {
771
771
} ) ;
772
772
} ) ;
773
773
774
+ describe ( 'where' , ( ) => {
775
+ const chars = ( length : number ) : Decoder < string > =>
776
+ string ( ) . where ( ( s : string ) => s . length === length , `expected a string of length ${ length } ` ) ;
777
+
778
+ const range = ( min : number , max : number ) : Decoder < number > =>
779
+ number ( ) . where (
780
+ ( n : number ) => n >= min && n <= max ,
781
+ `expected a number between ${ min } and ${ max } `
782
+ ) ;
783
+
784
+ it ( 'can test for strings of a given length' , ( ) => {
785
+ expect ( chars ( 7 ) . run ( '7777777' ) ) . toEqual ( { ok : true , result : '7777777' } ) ;
786
+
787
+ expect ( chars ( 7 ) . run ( '666666' ) ) . toMatchObject ( {
788
+ ok : false ,
789
+ error : { message : 'expected a string of length 7' }
790
+ } ) ;
791
+ } ) ;
792
+
793
+ it ( 'can test for numbers in a given range' , ( ) => {
794
+ expect ( range ( 1 , 9 ) . run ( 7 ) ) . toEqual ( { ok : true , result : 7 } ) ;
795
+
796
+ expect ( range ( 1 , 9 ) . run ( 12 ) ) . toMatchObject ( {
797
+ ok : false ,
798
+ error : { message : 'expected a number between 1 and 9' }
799
+ } ) ;
800
+ } ) ;
801
+
802
+ it ( 'reports when the base decoder fails' , ( ) => {
803
+ expect ( chars ( 7 ) . run ( false ) ) . toMatchObject ( {
804
+ ok : false ,
805
+ error : { message : 'expected a string, got a boolean' }
806
+ } ) ;
807
+
808
+ expect ( range ( 0 , 1 ) . run ( null ) ) . toMatchObject ( {
809
+ ok : false ,
810
+ error : { message : 'expected a number, got null' }
811
+ } ) ;
812
+ } ) ;
813
+ } ) ;
814
+
774
815
describe ( 'Result' , ( ) => {
775
816
describe ( 'can run a decoder with default value' , ( ) => {
776
817
const decoder = number ( ) ;
0 commit comments