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

string-format-validation

v2.0.2

Published

Javascript util library to format & validate strings (or Numbers)

Downloads

84

Readme

npm version semantic-release

Build Status JavaScript Style Guide Code Climate

forthebadge forthebadge

forthebadge

string-format-validation

Javascript library to format & validate strings (or Numbers)

============================

A common usage for this library is <input /> fields and validating the input and/or formatting (masking ie: phone (###) ###-####).

Built Atop Awesome Work By Others

validator.js string-mask

============================

Usage

npm install string-format-validation

Format (masking)

import StringFormatValidation from 'string-format-validation'

const userInput = '9166163600'
const format = '(###) ### - ####'
const formattedInput = StringFormatValidation.format(format, userInput)

console.log(formattedInput) // (916) 616 - 3600

Validate

import StringFormatValidation from 'string-format-validation'

const validationRules = {
  name: {
    min: 1,
    max: 20
  },
  email: {
    type: 'email'
  },
  zip: {
    size: 5
  }
}

const userInput1 = 'Cory'
const userInput2 = '[email protected]'
const userInput3 = '90210'
console.log( StringFormatValidation.validate(validationRules.name, userInput1) ) // returns `true`
console.log( StringFormatValidation.validate(validationRules.email, userInput2) ) // returns `true`
console.log( StringFormatValidation.validate(validationRules.email, userInput3) ) // returns `true` because the string length is '===' 5

Validation

Available rules: (Please open an issue to request a specific rule)

min {integer} // minimum length

max {integer} // max length

size {integer} // exact length

type

  • date
  • email
  • phone
  • creditcard
  • number

Rules must be in Object structure:

{
  min: 1, // min characters allowed
  max: 30 // max characters allowed
}

{
  size: 10,
  type: 'phone' // any of the listed 'types' above
}

Format

You can use Special Characters for your format needs.

import StringFormatValidation from 'string-format-validation'

const userInput = '9166163600'
const format = '(###) ### - ####'
const formattedInput = StringFormatValidation.format(format, userInput)

console.log(formattedInput) // (916) 616 - 3600

Character | Description --- | --- 0 | Any numbers 9 | Any numbers (Optional) # | Any numbers (recursive) A | Any alphanumeric character a | Any alphanumeric character (Optional) Not implemented yet S | Any letter U | Any letter (All lower case character will be mapped to uppercase) L | Any letter (All upper case character will be mapped to lowercase) $ | Escape character, used to escape any of the special formatting characters.

Putting It Together - Validate & Format

import StringFormatValidation from 'string-format-validation'

const rules = {
  name: {
    min: 1,
    max: 20
  },
  email: {
    type: 'email'
  },
  phone: {
    format: '(###) ### - ####'
    size: 13 // must include formatting added characters ie: '()' & '-'
  }
}

const userInputs = {
  name: 'cory',
  email: 'coryrobinson42', // not a valid email
  phone: '9166163600'
}

console.log( StringFormatValidation(rules, userInputs) )
/* returns:
  {
    name: true,
    email: false,
    phone: {
      valid: true,
      format: '(916) 616 - 3600'
    }
  }
*/

Note:

When invoking StringFormatValidation(rulesMap, userInputMap) rulesMap can be an object of keys that contain the rules for the respective key matching up to the key in userInputMap, ie:

const rulesMap = {
  // this key declares the rules (format/validation) for 'firstname' key in `userInputMap`
  firstName: {
    max: 20
  }
}

const userInputMap = {
  // this key string is matched against the rules in `rulesMap` under the same key
  firstName: 'cory'
}

StringFormatValidation(rulesMap, userInputMap)
// returns: { firstName: true } <-- validation

You can also simply pass an Object of validation/format rules as the first argument and a string as the second argument, ie:

StringFormatValidation({ type: phone, format: '(###) ###-####' }, '(916) 61')
// returns: { valid: false, format: '(916) 61'}

===========================