Skip to content

fail ​

fail always fails with the given expected message, without consuming any input. It's the counterpart of nothing, and is mostly useful inside when callbacks to reject input based on an already parsed value.

Usage ​

ts
const Parser = when(
  integer(),
  ({ value }) => value % 2 === 0 ? mapTo(nothing(), value) : fail('even number')
)

Success

ts
run(Parser).with('42')

{
  isOk: true,
  start: 0,
  end: 2,
  pos: 2,
  value: 42
}

Failure

ts
run(Parser).with('43')

{
  isOk: false,
  start: 2,
  end: 2,
  pos: 2,
  expected: 'even number'
}