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

con-reg-exp

v0.2.4

Published

Convenient Regular Expressions - a different approach to the regular expressions syntax.

Downloads

6

Readme

Convenient Regular Expressions

Web Page 〛  〚 Tutorial 〛  〚 Docs 〛  〚 Web Demo 〛 

[!WARNING] This is a project in an early stage of development. The main functionality is done. More work is needed especially in the context of documentation and tests.

Regular expression syntax redefined

The "Convenient Regular Expressions" give an alternative syntax for regular expressions. The main goal is to introduce a syntax that is easily manageable in complex regular expressions.

The module is still using a standard JavaScript's RegExp object. Essentially, it is a runtime transpiler that converts "Convenient Regular Expression" into a classic regular expression.

Usage

  1. Install with npm:

    npm install con-reg-exp
  2. Import it:

    import cre from "con-reg-exp";
  3. Use it as a tagged template:

    const myRegExp = cre`"Write your expression here."`;

You can find more details on usage in the documentation.

If you want to start using it, go to the tutorial.

Benefits

  • You can use whitespaces

    Organize your expression structure with spaces, new lines, and indentation.

  • You can use comments

    A good comment can clarify an expression a lot making it easier to understand later.

  • You are using words

    The words are significantly more readable than some arbitrarily selected special characters.

  • Maintenance is simpler

    If you want to make a change in a well-structured expression, you simply do it. With complex classic regular expressions, first, you have to parse it in your head, track brackets, and groups, and divide it into pieces.

  • Code review is faster and more accurate

    It is easier to understand well-structured expressions and changes in such expressions. Have you ever tried to review changes in regular expressions that are longer than 100 characters?

  • You can reuse the expressions

    You can put pieces of your expression into variables and reuse them multiple times later. It is like splitting your code into functions.

  • You are using sane syntax

    Long and complex regular expressions look like some esoteric programming languages created just to make the code unreadable.

Some example

Let's play a game. You have two functions. Try to understand what they are doing.

Classic RegExp implementation:

const pattern = /^\s*\[\s*(?:(?:-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?\s*,\s*)*-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?\s*)?\]\s*$/;

function guessWhatDoesThisFunctionDo(text) {
    return pattern.test(text);
}

Convenient Regular Expressions:

import cre from "con-reg-exp";

const number = cre`
    optional "-";               // Sign
    {                           // Integral part
        "0";                    // Zero is special case
    } or {
        [1-9], repeat digit;    // Everything above zero
    }
    optional {                  // Optional factional part
        ".";
        at-least-1 digit;
    }
    optional {                  // Optional exponent part
        [eE];
        optional [+-];
        at-least-1 digit;
    }
`;

const ws = cre`repeat whitespace`;

const pattern = cre`
    begin-of-text, ${ws};      // Trim leading whitespaces
    "[", ${ws};                // Begin of array
    optional {                 // Optional, because array can be empty
        repeat {               // Numbers with trailing comma
            ${number}, ${ws};
            ",", ${ws};
        }
        ${number}, ${ws};      // Last number has no comma
    }
    "]", ${ws};                // End of array
    end-of-text;
`;

function guessWhatDoesThisFunctionDo(text) {
    return pattern.test(text);
}

Which one was easier to understand? I'm assuming that you don't know the convenient regular expressions syntax yet. Were you able to guess despite this?

Click the link to see the answer.

Imagine that you want to modify the function to allow strings in the array.