Giter Site home page Giter Site logo

mattpjohnson / drb Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 1.16 MB

Build date regexes

Home Page: https://mattpjohnson.github.io/drb

License: MIT License

JavaScript 48.52% Shell 0.35% TypeScript 9.29% CSS 6.49% HTML 35.36%
regex regexp regexp-match dates momentjs regular-expression regexes

drb's Introduction

banner image

CircleCI Build Status Codecov Coverage Status SonarQube Technical Debt NPM Version NPM Bundle Size (min) NPM Bundle Size (min + gzip) NPM Type Definitions code style: prettier

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

drb's People

Contributors

dependabot[bot] avatar mattpjohnson avatar

Stargazers

 avatar

Watchers

 avatar

drb's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.