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

@c-syn/regext

v0.0.7

Published

Transforms regular expressions into dynamic templates for flexible text generation.

Downloads

7

Readme

RegExp to Template Converter

This package provides a class to convert regular expressions into template strings. It currently supports both a Mustache and plain style. The underlying function is strongly dependent on the regexp-tree package, which is used to parse the regular expression into an abstract syntax tree (AST). Its parser module is generated from the regexp grammar, which is based on the regular expressions grammar used in ECMAScript. After parsing, the AST is then traversed to generate the template string.

Installation

npm install @c-syn/regext

Usage

import RegExT from "@c-syn/regext" 

const regexp = /.../ // Your regular expression
const type = "Mustache" // or "plain"

const template = new RegExT(regexp, type).template

[!TIP] Try this code in the RegExT Playground.

RegExT is a class that takes two parameters: a regular expression and a type. The class has a toString method that returns the template string, it contains the following properties. The convert function that is used to generate the template string is also exposed import { convert } from 'regex-template'.

| Property | Type | Description | | --- | --- | --- | | regexp | RegExp | The regular expression that was used to generate the template string. | | type | string | The type of template string that was generated. | | template | string | The template string that was generated. |

type

The type parameter is a string that specifies the type of template string that should be generated. The following types are supported. If no type, or an unknown type, is specified, type is set to Mustache.

| Type | Description | | --- | --- | | Mustache | A template string that uses the Mustache syntax. | | plain | A plain template string. |

Example

/(?:(?<=[^`\\])|^)\[(?=[^@\n\]]+\]\([^@)]*@[:a-z0-9_-]*\))(?<showtext>[^@\n\]]+)\]\((?:(?:(?<type>[a-z0-9_-]*):)?)(?:(?<term>[^@\n:#)]*?)?(?:#(?<trait>[^@\n:#)]*))?)?@(?<scopetag>[a-z0-9_-]*)(?::(?<vsntag>[a-z0-9_-]*))?\)/g

The regular expression above is used to match a markdown link with a specific format. Depending on the specified type, the regular expression will be converted into the following templates.

Mustache

[{{showtext}}]({{#type}}{{type}}:{{/type}}{{term}}{{#trait}}#{{trait}}{{/trait}}@{{scopetag}}{{#vsntag}}:{{vsntag}}{{/vsntag}})

Plain

[showtext](type:term#trait@scopetag:vsntag)

Use Cases

The generated template string can be used to convert any texts into texts that match the regular expression. Conversion can be done by replacing the template string with the corresponding values. For instance, after interpreting a text, it is converted using Handlebars to match another regular expression.

The generated template string can also be used to quickly visualize the structure of the regular expression. This can be useful when debugging or when trying to understand the structure of a regular expression.

Process

After the regular expression is parsed into an abstract syntax tree (AST), the AST is traversed to generate the template string. The following steps are walked through on every node, mostly depending on the specific type.

  1. Certain types of nodes are removed (i.e., ClassRange, Disjunction, Assertion) as they may contain Char nodes that should not be included in the template string. For instance: ranges of characters that the regular expression is supposed to match are removed.
  2. Repetition nodes are changed to Alternative nodes if the Quantifier's from and to properties are identical. Within the changed node, the Repetition node is repeated n times.
  3. Group nodes are checked for their capturing property. If it is set to true, the group is converted into a Char node based on the specified template string type. Either the name, if it exists, or the number property is used to identify the group in the template string.
  4. Alternative (or concatenation) nodes are handled according to the specified template string type. For instance, in the case of using the Mustache type, the Alternative node is wrapped in a section block (e.g., {{#person}}{{person}} exists{{/person}}).

After these steps halve been walked through, every Char node its value is concatenated, resulting in the template string.

Limitations

Due to the nature of the process used to generate the template string, the following limitations apply.

  1. Nodes of types ClassRange, Disjunction, and Assertion are removed from the AST. This means that the template string will not contain any information about these nodes.
  2. Backreferences are not (yet) supported. This means that the template string will not contain any information about backreferences.
  3. Quantifiers (repetitions) are only handled if the from and to properties of the node are identical. In this case, the number of times the expressions is supposed to be repeated is clear. If the Quantifier applies to a Character class, it is ignored; as this means 'one of' a list of Chars is supposed to picked, which can't be assumed. If the from and to properties are different, the corresponding expression is not repeated in the template string.
  4. Meta characters representing ranges of characters (e.g., ., \s, \w) are ignored. This means that the template string will not contain any information about these nodes.