js-generate-password 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2022 Ahmad Joya
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Ahmad Joya
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,172 +1,280 @@
1
- <!-- prettier-ignore-start -->
2
- <!-- SOMETHING AUTO-GENERATED BY TOOLS - START -->
3
- <br />
4
- <br />
5
-
6
- <p align ="center">
7
- <a href="https://nodei.co/npm/js-generate-password" target="_blank">
8
- <img src="https://nodei.co/npm/js-generate-password.png" alt="npm Info" />
9
- </a>
10
- </p>
11
-
12
- <p align="center">
13
- <a href="http://npm.im/js-generate-password" target="_blank">
14
- <img src="https://img.shields.io/npm/v/js-generate-password.svg" alt="npm Version" />
15
- </a>
16
- <a href="http://npm.im/js-generate-password" target="_blank">
17
- <img src="https://img.shields.io/github/v/release/ahmadjoya/js-generate-password" alt="npm Release Version" />
18
- </a>
19
- <a href="http://npm.im/js-generate-password" target="_blank">
20
- <img src="https://img.shields.io/npm/dm/js-generate-password.svg" alt="npm Downloads" />
21
- </a>
22
- <a href="http://npm.im/js-generate-password" target="_blank">
23
- <img src="https://img.shields.io/npm/l/js-generate-password.svg" alt="npm Package License" />
24
- </a>
25
- </p>
26
-
27
- <p align="center">
28
- <a href="https://github.com/ahmadjoya/js-generate-password/stargazers" target="_blank">
29
- <img src="https://img.shields.io/github/stars/ahmadjoya/js-generate-password" alt="github Stars" />
30
- </a>
31
- <a href="https://github.com/ahmadjoya/js-generate-password/network/members" target="_blank">
32
- <img src="https://img.shields.io/github/forks/ahmadjoya/js-generate-password" alt="github Forks" />
33
- </a>
34
- <a href="https://github.com/ahmadjoya/js-generate-password/stargazers" target="_blank">
35
- <img src="https://img.shields.io/github/contributors/ahmadjoya/js-generate-password" alt="github Contributors" />
36
- </a>
37
- <a href="https://github.com/ahmadjoya/js-generate-password/issues" target="_blank">
38
- <img src="https://img.shields.io/github/issues/ahmadjoya/js-generate-password" alt="github Issues" />
39
- </a>
40
- </p>
41
-
42
- <br />
43
-
44
- # js-generate-password
45
-
46
- js-generate-password is usable for every typescript and typescript based project like react, vue, node, etc. it used to generate passwords that may contain alphabets, number and symbols. The options parameter enables the user to enable or disable the characters that are used to generate random password.
47
-
48
- The characters that may be choosen from are
49
-
50
- - Lowercase Characters
51
- - Uppercase Characters
52
- - Numbers
53
- - Symbols
54
-
55
- The user may also specify the minimum number of character for each type.
56
-
57
- ## Installation
58
-
59
- ```bash
60
- npm install js-generate-password
61
- ```
62
-
63
- or
64
-
65
- ```bash
66
- yarn add js-generate-password
67
- ```
68
-
69
- ## Usage
70
-
71
- For the password to be generated, **parameters** are able to pass as an optional **options** object.
72
-
73
- ```typescript
74
- import { GeneratePassword } from "js-generate-password";
75
-
76
- const password = GeneratePassword({
77
- length: 14,
78
- symbols: true,
79
- });
80
-
81
- console.log(password);
82
- ```
83
-
84
- If no parameter is passed, the default parameter will be taken as :
85
-
86
- ```typescript
87
- options = {
88
- length: 10,
89
- lowercase: true,
90
- uppercase: true,
91
- numbers: true,
92
- symbols: false,
93
- exclude: "",
94
- minLengthLowercase: 1,
95
- minLengthUppercase: 1,
96
- minLengthNumbers: 1,
97
- minLengthSymbols: 0,
98
- };
99
- ```
100
-
101
- ### Available options
102
-
103
- Any of these can be passed into the options object for each function.
104
-
105
- | Name | Description | Default Value |
106
- | ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- |
107
- | length | Integer, length of password. | 10 |
108
- | lowercase\* | Boolean, put lowercase letters in password | true |
109
- | uppercase\* | Boolean, put uppercase letters in password. | true |
110
- | numbers\* | Boolean, put numbers in password. | true |
111
- | symbols\* | Boolean, put symbols in password. | false |
112
- | exclude | String, characters to be excluded from password. | '' |
113
- | minLengthLowercase | only if **lowercase** is set to **true**, minLengthLowercase will create a password that will have minimum number of lower case characters in the password. | 1 |
114
- | minLengthUppercase | only if **uppercase** is set to **true**, minLengthUppercase will create a password that will have minimum number of upper case characters in the password. | 1 |
115
- | minLengthNumbers | only if **numbers** is set to **true**, minLengthNumbers will create a password that will have minimum number of numbers in the password. | 1 |
116
- | minLengthSymbols | only if **symbols** is set to **true**, minLengthSymbols will create a password that will have minimum number of symbols in the password. | 1 |
117
-
118
- * At least one should be true.
119
-
120
- ## Example
121
-
122
- - **No Options passed.**
123
-
124
- ```typescript
125
- const { GeneratePassword } = "js-generate-password";
126
-
127
- const password = GeneratePassword();
128
-
129
- console.log(password);
130
- ```
131
-
132
- In the above case, no parameter is passed as option. Therefore the default values will be taken - which will have alphabets, both upper and lower case, numbers, and will be of length 10 characters.
133
-
134
- ```typescript
135
- xDU6izb3PV;
136
- ```
137
-
138
- - **Length parameter passed.**
139
-
140
- ```typescript
141
- const password = GeneratePassword({ length: 25 });
142
- console.log(password);
143
- ```
144
-
145
- The password generated is like this
146
-
147
- ```typescript
148
- U4c3KpQP5UrbZgTcrqMgFeI3R;
149
- ```
150
-
151
- - **exclude parameter passed.**
152
-
153
- ```typescript
154
- options = {
155
- exclude : 'abc567XYZ';
156
- }
157
-
158
- password = GeneratePassword(options);
159
-
160
- console.log(password);
161
- ```
162
-
163
- The generated password wouldn't has none of 'abc567XYZ' characters
164
-
165
- ```typescript
166
- J9yCfttQNj;
167
- ```
168
-
169
-
170
- ## License
171
-
172
- js-generate-password is licensed under the MIT License. See the [LICENSE](https://github.com/ahmadjoya/js-generate-password#readme) file for more details.
1
+ <p align ="center">
2
+ <a href="https://nodei.co/npm/js-generate-password" target="_blank">
3
+ <img src="https://nodei.co/npm/js-generate-password.png" alt="npm Info" />
4
+ </a>
5
+ </p>
6
+
7
+ <p align="center">
8
+ <a href="https://npm.im/js-generate-password" target="_blank">
9
+ <img src="https://img.shields.io/npm/v/js-generate-password.svg" alt="npm Version" />
10
+ </a>
11
+ <a href="https://npm.im/js-generate-password" target="_blank">
12
+ <img src="https://img.shields.io/npm/dm/js-generate-password.svg" alt="npm Downloads" />
13
+ </a>
14
+ <a href="https://bundlephobia.com/package/js-generate-password" target="_blank">
15
+ <img src="https://img.shields.io/bundlephobia/minzip/js-generate-password" alt="Bundle Size" />
16
+ </a>
17
+ <a href="https://github.com/ahmadjoya/generate-password-lite/blob/main/LICENSE" target="_blank">
18
+ <img src="https://img.shields.io/npm/l/js-generate-password.svg" alt="License" />
19
+ </a>
20
+ </p>
21
+
22
+ <p align="center">
23
+ <a href="https://github.com/ahmadjoya/generate-password-lite/stargazers" target="_blank">
24
+ <img src="https://img.shields.io/github/stars/ahmadjoya/generate-password-lite" alt="GitHub Stars" />
25
+ </a>
26
+ <a href="https://github.com/ahmadjoya/generate-password-lite/network/members" target="_blank">
27
+ <img src="https://img.shields.io/github/forks/ahmadjoya/generate-password-lite" alt="GitHub Forks" />
28
+ </a>
29
+ <a href="https://github.com/ahmadjoya/generate-password-lite/issues" target="_blank">
30
+ <img src="https://img.shields.io/github/issues/ahmadjoya/generate-password-lite" alt="GitHub Issues" />
31
+ </a>
32
+ </p>
33
+
34
+ # js-generate-password
35
+
36
+ A tiny, dependency-free password generator for JavaScript and TypeScript projects — Node.js, React, Next.js, Vue, Svelte, Deno, Bun, and the browser.
37
+
38
+ Passwords may contain lowercase letters, uppercase letters, numbers and symbols. The `options` parameter lets you enable or disable each group, exclude specific characters, and require a minimum number of characters from each group.
39
+
40
+ - **Secure by default** — every character comes from the Web Crypto CSPRNG (`crypto.getRandomValues`), never `Math.random()`.
41
+ - **Zero dependencies** — nothing else is pulled into your lockfile.
42
+ - **Dual ESM + CommonJS** — `import` and `require` both work, with matching type declarations.
43
+ - **Fully typed** — TypeScript definitions ship with the package.
44
+
45
+ > **Note**
46
+ > This package is also published under the name [`generate-password-lite`](https://www.npmjs.com/package/generate-password-lite). The two packages are the same library use whichever name you already depend on.
47
+
48
+ ## Installation
49
+
50
+ ```bash
51
+ npm install js-generate-password
52
+ ```
53
+
54
+ ```bash
55
+ yarn add js-generate-password
56
+ ```
57
+
58
+ ```bash
59
+ pnpm add js-generate-password
60
+ ```
61
+
62
+ **Requirements:** Node.js 18 or newer, or any modern browser. Both provide the Web Crypto API that this package relies on.
63
+
64
+ ## Usage
65
+
66
+ ### ES modules / TypeScript
67
+
68
+ ```ts
69
+ import { GeneratePassword } from 'js-generate-password'
70
+
71
+ const password = GeneratePassword({
72
+ length: 14,
73
+ symbols: true,
74
+ })
75
+
76
+ console.log(password) // => "q7#Rk2mV$pLx8w"
77
+ ```
78
+
79
+ A default export is available too, if you prefer it:
80
+
81
+ ```ts
82
+ import GeneratePassword from 'js-generate-password'
83
+ ```
84
+
85
+ ### CommonJS
86
+
87
+ ```js
88
+ const { GeneratePassword } = require('js-generate-password')
89
+
90
+ const password = GeneratePassword({ length: 14, symbols: true })
91
+ ```
92
+
93
+ ### React
94
+
95
+ ```tsx
96
+ import { useState } from 'react'
97
+ import { GeneratePassword } from 'js-generate-password'
98
+
99
+ export function PasswordField() {
100
+ const [password, setPassword] = useState(() =>
101
+ GeneratePassword({ length: 16, symbols: true })
102
+ )
103
+
104
+ return (
105
+ <div>
106
+ <input readOnly value={password} />
107
+ <button onClick={() => setPassword(GeneratePassword({ length: 16, symbols: true }))}>
108
+ Regenerate
109
+ </button>
110
+ </div>
111
+ )
112
+ }
113
+ ```
114
+
115
+ ### Next.js
116
+
117
+ The package works in both the App Router and the Pages Router, on the server and in the browser.
118
+
119
+ ```ts
120
+ // app/api/password/route.ts
121
+ import { GeneratePassword } from 'js-generate-password'
122
+
123
+ export function GET() {
124
+ return Response.json({ password: GeneratePassword({ length: 20, symbols: true }) })
125
+ }
126
+ ```
127
+
128
+ Generating a password during render of a **server component** produces a different value on the server and on the client. Generate it in an event handler, in a route handler, or inside `useState`'s initializer in a client component.
129
+
130
+ ## Options
131
+
132
+ Every option is optional. Calling `GeneratePassword()` with no argument uses the defaults below.
133
+
134
+ ```ts
135
+ GeneratePassword({
136
+ length: 10,
137
+ lowercase: true,
138
+ uppercase: true,
139
+ numbers: true,
140
+ symbols: false,
141
+ exclude: '',
142
+ minLengthLowercase: 1,
143
+ minLengthUppercase: 1,
144
+ minLengthNumbers: 1,
145
+ minLengthSymbols: 0,
146
+ })
147
+ ```
148
+
149
+ | Name | Type | Description | Default |
150
+ | -------------------- | --------- | --------------------------------------------------------------------------------- | ------- |
151
+ | `length` | `number` | Length of the generated password. Must be a positive integer. | `10` |
152
+ | `lowercase` | `boolean` | Include lowercase letters (`a–z`). | `true` |
153
+ | `uppercase` | `boolean` | Include uppercase letters (`A–Z`). | `true` |
154
+ | `numbers` | `boolean` | Include digits (`0–9`). | `true` |
155
+ | `symbols` | `boolean` | Include symbols (see [Symbols](#symbols)). | `false` |
156
+ | `exclude` | `string` | Characters to leave out of the password. | `''` |
157
+ | `minLengthLowercase` | `number` | Minimum number of lowercase letters. Forced to `0` when `lowercase` is `false`. | `1` |
158
+ | `minLengthUppercase` | `number` | Minimum number of uppercase letters. Forced to `0` when `uppercase` is `false`. | `1` |
159
+ | `minLengthNumbers` | `number` | Minimum number of digits. Forced to `0` when `numbers` is `false`. | `1` |
160
+ | `minLengthSymbols` | `number` | Minimum number of symbols. Forced to `0` when `symbols` is `false`. | `1`\* |
161
+
162
+ At least one of `lowercase`, `uppercase`, `numbers` or `symbols` must be `true`.
163
+
164
+ \* `minLengthSymbols` defaults to `1` whenever you turn `symbols` on. Because `symbols` is `false` by default, the effective default for an untouched call is `0`.
165
+
166
+ ### Symbols
167
+
168
+ When `symbols` is enabled the following characters are used:
169
+
170
+ ```text
171
+ !#$%&'()*+,-./:;<=>?@[]^_{|}~
172
+ ```
173
+
174
+ ## Examples
175
+
176
+ **No options.** Defaults apply: 10 characters, upper and lowercase letters plus digits, no symbols.
177
+
178
+ ```ts
179
+ import { GeneratePassword } from 'js-generate-password'
180
+
181
+ console.log(GeneratePassword())
182
+ // => "xDU6izb3PV"
183
+ ```
184
+
185
+ **A longer password.**
186
+
187
+ ```ts
188
+ console.log(GeneratePassword({ length: 25 }))
189
+ // => "U4c3KpQP5UrbZgTcrqMgFeI3R"
190
+ ```
191
+
192
+ **Excluding characters.** Useful for dropping look-alikes such as `O`/`0` and `l`/`1`.
193
+
194
+ ```ts
195
+ console.log(GeneratePassword({ length: 16, exclude: 'Ol01Il' }))
196
+ // => "gT7yqW4nZbK9vRhs"
197
+ ```
198
+
199
+ **Requiring a mix.** Guarantee at least two of each group.
200
+
201
+ ```ts
202
+ console.log(
203
+ GeneratePassword({
204
+ length: 20,
205
+ symbols: true,
206
+ minLengthLowercase: 2,
207
+ minLengthUppercase: 2,
208
+ minLengthNumbers: 2,
209
+ minLengthSymbols: 2,
210
+ })
211
+ )
212
+ // => "k#9Wq2mZ$vT7pL4nRj%x"
213
+ ```
214
+
215
+ **Digits only, for a one-time code.**
216
+
217
+ ```ts
218
+ console.log(
219
+ GeneratePassword({
220
+ length: 6,
221
+ lowercase: false,
222
+ uppercase: false,
223
+ numbers: true,
224
+ symbols: false,
225
+ })
226
+ )
227
+ // => "482915"
228
+ ```
229
+
230
+ ## Errors
231
+
232
+ `GeneratePassword` throws an `Error` when the options cannot produce a valid password:
233
+
234
+ | Condition | Example |
235
+ | ---------------------------------------------------------------------------- | -------------------------------------------------------------------- |
236
+ | `length` is not a positive integer | `GeneratePassword({ length: 0 })` |
237
+ | A `minLength*` value is not a non-negative integer | `GeneratePassword({ minLengthNumbers: -1 })` |
238
+ | Every character group is disabled | `GeneratePassword({ lowercase: false, uppercase: false, numbers: false, symbols: false })` |
239
+ | The `minLength*` values add up to more than `length` | `GeneratePassword({ length: 4, symbols: true, minLengthSymbols: 5 })` |
240
+ | `exclude` removes every character of a group that still has a minimum | `GeneratePassword({ exclude: '0123456789' })` — digits are on by default |
241
+
242
+ The last case is worth calling out: excluding all digits while `numbers` is still `true` is a contradiction, so it fails loudly instead of quietly returning a password with no digits.
243
+
244
+ ## Security
245
+
246
+ Passwords are generated with `crypto.getRandomValues`, the cryptographically secure random source built into Node.js 18+ and every modern browser. Values are drawn using rejection sampling, so each character in the allowed set is equally likely — a plain modulo would skew the results toward the start of the alphabet.
247
+
248
+ The characters that satisfy your `minLength*` requirements are shuffled through the whole password rather than placed at the front, so the position of each group carries no information.
249
+
250
+ If `globalThis.crypto` is unavailable, the package throws rather than silently falling back to a predictable random source.
251
+
252
+ ## TypeScript
253
+
254
+ The `GenerateOptions` type is exported for reuse:
255
+
256
+ ```ts
257
+ import { GeneratePassword, type GenerateOptions } from 'js-generate-password'
258
+
259
+ const strongDefaults: GenerateOptions = {
260
+ length: 24,
261
+ symbols: true,
262
+ minLengthSymbols: 3,
263
+ }
264
+
265
+ const password = GeneratePassword(strongDefaults)
266
+ ```
267
+
268
+ ## Contributing
269
+
270
+ Issues and pull requests are welcome at [github.com/ahmadjoya/generate-password-lite](https://github.com/ahmadjoya/generate-password-lite).
271
+
272
+ ```bash
273
+ npm install
274
+ npm run build
275
+ npm test
276
+ ```
277
+
278
+ ## License
279
+
280
+ [MIT](https://github.com/ahmadjoya/generate-password-lite/blob/main/LICENSE) © Ahmad Joya