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

re-scaled

v1.3.3

Published

Helpers and utility functions for creating scalable regular expressions

Downloads

12

Readme

reScaled

Helpers and utility functions for creating readable, scalable and reusable regular expressions.

Tiny guide

When dealing with real-world regular expressions, it is usually super hard to manage them. This is because they are implemented (at least in JavaScript) in such a way that makes them intrinsicly unreadable and unscalable.

Consider the following use case:

const input = prompt();
const regex = /^(?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d)$/;

regex.test(input);

Just for fun, try to understand what's happening in the code before reading the next paragraph

Without the knowledge that the regular expression has something to do with IP addresses, you would probably struggle a lot to understand the meaning of the given regex. This happens because regular expressions heavily utilize special characters, which always decrease readability; in most cases, plain words are much easier to read than special characters.

reScaled provides handy functions for the most frequently used features, otherwise achieved by special syntax. Examples of such functions are: eitherOf(), optional(), separatedBy() etc.

Another problem with the regex in the example above is that it is highly repetitive (it is often the case with regular expressions). There is no ability to refer to the regular expression in the body of another one. Would it be the case, it would dramatically improve scalability and usability of regular expressions. Fortunately, with reScaled this is possible.

First, create smaller building blocks, atoms, from which our main regex will be comprised later:

const $Dot = /\./;
const $Digit = /\d/;
const $Bit = eitherOf(0, 1);

const $OctetHigh = /25[0-5]/; // 250 to 255
const $OctetMiddle = /2[0-4]\d/; // 200 to 249
const $OctetLow = combined(optional($Bit), optional($Digit), $Digit); // 0 to 199
// same as /[01]?\d?\d/

const $Octet = eitherOf($OctetHigh, $OctetMiddle, $OctetLow); // order matters
// same as /(?:25[0-5]|2[0-4]\d|[01]?\d?\d)/

Now, with the help of $Octet and $Dot atoms we can create regular expression for IP addresses, that is easy to read and modify:

const $IPAddress = detached(repeated.times(3)($Octet, $Dot), $Octet);
// three octets each followed by dot, then single octet without a dot
// same as /^(?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d)$/
const $IPAddress = detached(separatedBy($Dot)($Octet, $Octet, $Octet, $Octet));
// four octets with dots in between

What is great here, is that we can use $IPAddress as an atom to some other regex, if needed:

import $IPAddress from "./ip-address-regex";
import $Port from "./port-regex";
import $HumanFriendlyURL from "./human-friendly-url-regex";

const $MachineFriendlyURL = combined($IPAddress, optional(":", $Port));
const $URL = eitherOf($MachineFriendlyURL, $HumanFriendlyURL);

export default $URL;

We can build regular expressions on top of each other!

API

Follow the docs.

Try direct link if the previous didn't work.

Legacy notes

In order to see versions of the package prior to [email protected], refer to history of the package regex-utils.