import { Option } from './types' ; import { ResultEquipped } from '../result/equipped' ; import { Result , ResultKind } from '../result/types' ; import { panic } from '../helpers' ; import { Ok , Err } from '../result/helpers' ; import { None } from './consts' ; /**
 * Wrapper for Option<T> containing all methods (that make sense)
 * from Rust's Option<T>
 */ export class OptionEquipped < T > { private _opt : Option < T > ; constructor ( opt ) { this . _opt = opt ; } get inner ( ) : Option < T > { return this . _opt ; } and < U > ( optb ) { if ( this . _opt == null ) { return this as unknown as OptionEquipped < U > ; } else { return new OptionEquipped < U > ( optb ) ; } } andThen < U > ( f ) { if ( this . _opt == null ) { return this as unknown as OptionEquipped < U > ; } else { return new OptionEquipped < U > ( f ( this . _opt ) ) ; } } contains ( value ) { return this . _opt === value ; } expect ( msg ) { if ( this . _opt == null ) { return panic ( `${ msg }` ) ; } else { return this . _opt ; } } filter ( f ) { if ( this . _opt == null ) { return this ; } else if ( f ( this . _opt ) ) { return this ; } else { return new OptionEquipped < T > ( None ) ; } } getOrInsert ( value ) { if ( this . _opt == null ) { return this . _opt = value ; } else { return this . _opt ; } } getOrInsertWith ( f ) { if ( this . _opt == null ) { return this . _opt = f ( ) ; } else { return this . _opt ; } } insert ( value ) { return this . _opt = value ; } isNone ( ) { return this . _opt == null ; } isSome ( ) { return this . _opt != null ; } map < U > ( f ) { if ( this . _opt == null ) { return this as unknown as OptionEquipped < U > ; } else { return new OptionEquipped ( f ( this . _opt ) ) ; } } mapOr < U > ( d , f ) { if ( this . _opt == null ) { return d ; } else { return f ( this . _opt ) ; } } mapOrElse < U > ( df , mf ) { if ( this . _opt == null ) { return df ( ) ; } else { return mf ( this . _opt ) ; } } okOr < E > ( err ) { if ( this . _opt == null ) { return new ResultEquipped < T , E > ( Err ( err ) ) ; } else { return new ResultEquipped < T , E > ( Ok ( this . _opt ) ) ; } } okOrElse < E > ( f ) { if ( this . _opt == null ) { return new ResultEquipped < T , E > ( Err ( f ( ) ) ) ; } else { return new ResultEquipped < T , E > ( Ok ( this . _opt ) ) ; } } or ( optb ) { if ( this . _opt == null ) { return new OptionEquipped ( optb ) ; } else { return this ; } } orElse ( f ) { if ( this . _opt == null ) { return new OptionEquipped ( f ( ) ) ; } else { return this ; } } replace ( value ) { const oldValue = this . _opt ; this . _opt = value ; return new OptionEquipped ( oldValue ) ; } take ( ) { const oldValue = this . _opt ; this . _opt = None ; return new OptionEquipped ( oldValue ) ; } transpose < E > ( ) { const value = this . _opt as Option < Result < T , E > | ResultEquipped < T , E > > ; if ( value == null ) { return new ResultEquipped < Option < T > , E > ( Ok ( value ) ) ; } else { if ( value . __kind === ResultKind . Ok ) { return new ResultEquipped < Option < T > , E > ( Ok ( value . data as T ) ) ; } else { return new ResultEquipped < Option < T > , E > ( Err ( value . data as E ) ) ; } } } unwrap ( ) { if ( this . _opt == null ) { return panic ( 'called OptionEquipped.unwrap() on a `None` value' ) ; } else { return this . _opt ; } } unwrapOr ( d ) { return this . _opt ?? d ; } unwrapOrElse ( f ) { return this . _opt ?? f ( ) ; } xor ( optb ) { if ( this . _opt == null && optb != null ) { return new OptionEquipped ( optb ) ; } else if ( this . _opt != null && optb == null ) { return this ; } else { return new OptionEquipped < T > ( None ) ; } } zip < U > ( optb ) { if ( this . _opt == null || optb == null ) { return new OptionEquipped < [ T , U ] > ( None ) ; } else { return new OptionEquipped ( [ this . _opt , optb ] ) ; } } zipWith < U , R > ( optb , f ) { if ( this . _opt == null || optb == null ) { return new OptionEquipped < R > ( None ) ; } else { return new OptionEquipped ( f ( this . _opt , optb ) ) ; } } }