1.0.1 • Published 1 year ago

gpassword v1.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

generatePassword Function

This repository contains a function generatePassword which generates a password based on various criteria.

Installation

To use the generatePassword function in your project, first install the required packages:

npm install gpassword

Usage

To use the generatePassword function, import it into your project as follows:

import { generatePassword } from "gpassword";

Function Parameters

The generatePassword function accepts an object with the following optional properties:

PropTypeDescriptionDefault Value
lengthnumberLength of the generated password8
numbersbooleanInclude numbers in the passwordtrue
lowercasebooleanInclude lowercase letters in the passwordtrue
uppercasebooleanInclude uppercase letters in the passwordtrue
containSpecialCharactersbooleanInclude special characters in the passwordtrue
uniqueCharactersbooleanEnsure all characters in the password are uniquefalse

Example Usage

Here are some examples of how to use the generatePassword function:

// Generate a default password
const password1 = generatePassword({});
console.log(password1); // Example output: aB1@cD2#

// Generate a password with specified length
const password2 = generatePassword({ length: 12 });
console.log(password2); // Example output: aB1@cD2#eF3$

// Generate a password with only numbers
const password3 = generatePassword({
  numbers: true,
  lowercase: false,
  uppercase: false,
  containSpecialCharacters: false,
});
console.log(password3); // Example output: 12345678

// Generate a password with unique characters
const password4 = generatePassword({ length: 10, uniqueCharacters: true });
console.log(password4); // Example output: aB1@cD2#eF

Test Coverage

The function is thoroughly tested to cover various scenarios:

Test CaseDescription
Default settingsGenerates a password with length 8, including numbers, lowercase, uppercase, and special characters
Specified lengthGenerates a password with a specified length
Numbers onlyGenerates a password containing only numbers
No character typesThrows an error if no character types are included
Length smaller than conditionsThrows an error if the length is smaller than the number of required conditions
Unique charactersGenerates a password with unique characters
Exceeding unique lengthThrows an error if the unique character length exceeds available characters
No special charactersGenerates a password without special characters
No uppercase charactersGenerates a password without uppercase characters
No lowercase charactersGenerates a password without lowercase characters
Only special charactersGenerates a password containing only special characters
Mix with unique charactersGenerates a password with a mix of all character types and unique characters

To run the tests, use the following command:

npm run test

This will execute all the test cases and verify the functionality of the generatePassword function.

1.0.1

1 year ago

1.0.0

1 year ago