All files Nullable.ts

100% Statements 12/12
100% Branches 4/4
100% Functions 3/3
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 22 23  1x 1x   1x   1x       6x 6x     1x 6x 1x     5x   1x  
import { Option } from 'fp-ts/lib/Option';
import * as OptionFn from 'fp-ts/lib/Option';
import * as ValidationFn from 'fp-ts/lib/Validation';
 
import Decoder, { Decoded } from './Decoder';
 
export default class NullableD<a> extends Decoder<Option<a>> {
  private decoder: Decoder<a>;
 
  constructor(decoder: Decoder<a>) {
    super();
    this.decoder = decoder;
  }
 
  public run(maybeValue: unknown): Decoded<Option<a>> {
    if (maybeValue === null) {
      return ValidationFn.success(OptionFn.none);
    }
 
    return this.decoder.run(maybeValue).map(OptionFn.some);
  }
}