All files AndThen.ts

100% Statements 13/13
100% Branches 2/2
100% Functions 5/5
100% Lines 11/11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 221x   1x   1x         3x 3x 3x     3x 3x 1x 2x     1x  
import * as ValidationFn from 'fp-ts/lib/Validation';
 
import Decoder, { Decoded } from './Decoder';
 
export default class AndThen<a, b> extends Decoder<b> {
  private decoderFn: (value: a) => Decoder<b>;
  private decoder: Decoder<a>;
 
  constructor(decoderFn: (value: a) => Decoder<b>, decoder: Decoder<a>) {
    super();
    this.decoder = decoder;
    this.decoderFn = decoderFn;
  }
 
  public run(value: unknown): Decoded<b> {
    return this.decoder.run(value).fold(
      (errors) => ValidationFn.failure(errors),
      (success) => this.decoderFn(success).run(value),
    );
  }
}