All files / maybe maybe.ts

100% Statements 29/29
100% Branches 18/18
100% Functions 10/10
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166                                                                                  1x 72x     58x       32x 19x       3x 2x       13x 8x       3x 1x       7x 3x       14x     72x 72x 72x 72x 72x 72x   72x                                                                                                                                                       1x 14x        
type Box<T> = {
  /**
   * takes your value as an argument, allowing you to update it safely
   */
  map: <U>(fn: (a: NonNullable<T>) => U) => Box<U>
  /**
   * if value is defined, calls the function you give on
   * the item in the Box and returns its result
   */
  flatMap: <U>(fn: (a: NonNullable<T>) => Box<U>) => Box<U>
  /**
   * takes your value as an argument, allowing you to return a predicate
   */
  filter: (fn: (a: NonNullable<T>) => boolean) => Box<T>
  /**
   * expects a fallback value in case of the initial value was `undefined`
   * or `null`.
   */
  getOrElse: (t: NonNullable<T>) => NonNullable<T>
  /**
   * returns your value
   */
  get: () => T | undefined
  /**
   * Takes two functions. a first function that will get executed if the value
   * is`undefined` or `null`, allowing you to return a fallback value.
   *
   * A second function that will get called with the value if defined.
   * The result of this function will be then returned.
   */
  fold: <U>(onNone: () => U, onSome: (a: NonNullable<T>) => U) => U
}
 
/**
 * Wraps a potentially `nullable` value and returns a `Box` object, allowing you
 * to manipulate the value safely as if it was defined.
 *
 * @param value A potentially nullable value
 * @returns Returns a `Box` with 5 methods, `map`, `filter`, `fold`, `get` and `getOrElse`
 *
 */
export function maybe<T>(a: T) {
  const self = {} as Box<T>
 
  function isNone(a: T) {
    return a === undefined || a === null
  }
 
  function map<U>(fn: (arg: NonNullable<T>) => U) {
    if (isNone(a)) return maybe(undefined as unknown) as Box<U>
    return maybe(fn(a as NonNullable<T>))
  }
 
  function flatMap<U>(fn: (arg: NonNullable<T>) => Box<U>) {
    if (isNone(a)) return maybe(undefined as unknown) as Box<U>
    return fn(a as NonNullable<T>)
  }
 
  function filter(fn: (arg: NonNullable<T>) => boolean) {
    if (!isNone(a) && fn(a as NonNullable<T>)) return maybe(a)
    return (maybe(undefined) as unknown) as Box<T>
  }
 
  function getOrElse(t: NonNullable<T>): NonNullable<T> {
    if (isNone(a)) return t
    return a as NonNullable<T>
  }
 
  function fold<U>(onNone: () => U, onSome: (a: NonNullable<T>) => U) {
    if (isNone(a)) return onNone()
    return onSome(a as NonNullable<T>)
  }
 
  function get() {
    return a
  }
 
  self.map = map
  self.flatMap = flatMap
  self.filter = filter
  self.getOrElse = getOrElse
  self.get = get
  self.fold = fold
 
  return self
}
 
/**
 * Wraps a tuple (up to 5 elements) containing potentially `nullable` values and
 * returns a `Box` object, allowing you
 * to manipulate the values safely, as if there were all defined.
 * If not all values are defined, only `getOrElse` will be called.
 *
 * @param tuple A tuple (array) containing potentially nullable values
 * @returns Returns a `Box` with 4 methods, `map`, `filter`, `get` and `getOrElse`
 *
 */
export function maybeAll<T1, T2, T3, T4, T5>(
  as: [T1, T2, T3, T4, T5],
): Box<
  [NonNullable<T1>, NonNullable<T2>, NonNullable<T3>, NonNullable<T4>, NonNullable<T5>]
>
/**
 * Wraps a tuple (up to 5 elements) containing potentially `nullable` values and
 * returns a `Box` object, allowing you
 * to manipulate the values safely, as if they were all defined.
 * If not all values are defined, only `getOrElse` will be called.
 *
 * @param tuple A tuple (array) containing potentially nullable values
 * @returns Returns a `Box` with 5 methods, `map`, `filter`, `fold`, `get` and `getOrElse`
 *
 */
export function maybeAll<T1, T2, T3, T4>(
  as: [T1, T2, T3, T4],
): Box<[NonNullable<T1>, NonNullable<T2>, NonNullable<T3>, NonNullable<T4>]>
/**
 * Wraps a tuple (up to 5 elements) containing potentially `nullable` values and
 * returns a `Box` object, allowing you
 * to manipulate the values safely, as if there were all defined.
 * If not all values are defined, only `getOrElse` will be called.
 *
 * @param tuple A tuple (array) containing potentially nullable values.
 * @returns Returns a `Box` with 4 methods, `map`, `filter`, `get` and `getOrElse`
 *
 */
export function maybeAll<T1, T2, T3>(
  as: [T1, T2, T3],
): Box<[NonNullable<T1>, NonNullable<T2>, NonNullable<T3>]>
/**
 * Wraps a tuple (up to 5 elements) containing potentially `nullable` values and
 * returns a `Box` object, allowing you
 * to manipulate the values safely, as if there were all defined.
 * If not all values are defined, only `getOrElse` will be called.
 *
 * @param tuple A tuple (array) containing potentially nullable values
 * @returns Returns a `Box` with 5 methods, `map`, `filter`, `fold`, `get` and `getOrElse`
 *
 */
export function maybeAll<T1, T2>(as: [T1, T2]): Box<[NonNullable<T1>, NonNullable<T2>]>
/**
 * Wraps a tuple (up to 5 elements) containing potentially `nullable` values and
 * returns a `Box` object, allowing you
 * to manipulate the values safely, as if there were all defined.
 * If not all values are defined, only `getOrElse` will be called.
 *
 * @param tuple A tuple (array) containing potentially nullable values.
 * @returns Returns a `Box` with 5 methods, `map`, `filter`, `fold`, `get` and `getOrElse`
 *
 */
export function maybeAll<T1>(as: [T1]): Box<[NonNullable<T1>]>
/**
 * Wraps a tuple (up to 5 elements) containing potentially `nullable` values and
 * returns a `Box` object, allowing you
 * to manipulate the values safely, as if there were all defined.
 * If not all values are defined, only `getOrElse` will be called.
 *
 * @param tuple A tuple (array) containing potentially nullable values.
 * @returns Returns a `Box` with 5 methods, `map`, `filter`, `fold`, `get` and `getOrElse`
 *
 */
export function maybeAll<T>(as: T[]) {
  return !as.some(a => a === undefined || a === null)
    ? maybe<T>((as as unknown) as T)
    : maybe<T>((undefined as unknown) as T)
}