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

drb

v0.2.1

Published

Build date regexes

Downloads

7

Readme

drb

Easily build date regular expressions

drb (Date Regex Builder) is a library to aide in generating regular expressions to match dates.

Table of Contents

Purpose

Regular expressions are difficult to get right and can get messy quickly. Consider creating a regular expression to match a day of the month. A naïve attempt might be to create a regex that matches two digits:

\d{2}

But there's a problem with this - 99 matches this regular expression and is not a valid day of the month. So we must refactor this to be a little more clever:

[0-3][0-9]

This is closer to what we want, but it still matches numbers from 32 to 39.

[0-2][0-9]|3[0-1]

We're almost there. Valid dates still match correctly, and 32+ numbers are not matching, which is what we want. However, 00 generates a false-positive match when using this regex. We can finally meet all of the requirements with the following regex:

0[1-9]|[1-2][0-9]|3[0-1]

Using drb, this regex is easy to generate:

const drbMoment = drb(momentFormatter)
const regex = drbMoment('DD')
console.log(regex) // (?:(?:0[1-9]|[12][0-9]|3[01]))

Installation

Install with npm:

$ npm install --save drb

Or optionally using yarn

$ yarn add drb

Usage

Import drb and a formatter (currently only Moment.js is supported) using one of these methods:

Node.js

const { drb } = require('drb')
const { momentFormatter } = require('drb/formatters/momentFormatter')

ES6/TypeScript

import { drb } from 'drb'
import { momentFormatter } from 'drb/formatters/momentFormatter'

Now combine drb with a formatter

const drbMoment = drb(momentFormatter)

Use drbMoment to create regexes using the syntax provided by Moment.js

const regex1 = drbMoment('DD')
console.log(regex1) // (?:(?:0[1-9]|[12][0-9]|3[01]))

const regex2 = drbMoment('MM-DD-YYYY hh:mm:ss')
console.log(regex2)
// (?:(?:0[1-9]|1[0-2]))(?:-)(?:(?:0[1-9]|[12][0-9]|3[01]))(?:-)(?:\d{4})(?: )(?:(?:0[1-9]|1[0-2]))(?::)(?:(?:0[0-9]|[1-5][0-9]))(?::)(?:(?:0[0-9]|[1-5][0-9]))

License

MIT