README
@cursorsdottsx/metasyntax
Metasyntax parser in TypeScript.
Parses strings according to a given metasyntax, providing a cleaner way to parse user input into numbers, booleans, dates, and other objects. Easy to learn, easy to use, and a configurable behaviour topped with custom types and aliases.
Installation and Usage
# Install with NPM:
$ npm install @cursorsdottsx/metasyntax
# or alternatively, with Yarn:
$ yarn add @cursorsdottsx/metasyntax
// Available with CommonJS:
const Metasyntax = require("@cursorsdottsx/metasyntax");
// or with ESM:
import Metasyntax from "@cursorsdottsx/metasyntax";
Documentation
Metasyntax
Class
new Metasyntax(metasyntax, options?)
metasyntax
– The metasyntax to parse.options
– Options for the instance.$
– Placeholder for a string literal.types
– Define custom types to use.[type: string]: RegExp | [RegExp, (match: string) => unknown]
aliases
– Define custom type aliases.[alias: string]: string
strict
– Strict parsing.partial
– Partial parsing.case
– Case insensitive parsing.
Creates a new Metasyntax
instance.
Metasyntax.prototype.test(target)
target
– Target to test.
Returns true if the target matches the metasyntax, false if otherwise.
Metasyntax.prototype.exec(target)
target
– Target to parse.
Returns an array of parsed values from the target.
Typings
This library comes with default typings that are extremely general but work well enough for both JavaScript and TypeScript users.
However, @cursorsdottsx/metasyntax
comes with a typings file that parses metasyntax using types to provide fine and accurate types for Metasyntax
.
This parser will obviously slow down your language server, so it is optional. To opt in, add ./node_modules/@cursorsdottsx/metasyntax/types.d.ts
to your include
paths in your tsconfig.json
.
You can also fiddle with the typings
and typeRoots
options if the above does not work.
Finally, if all has failed, there is still the triple-slash directive to reference it.
If you are using the optional parser, please use as const
with the options for finer types.
// Examples of the parser in action:
new Metasyntax("[string] $ <number>", {
$: "dollar",
} as const).exec("'some string' dollar 1234");
// => [string | undefined, "dollar", number] | undefined
new Metasyntax("[string] $ <number>").exec("'some string' dollar 1234");
// => [string | undefined, {
// error: "TypeError: Special symbol '