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

regexbuddy

v2.0.1

Published

Implement regex functions with ease in JavaScript

Downloads

139

Readme


Currently, regexbuddy has functions for regex-based email validation, password validation, case conversion, as well as array duplicate functions. The array functions - along with other features - are being added, and a new version is released weekly.

Features

1. Email Validation

2. Password Validation

3. Case Conversion

4. Array De-Duplication

Installation

Install in your project locally:

npm install regexbuddy

Implement into your project

Step 1: Import into your project

NOTE: Since regexbuddy has function names that are considered generic (i.e. password(input).validate()), it's recommended to import it like this:

import * as regexBuddy from "regexbuddy";

Step 2: That way, you can use it in your code like this

regexBuddy.password(input).validate();

COMMENT: While having generic-sounding function names is a (rightfully) contested topic, doing so makes the syntax feel more natural. And implementing it like the example above makes it easier to find where regexbuddy is used in your code, because the functions are prefixed with regexBuddy.

Available Functions and Methods

Email Validation

For email validation, you can simply use:

regexBuddy.email(input).validate();

This runs a regex function to ensure the input is a valid email address. The return value from the above method is:

{
  valid: Boolean
  message: `Error message` || null
}

Email Validation Options

You can also customize validation criteria with permitted and restricted values.

For instance, if you wanted to only allow email addresses with a @yourcompanyname email address, you would do the following:

regexBuddy.email(input).validate({ permitted: "yourcompanyname" });

You can do the same with email domains you explicitly do not want to permit, like this:

regexBuddy.email(input).validate({ restricted: "aol" });

The permitted and restricted values let you pass in either a String or Array. So you can also do this:

regexBuddy.email(input).validate({ restricted: ["aol", "hotmail"] });

Password Validation

For password validation, you can simply use:

regexBuddy.password(input).validate();

This takes the input value you pass in as an argument, and validates against the default password requirements.

Default requirements are that a password must contain (with the option name and data type for overwriting defaults):

  1. At least one uppercase letter (name: requireUpperCase, type: boolean)
  2. At least one lowercase letter (name: requireLowerCase, type: boolean)
  3. At least one number (name: requireNumber, type: boolean)
  4. At least one special character (name: requireSpecialCharacter, type: boolean)
  5. A minimum of 5 characters (name: minLength, type: number)

The default requirements can be overwritten by passing in your requirements in the validate function like this:

regexBuddy.password(input).validate({ minLength: 8, requireSpecialCharacter: false });

NOTE: Options that are ignored will still have their default values used. So in the example above, a password must have at least 8 characters and does not need to have a number. But it must also still include an uppercase letter, lowercase letter, and a special character.

NOTE: The password validation method returns an object structured like this:

{
  valid: Boolean,
  errors: [ validationErrors ] || null
}

This gives you the option of serving the client an array of the criteria not met when creating a password, or simply using the password validation method in a conditional expression. And the valid property was added for those who prefer using explicit conditional evaluation over implicit "truthy" methods for something like a password.

An example of how to use this in your code would be:

const passwordCheck = regexBuddy.password(input).validate({ minlength: 8, requireSpecialCharacter: false });

This would let you reference the passwordCheck variable in a simple way, like these examples:

// If a password is invalid
if (passwordCheck.errors) {
  // You can then map the array of errors in passwordCheck.errors to a DOM object, like a toast, modal, etc.
  ...
}

Or implement as a simple, explicit conditional in your corresponding template file, like this example, where a submit button is disabled when there are any errors with the user's input value:

<button type="submit" disabled={!passwordCheck.valid}>Submit</button>

You can also ensure strict password matching when requiring someone to re-enter their password with the matches() method, like this:

regexBuddy.password(password1).matches(password2);

This simply returns true or false.

Case Conversion

With regexbuddy, you can convert any string into either pascal, camel, kebab, snake, or sql case. You can do this with the following method:

regexBuddy.convertCase("your String goes Here")

Using the base syntax returns an object that looks like this:

{
  original: 'your String goes Here',
  camel: 'yourStringGoesHere',
  kebab: 'your-string-goes-here',
  pascal: 'Your String Goes Here',
  snake: 'your_string_goes_here',
  sql: 'YOUR_STRING_GOES_HERE'
}

If you need to use different cases for a value throughout your code, you can utilize case conversion like this:

const dName = regexBuddy.convertCase(displayName)

And wherever you use the dName variable, you can specifiy the case like this:

<h2 className="card-header">{dName.pascal}</h2>

The case converter also accepts a designated case as an option. You can do so like this:

regexBuddy.convertCase("your String goes Here", { case: 'camel' })

The return value of this is simply the string converted into the desired case.

NOTE: This is a much more performant way to utilize the case converter, and is recommended, when you can.