import { pipe , R , O , A , S , D } from '../..' ; describe ( 'toOption' , ( ) => { it ( 'returns None' , ( ) => { expect ( pipe ( R . fromNullable ( null , 'this is bad' ) , R . toOption ) ) . toEqual ( O . None ) ; expect ( pipe ( R . fromNullable ( 'value' , 'this is bad' ) , R . flatMap ( _ => R . Error ( 'new error' ) ) , R . toOption ) ) . toEqual ( O . None ) ; expect ( pipe ( R . fromNullable ( null , 'this is bad' ) , R . flatMap ( _ => R . Ok ( 'this is fine' ) ) , R . toOption ) ) . toEqual ( O . None ) ; } ) ; it ( 'returns Some' , ( ) => { expect ( pipe ( R . Ok ( 'value' ) , R . toOption ) ) . toEqual ( O . Some ( 'value' ) ) ; expect ( pipe ( R . fromNullable ( 'value' , 'this is bad' ) , R . toOption ) ) . toEqual ( O . Some ( 'value' ) ) ; expect ( pipe ( R . fromNullable ( 'this is fine' , 'this is bad' ) , R . flatMap ( str => R . Ok ( `${ str }!` ) ) , R . toOption ) ) . toEqual ( O . Some ( 'this is fine!' ) ) ; } ) ; it ( '*' , ( ) => { let obj = { names : 'hello,world,ts,belt' , } ; expect ( pipe ( R . fromNullable ( obj , 'cannot be nullable' ) , R . map ( D . getUnsafe ( 'names' ) ) , R . map ( S . split ( ',' ) ) , R . toOption , O . flatMap ( A . dropExactly ( 2 ) ) , O . map ( A . join ( '-' ) ) , O . getWithDefault ( 'nothing found' ) ) ) . toEqual ( 'ts-belt' ) ; obj = null as unknown as { names : string ; } | null ; expect ( pipe ( R . fromNullable ( obj , 'cannot be nullable' ) , R . map ( D . getUnsafe ( 'names' ) ) , R . map ( S . split ( ',' ) ) , R . toOption , O . flatMap ( A . dropExactly ( 2 ) ) , O . map ( A . join ( '-' ) ) , O . getWithDefault ( 'nothing found' ) ) ) . toEqual ( 'nothing found' ) ; } ) ; } ) ;