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

@doreentseng/passwd

v1.3.1

Published

Random password generator and validation.

Downloads

444

Readme

@doreentseng/passwd

Random Password Generator and Validation.

NPM JavaScript Style Guide

Features

Random Password Generator

  • Fixed Length
  • Maximum Length and Minimum Length
  • Custom Characters

Password Validation

  • Validata Empty
  • Validate Length
  • Validate Characters
  • Custom Error Messages

DEMO

Link: https://codesandbox.io/s/random-password-generator-npm-o1253

Install

npm install --save @doreentseng/passwd

Usage

Generation

import React, { useEffect, useState } from 'react';
import { generate } from '@doreentseng/passwd';

const Example = () => {
  const [passwd, setPasswd] = useState('');

  const onClickGenerate = () => {
    setPassword(
      generatePassword({
        uppercase: true, // default is true so this line can be removed
        lowercase: false, // the password will not contain any lowercase
        // numbers: true, // default is true so this line can be removed
        symbols: "_-@!", // custom symbols
        maxLength: 20,
        minLength: 8
      })
    );
  };

  const handleChange = e => {
    const value = e.target.value;
    setPasswd(value);
  }

  return (
    <div>
      <input value={passwd} onChange={handleChange} />
      <br />
      <button onClick={onClickGenerate}>Generate</button>
    </div>
  )
}
export default Example;

Validation

validation({ value, ...rules }); // error codes array: ["101", "103"]
import React, { useEffect, useState } from 'react';
import { generate } from "./password";

const Example {
  const [password, setPassword] = useState("");
  const [error, setError] = useState([]);

  const maxLength = 20;
  const minLength = 8;

  const rules = useMemo(
    () => ({
      uppercase: true, // default is true so this line can be removed
      lowercase: true, // the password will not contain any lowercase
      // numbers: true, // default is true so this line can be removed
      symbols: "_-@!", // custom symbols
      maxLength,
      minLength
    }),
    []
  );

  /** 
   * validation returns error codes, you can edit your error messages
   * e.g. ['101', '103']
   */
  const handleError = (codes) => {
    let errors = [];
    for (let i = 0; i < codes.length; i++) {
      switch (codes[i]) {
        case "101":
          errors.push("Required");
          break;
        case "102":
          if (minLength === maxLength) {
            errors.push(`Must be ${minLength} characters.`);
          } else {
            errors.push(`Must be between ${minLength}~${maxLength} characters.`);
          }
          break;
        case "103":
          errors.push("At least one uppercase.");
          break;
        case "104":
          errors.push("At least one lowercase.");
          break;
        case "105":
          errors.push("At least one number.");
          break;
        case "106":
          errors.push("At least one symbol.");
          break;
        case "107":
          errors.push("No other characters are allowed.");
          break;
        default:
          break;
      }
    }
    return errors;
  };

  useEffect(() => {
    const errorCodes = validate({
      value: password,
      ...rules
    });
    const errorList = handleError(errorCodes);
    setError(errorList);
  }, [password, rules]);

  const handleChange = (e) => {
    const value = e.target.value;
    setPassword(value);
  };

  return (
    <div id="main">
      <h3>Random password generator</h3>
      <ol>
        <li>Must be between 8~20 characters.</li>
        <li>At least one upper-case, one lower-case and one number.</li>
        <li>
          At least one of these special characters: - or _. No other special
          characters are allowed.
        </li>
      </ol>
      <div id="container-passwd">
        <input value={password} onChange={handleChange} />
      </div>
      <p style={{ textAlign: "center" }}>Password length: {password.length}</p>

      {!!error && error.length > 0 ? (
        <ul>
          {error.map((item, i) => (
            <li key={i} style={{ color: "red" }}>
              {item}
            </li>
          ))}
        </ul>
      ) : (
        <p style={{ textAlign: "center", color: "green" }}>OK</p>
      )}
    </div>
  );
}

Props

*Notice! If you use generate() and validate() on the same password, you should make sure their props are the same.

Generator Props

*Notice! If maxLength is the same with minLength, the password will get a fixed length.

| Name | Required | Default Value | Type | Description | |-----------|:--------:|:-------------:|:--------------:|-------------| | uppercase | | true | Boolean/String | By default true, the password will contain uppercase ABCDEFGHJKLMNPQRZTUVWXYZ (without I, O). If set false, the password will not contain any uppercase character. You can customize it like {uppercase: "ABC"} | | lowercase | | true | Boolean/String | By default true, the password will contain lowercase abcdefghijkmnopqrstuvwxyz (without l). If set false, the password will not contain any lowercase character. You can customize it like {lowercase: "abc"} | | numbers | | true | Boolean/String | By default true, the password will contain numbers 123456789 (without 0). If set false, the password will not contain any number. You can customize it like {numbers: "012"} | | symbols | | true | Boolean/String | By default true, the password will contain symbols -_. If set false, the password will not contain any symbol. You can customize it like {symbols: "-_@!"} | | maxLength | √ | | Integer | The maximum length of the password. At least 4. | minLength | √ | | Integer | The maximum length of the password. At least 4.

Validation Props

*Notice! Props uppercase, lowercase, numbers and symbols are totally the same as in Generator Props.

| Name | Required | Default Value | Type | Description | |-----------|:--------:|:-------------:|:--------------:|----------------------------------| | value | √ | | String | Password value. | | uppercase | | true | Boolean/String | The same as Generator Props. | | lowercase | | true | Boolean/String | The same as Generator Props. | | numbers | | true | Boolean/String | The same as Generator Props. | | symbols | | true | Boolean/String | The same as Generator Props. | | maxLength | √ | | Integer | The same as Generator Props. | | minLength | √ | | Integer | The same as Generator Props. |

Validation Error Codes

| Code | Description | |------|-----------------------------------| | 101 | The password is required. | | 102 | Length does not match. | | 103 | Uppercase does not match. | | 104 | Lowercase does not match. | | 105 | Numbers does not match. | | 106 | Symbols does not match. | | 107 | Other characters are not allowed. |

License

MIT ©


This hook is created using create-react-hook.