import { Failure } from './failure' ; import { Option } from '../option/option' ; import { Try } from './try' ; /**
 * Success represents a computation that ended normally.  It signals that a computation that may or
 * may not have completed did actually complete.
 */ class Success < A > implements Try < A > { private value : A ; get isFailure ( ) : boolean { return false ; } get isSuccess ( ) : boolean { return true ; } constructor ( value ) { this . value = value ; } equals < B > ( other ) { return ( other instanceof Success ) && this . value === other . value ; } filter ( predicate ) { if ( predicate ( this . value ) ) { return this ; } else { return new Failure < A > ( new Error ( `Predicate does not hold for ${ this . value }` ) ) ; } } flatMap < B > ( f ) { return f ( this . value ) ; } foreach ( run ) { run ( this . value ) ; } get ( ) { return this . value ; } getOrElse < B , A extends B > ( this , defaultValue ) { return this . value ; } map < B > ( f ) { return new Success ( f ( this . value ) ) ; } match < B > ( matcher ) { return matcher . Success ( this . value ) ; } orElse < B , A extends B > ( this , alternative ) { return this ; } recover ( fn ) { return this ; } recoverWith ( fn ) { return this ; } toOption ( ) { return Option ( this . value ) ; } } export { Success } ;