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-sure

v1.0.7

Published

A simple string validator for Node.js

Downloads

496

Readme

string-sure

A simple yet powerful validation library for JavaScript strings. This library provides methods to validate email format and various string rules, including character requirements, length constraints, and specific content checks.

Installation

You can install the string-sure package via npm:

npm install string-sure

Usage

Importing the Validator

const { Validator } = require("string-sure");

Email Validation

To validate an email address, use the validateEmail method:

const isValidEmail = Validator.validateEmail('[email protected]');
const isValidEmail2 = Validator.validateEmail('example#example_npm.com');
console.log(isValidEmail); // true
console.log(isValidEmail2); //false

String Validation

To validate a string according to specified rules, use the validateString method:

const rules = {
    minLength: 5,
    maxLength: 20,
    uppercase: {
        minLength: 1,
        maxLength: 5
    },
    lowercase: {
        minLength: 1,
        maxLength: 5
    },
    specialChars: {
        allowed: ["@", "&", "_", "!"],
        compulsory: ["@", "_"]
    },
    number: {
        minLength: 3,
        maxLength: 5
    }
};

const isValidString = Validator.validateString('Wow@_123', rules);
console.log(isValidString); // true

Logging Validation Results

You can control the logging of validation results using the logging parameter in the validateString method. Set it to true to enable logging:

console.log(Validator.validateString("Wow@_123", rules, true)); // Logs validation results
console.log(Validator.validateString("Wow@_123", rules)); // No logs

Validation Rules

The validateString method accepts the following rules:

  • minLength: Minimum length of the string (if omitted, minimum length will be 0).
  • maxLength: Maximum length of the string (if omitted, maximum length will be Infinity).
  • uppercase:
    • minLength: Minimum number of uppercase letters.
    • maxLength: Maximum number of uppercase letters.
  • lowercase:
    • minLength: Minimum number of lowercase letters.
    • maxLength: Maximum number of lowercase letters.
  • specialChars:
    • allowed: Array of allowed special characters.
    • compulsory: Array of compulsory special characters that must be present in the string. This array must be a subset of allowed array.
    • minLength: Minimum number of characters.
    • maxLength: Maximum number of characters.
  • number:
    • minLength: Minimum number of digits.
    • maxLength: Maximum number of digits.

Examples

Here are some example cases for validating strings:

console.log(Validator.validateString("Wow@_123", rules)); // true
console.log(Validator.validateString("Wow@123", rules)); // false (missing compulsory special character)
console.log(Validator.validateString("WOW@_123", rules)); // false (missing lowercase)
console.log(Validator.validateString("Wow", rules)); // false (too short)

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any enhancements or bug fixes.

License

This project is licensed under the MIT License.

Contact

For any questions or feedback, please contact me on email