value
value(p: Parser, x: *, m?: string): Parser
Applies a parser and, if it succeeds, returns another value.
x can be anything, so value is one of the few combinators that can return something other than a string.
value(p, x) is an optimized implementation of chain(p, () => always(x)).
Example
const parser = value(string('""'), '"')
const s = parse(parser, '""')
console.log(status(s)) // "ok"
console.log(success(s)) / "\""
const f = parse(parser, '"')
console.log(status(f)) // "fail"
console.log(failure(f)) // Parse error at (line 1, column 1):
//
// "
// ^
// Expected '""'
In this example, if the parser string('""') succeeds, then value returns a single character ("). This parser could be used for detecting a quote inside a quoted string in CSV, for instance, where quotation marks are escaped by doubling them.
Parameters
p: The parser that gets applied to the input.x: The value forvalueto return ifpsucceeds.m: The optional expected error message that will take the place of the default error message.
Success
- Succeeds if
psucceeds. In this case, the result ofpis discarded andxis returned instead.
Failure
- Fails if
pfails.
Fatal Failure
- Fails fatally if
pfails fatally.
Throws
- Throws an error if
pis not a parser. - Throws an error if
mexists and is not a string.