Skip to content

count ​

count combinator applies parser exactly n times and collects the values into an array. Fails with the first failure of parser. Resolves to an empty array if n is less than one.

Usage ​

Combined with when, count makes it easy to parse length-prefixed data, e.g. netstring-like fields:

ts
const Parser = when(
  first(integer(), string(':')),
  ({ value }) => map(count(any(), value), (chars) => chars.join(''))
)

Success

ts
run(Parser).with('4:abcd')

{
  isOk: true,
  start: 0,
  end: 6,
  pos: 6,
  value: 'abcd'
}

Failure

ts
run(Parser).with('4:ab')

{
  isOk: false,
  start: 4,
  end: 4,
  pos: 4,
  expected: 'any @ reached the end of input'
}