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

@regexp-template/core

v1.1.1-alpha

Published

A JavaScript library to generate complex regular expressions more easily.

Downloads

3

Readme

RegExpTemplate

A JavaScript library to generate complex regular expressions more easily.

npm version Conventional Commits

:warning: WARNING: This library still under development and should not be used in production!

Getting Started

Add to your project

The library can be used in the browser using the CDN:

<script src="https://cdn.jsdelivr.net/npm/@regexp-template/core/dist/regexp-template.min.js"></script>

or via npm:

npm i @regexp-template/core

Using the script tag the RegExpTemplate class becomes available on the global scope.

On Node.js can be required using:

const RegExpTemplate = require('@regexp-template/core');

Hello world example

Lets start with a simple example: concatenation of two regular expressions, /hello/ and /world/.

We first create a RegExpTemplate instance with our regular expressions as arguments:

var hwTemplate = new RegExpTemplate(/hello/, /world/);

then we can compile our template to a RegExp:

var re = hwTemplate.compile();
// outputs /helloworld/

The compile method joins the two regular expressions into a new one.

In this case we only passed two regular expressions but you can pass as many template elements (see below) as you want.

Template Elements

The template is constructed using pieces called elements. In the hello world example above, /hello/ and /world/ are two elements of that template.

Template elements have 4 types:

  • RegExp - used to add regular expression segments to the template. Also allow use of template variables.

  • string - all characters are interpreted as literal characters (except especials of strings like \n).

  • RegExpTemplate - this allows templates to be used inside another templates. See the subtemplates section.

  • extension - a custom user defined class. See the template extension section for more info.

Any other type passed to the template gets converted to a string.

Template Variables

Template Variables let you introduce new template elements inside of regular expressions.

Declaring variables

Template variables can be declared by using the notation \V{varname} inside regular expressions, where the varname is the name of the variable.

Note that in \V the V MUST be uppercase. '\v' is already used in regular expressions and strings as vertical tab.

In the code below a template is created and a variable called myVar is defined.

new RegExpTemplate(/\V{ myVar }/);

Note: whitespace can be used inside the curly braces.

variables are unique, any reference for varname refers to the same variable.

new RegExpTemplate(
  /\V{ foo } is the/,
  /same as \V{ foo } but not \V{ bar }/
);

Assigning values

Once defined a variable can then be assigned. This is done using the applyVars method.

const myTemplate = new RegExpTemplate(/\V{ myVar }/);

myTemplate.applyVars({
  myVar: "myValue"
});

The applyVars method expects an object that maps the name of the template variable to its value.

The value can be any template element.

After the method is called then all references of the variables passed are going to be replaced by the corresponding value.

const myTemplate = 
new RegExpTemplate(
  /\V{ foo } is the/,
  /same as \V{ foo } but not \V{ bar }/
);

myTemplate.applyVars({
  foo: "dog",
  bar: "cat"
});

myTemplate.compile();
// outputs /dog is the same as dog but not cat/

You don't need to set variables at once. you can do something like:

myTemplate.applyVars({foo: "dog"});
myTemplate.applyVars({bar: "cat"});

or in chain:

myTemplate
.applyVars({foo: "dog"})
.applyVars({bar: "cat"});

keep in mind however that all variables must be assigned before compiling the template:

const myTemplate = new RegExpTemplate(
  /\V{ myVar } was not assigned/
);

myTemplate.compile();
// throws an error because 'myVar' is not assigned

New variable reference in assign.

If you assign a value that contains new variables, that variables will be defined/referenced once the variable is applied.

const myTemplate = new RegExpTemplate(
  /\V{ oldVar }/
);

myTemplate
.applyVars({oldVar: /\(\V{ newVar }\)/})
.applyVars({newVar: '2'});

In the example above a regular expression defines a variable called oldVar.

The first applyVars call replaces the oldVar by a new Regular Expression that contains a variable reference wrapped in parentesis.

Once added to the template the new variable newVar is defined.

Then the second applyVars replaces newVar with the string '2'.

Since all variables (oldVar and ´newVar´) defined are assigned, the template can now be compiled.

myTemplate.compile();
// outputs /(2)/

Subtemplates

Templates can be included inside other templates.

// this template might be more complex
// but we keep it simple for the example
const numberTemplate = new RegExpTemplate(/\d+\.\d*/);

// parent template using the numberTemplate
const sumTemplate = new RegExpTemplate(
  numberTemplate, ' + ', numberTemplate
);

sumTemplate.compile();
// outputs /\d+\.\d* \+ \d+\.\d*/

This allows to split parts of a complex regular expression into smaller ones, making it more easy to read and edit. It also allows to reuse this smaller parts in different contexts that the part might be common e.q. the number template in the example above might be used in other contexts that not a sum.

The subtemplates are independent. The template variables assigned to the parent template will not propagate to the subtemplate.

When the parent template compile method is called the subtemplate compile method is called as well and used to compute the result of the parent. This means that the subtemplate must be ready to compile when the compile is called on the parent (see template variables).

Template Extensions

This feature is not implemented yet.

The RegExpTemplate provides an API that allow you to make your own template elements.

Contributing

This project is still in very early stages and its not ready to receive pull requests yet.

However you can still help by playing with the library and try to find possible bugs in it. If you do, please let me know by adding in the issues.

Also, sugestions are welcome :)

Licence

MIT