Skip to content

Commit

Permalink
chore(core): add bulk password option and change the way of getting c…
Browse files Browse the repository at this point in the history
…haracters

Also, update the readme, description, and keywords accordingly
  • Loading branch information
SMAKSS committed Nov 19, 2023
1 parent 5fd7da6 commit 5a3b6c0
Show file tree
Hide file tree
Showing 5 changed files with 329 additions and 346 deletions.
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
package.json
.cache
.shadowenv.d
.vscode
Expand Down
43 changes: 25 additions & 18 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

![npm](https://img.shields.io/npm/v/@smakss/password-generator) ![NPM](https://img.shields.io/npm/l/@smakss/password-generator) ![npm](https://img.shields.io/npm/dt/@smakss/password-generator) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/min/@smakss/password-generator)

Creating secure passwords for users might be a big challenge for developers. This package will help you generate secure random passwords easily. You can choose from a predefined set of character types or define your own characters with a specified or random length. This package is written using ES6+ syntax, so if you're using older standards of JavaScript, a transpiler like Babel may be required.
Generating secure and random passwords is a common necessity in today's digital world. The `@smakss/password-generator` package simplifies this task, providing an easy-to-use tool for creating random passwords. Whether you need a single password or a bulk set, this package offers the flexibility to include various character types or use a custom set of characters.

## Demo

Check out the [working demo](https://runkit.com/smakss/password-generator) on RunKit.

or
Explore the functionality in a live environment on CodeSandbox:

[![View @smakss/password-generator](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/smakss-password-generator-o9ev4k?fontsize=14&hidenavigation=1&theme=dark)

## How It Works
## Installation

To install the package, run:

Expand All @@ -34,18 +32,19 @@ For ECMAScript modules:
import PasswordGenerator from '@smakss/password-generator';
```

To use it within your application:
## Usage

The `PasswordGenerator` function accepts an options object with the following optional parameters:
The `PasswordGenerator` function accepts an options object with the following parameters:

- `length` (`number`, Default: random between 1-20): The length of the desired password.
- `lowerIncluded` (`boolean`, Default: `true`): Include lowercase characters.
- `capsIncluded` (`boolean`, Default: `true`): Include uppercase characters.
- `numIncluded` (`boolean`, Default: `true`): Include numeric characters.
- `specIncluded` (`boolean`, Default: `true`): Include special characters.
- `characters` (`Array<string | number>`): Specify custom characters for the password.
- `includeLower` (`boolean`, Default: `true`): Include lowercase characters.
- `includeCaps` (`boolean`, Default: `true`): Include uppercase characters.
- `includeNums` (`boolean`, Default: `true`): Include numeric characters.
- `includeSpecs` (`boolean`, Default: `true`): Include special characters.
- `characters` (`string`): Specify a custom string of characters for the password.
- `numberOfPasswords` (`number`): Number of passwords to generate in bulk.

## Examples of Usage
## Examples

### Generate a password with default parameters

Expand All @@ -66,23 +65,31 @@ const password = PasswordGenerator({ length: 10 });
```js
const password = PasswordGenerator({
length: 10,
lowerIncluded: false,
numIncluded: false
includeLower: false,
includeNums: false
});
// Result might be something like: ":+U,G:JNXL"
```

### Generate a password using only specified characters

```js
const password = PasswordGenerator({ length: 10, characters: ['a', 1, '~'] });
const password = PasswordGenerator({ length: 10, characters: 'a1~' });
// Result might be something like: "~a~a1~~~a~"
```

### Generate a bulk set of passwords

```js
const passwords = PasswordGenerator({ numberOfPasswords: 5 });
// Results in an array of 5 random passwords
console.log(passwords);
```

## Contributing

If you're interested in contributing to this project, please see [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines and details.
Contributions to enhance `@smakss/password-generator` are welcome. See [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines.

## Code of Conduct

We value the well-being of all contributors and users. To ensure this project remains welcoming to everyone, please refer to our [Code of Conduct](./CODE_OF_CONDUCT.md).
Our commitment to providing a welcoming and inclusive environment is outlined in our [Code of Conduct](./CODE_OF_CONDUCT.md).
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"bugs": {
"url": "https://github.com/SMAKSS/password-generator/issues"
},
"description": "Generate random password.",
"description": "Securely generate random passwords with @smakss/password-generator. Customizable for length, characters, and complexity, it's perfect for both individual and bulk password creation.",
"devDependencies": {
"@commitlint/cli": "^18.2.0",
"@commitlint/config-conventional": "^18.1.0",
Expand Down Expand Up @@ -32,6 +32,11 @@
],
"homepage": "https://github.com/SMAKSS/password-generator#readme",
"keywords": [
"password-creation",
"secure-passwords",
"random-passwords",
"custom-passwords",
"bulk-password-generator",
"npm",
"yarn",
"Random",
Expand Down Expand Up @@ -61,5 +66,5 @@
},
"type": "module",
"types": "lib",
"version": "2.0.0"
"version": "3.0.0-beta.0"
}
106 changes: 39 additions & 67 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,21 @@
/** Array of numeric characters */
const num: string[] = '0123456789'.split('');

/** Array of uppercase alphabetic characters */
const caps: string[] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');

/** Array of lowercase alphabetic characters */
const lower: string[] = 'abcdefghijklmnopqrstuvwxyz'.split('');

/** Array of special characters */
const spec: string[] = [
'@',
'%',
'+',
'\\',
'/',
"'",
'!',
'#',
'$',
'^',
'?',
':',
',',
')',
'(',
'}',
'{',
']',
'[',
'~',
'-',
'_',
'.'
];
// Character Sets
const num = '0123456789'.split('');
const caps = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
const lower = 'abcdefghijklmnopqrstuvwxyz'.split('');
const spec = "@%+\\/'!#$^?:,)(}{][~-_.".split('');

/**
* Options for the PasswordGenerator function.
* @interface
*/
interface PasswordGeneratorOptions {
length?: number;
lowerIncluded?: boolean;
capsIncluded?: boolean;
numIncluded?: boolean;
specIncluded?: boolean;
characters?: (string | number)[];
includeLower?: boolean;
includeCaps?: boolean;
includeNums?: boolean;
includeSpecs?: boolean;
characters?: string;
numberOfPasswords?: number;
}

/**
Expand All @@ -59,49 +30,50 @@ function getRandomIndex(max: number): number {
/**
* Generates a random password based on the provided options.
* @param options Configuration options for generating the password.
* @param options.length The length of the password.
* @param options.lowerIncluded Whether to include lowercase characters.
* @param options.capsIncluded Whether to include uppercase characters.
* @param options.numIncluded Whether to include numeric characters.
* @param options.specIncluded Whether to include special characters.
* @param options.characters A custom array of characters to be used for generating the password.
* @returns A string representing the generated password.
* @returns A string representing the generated password or an array of passwords if numberOfPasswords is specified.
*
* @example
* // Generates a 12-character password with all options enabled
* const password = PasswordGenerator({ length: 12 });
* console.log(password);
*
* @example
* // Generates a password using a custom set of characters
* const customChars = 'abcdef'.split('');
* const password = PasswordGenerator({ characters: customChars });
* console.log(password);
* // Generates 5 passwords using a custom set of characters
* const passwordBulk = PasswordGenerator({ characters: 'abcdef', numberOfPasswords: 5 });
* console.log(passwordBulk);
*/
function PasswordGenerator({
length = Math.ceil(Math.random() * 20),
lowerIncluded = true,
capsIncluded = true,
numIncluded = true,
specIncluded = true,
characters = []
}: PasswordGeneratorOptions = {}): string {
includeLower = true,
includeCaps = true,
includeNums = true,
includeSpecs = true,
characters = '',
numberOfPasswords = 1
}: PasswordGeneratorOptions = {}): string | string[] {
const customCharacters = characters?.split('') || [];

const allowedCharacters =
characters.length > 0
? characters
? customCharacters
: [
...(lowerIncluded ? lower : []),
...(capsIncluded ? caps : []),
...(numIncluded ? num : []),
...(specIncluded ? spec : [])
...(includeLower ? lower : []),
...(includeCaps ? caps : []),
...(includeNums ? num : []),
...(includeSpecs ? spec : [])
];

const password = Array.from(
{ length },
() => allowedCharacters[getRandomIndex(allowedCharacters.length)]
).join('');
const generatePassword = () =>
Array.from(
{ length },
() => allowedCharacters[getRandomIndex(allowedCharacters.length)]
).join('');

if (numberOfPasswords > 1) {
return Array.from({ length: numberOfPasswords }, generatePassword);
}

return password;
return generatePassword();
}

export default PasswordGenerator;
Loading

0 comments on commit 5a3b6c0

Please sign in to comment.