npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@cursorsdottsx/metasyntax

v1.1.1

Published

Metasyntax parser in TypeScript.

Downloads

5

Readme

Banner

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

Class Metasyntax

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 '$' requires a value to be used."
//    }, number] | undefined

// The parser also works with all the other options, including `aliases` and `types`.

Metasyntax

This flavor of metasyntax is very easy to use and remember. There are only a few simple guidelines and rules.

General syntax:

<type|'literal'|"literal"|type()>
[type|'literal'|"literal"|type()]
$

Operators:

| Operator | Description | | -------- | ------------------- | | <...> | Required arguments. | | [...] | Optional arguments. |

Default types:

| Identifier | Type | Description | | ----------- | ----------- | ------------------------------------------------------------- | | string | string | A string wrapped in ' or " (escape with \). | | number | number | A number in decimal form. | | boolean | boolean | A boolean (only true or false). | | integer | number | An integer in decimal form. | | bigint | bigint | A BigInt instance. | | any | string | Any string of characters that isn't a space. | | char | string | Any character that isn't a space. | | undefined | undefined | undefined | | null | null | null | | date | Date | A date (in ISO form). | | duration | number | Any duration parsable by ms | | regex | RegExp | A regular expression. |

Symbols:

| Symbol | Description | | ------ | --------------------------------- | | $ | Placeholder for a string literal. |

More:

  • For array types, use () followed by the default type. Array types must be last and can be optional.

  • Array types can't be nested (type()() is not allowed).

  • Aliases cannot include other aliases to prevent an infinite loop.

Known bugs or issues

None! If you know one, please submit an issue to get it fixed!