Skip to content

optional

The given parser may or may not match. This combinator can not fail.

Available since: v1.0.0-alpha.4

Type declaration

const optional: (parser: Parser, name?: string) => Parser;

Example

In this example we are going to parse the string "Hello" which may not be present.

import { optional, string } from "rudus";
const parser = optional(string("Hello"));
const result = parser.run("World");

The result of the parser above will be:

{
"input": "World",
"isError": false,
"offset": 0,
"result": null
}