Skip to content

Commit

Permalink
adds assert docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Mock committed Jul 26, 2019
1 parent 9d2245f commit 22abaf1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
25 changes: 25 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
* [parser.lookahead(anotherParser)](#parserlookaheadanotherparser)
* [parser.lookahead(string)](#parserlookaheadstring)
* [parser.lookahead(regexp)](#parserlookaheadregexp)
* [parser.assert(condition, message)](#parserassertcondition-message)
* [parser.tie()](#parsertie)
* [parser.many()](#parsermany)
* [parser.times(n)](#parsertimesn)
Expand Down Expand Up @@ -1091,6 +1092,30 @@ Returns a parser that looks for `string` but does not consume it. Yields the sam

Returns a parser that wants the input to match `regexp`. Yields the same result as `parser`. Equivalent to `parser.skip(Parsimmon.lookahead(regexp))`.

## parser.assert(condition, message)

Passes the result of `parser` to the function `condition`, which returns a boolean. If the the condition is false, returns a failed parse with the given `message`.

```js
var evenLengthNumber =
P.digits
.assert(
function(s) {
return s.length % 2 === 0;
},
"even length number"
)
.map(Number);

evenLengthNumber.parse("34");
// { status: true, value: 34 }

evenLengthNumber.parse("1");
// { status: false,
// expected: ["even length number"],
// index: {...} }
```

## parser.tieWith(separator)

When called on a parser yielding an array of strings, yields all their strings concatenated with the `separator`. Asserts that its input is actually an array of strings.
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## version 1.13.0 (2019-07-25)

- Adds `parser.assert(condition, message)`

## version 1.12.1 (2019-07-07)

- Adds links within documentation
Expand Down

0 comments on commit 22abaf1

Please sign in to comment.