text: import { TMonad } from './t-monad.js'
  
  /**
   * Returns a function that then takes a TMonad. If that value is invalid, it returns the replacement instead
   * @function TMonad
   * @param {any} replacement - The value to be returned if the resulting funtion is invalid
   * @return {any} Either the original value or the replacement
   * @example
   * 
   * const ifInvalid = IfInvalid('hey')
   * const value = ToNumber('hola')
   * const result = ifInvalid(value).value // 'hey'
   */
  
  export function IfInvalid(replacement) {
  
      /** Return a new function that takes a TMonad */
      return function IfInvalidInternal(value) {
          const result = TMonad(value)
  
          /** If the stop flag is true or the TMonad is valid, continue */
          if (result.stop || !!result.valid) { return result }
  
          /** If not valid, return replacement */
          return TMonad(replacement)
      }
  }