generate-password-lite 1.0.2 → 1.1.1

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,171 +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/generate-password-lite" target="_blank">
8
- <img src="https://nodei.co/npm/generate-password-lite.png" alt="npm Info" />
9
- </a>
10
- </p>
11
-
12
- <p align="center">
13
- <a href="http://npm.im/generate-password-lite" target="_blank">
14
- <img src="https://img.shields.io/npm/v/generate-password-lite.svg" alt="npm Version" />
15
- </a>
16
- <a href="http://npm.im/generate-password-lite" target="_blank">
17
- <img src="https://img.shields.io/github/v/release/ahmadjoya/generate-password-lite" alt="npm Release Version" />
18
- </a>
19
- <a href="http://npm.im/generate-password-lite" target="_blank">
20
- <img src="https://img.shields.io/npm/dm/generate-password-lite.svg" alt="npm Downloads" />
21
- </a>
22
- <a href="http://npm.im/generate-password-lite" target="_blank">
23
- <img src="https://img.shields.io/npm/l/generate-password-lite.svg" alt="npm Package License" />
24
- </a>
25
- </p>
26
-
27
- <p align="center">
28
- <a href="https://github.com/ahmadjoya/generate-password-lite/stargazers" target="_blank">
29
- <img src="https://img.shields.io/github/stars/ahmadjoya/generate-password-lite" alt="github Stars" />
30
- </a>
31
- <a href="https://github.com/ahmadjoya/generate-password-lite/network/members" target="_blank">
32
- <img src="https://img.shields.io/github/forks/ahmadjoya/generate-password-lite" alt="github Forks" />
33
- </a>
34
- <a href="https://github.com/ahmadjoya/generate-password-lite/stargazers" target="_blank">
35
- <img src="https://img.shields.io/github/contributors/ahmadjoya/generate-password-lite" alt="github Contributors" />
36
- </a>
37
- <a href="https://github.com/ahmadjoya/generate-password-lite/issues" target="_blank">
38
- <img src="https://img.shields.io/github/issues/ahmadjoya/generate-password-lite" alt="github Issues" />
39
- </a>
40
- </p>
41
-
42
- <br />
43
-
44
- # generate-password-lite
45
-
46
- generate-password-lite 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 generate-password-lite
61
- ```
62
-
63
- or
64
-
65
- ```bash
66
- yarn add generate-password-lite
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 "generate-password-lite";
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 } = "generate-password-lite";
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
- ## License
170
-
171
- generate-password-lite is licensed under the MIT License. See the [LICENSE](https://github.com/ahmadjoya/generate-password-lite#readme) file for more details.
1
+ <p align ="center">
2
+ <a href="https://nodei.co/npm/generate-password-lite" target="_blank">
3
+ <img src="https://nodei.co/npm/generate-password-lite.png" alt="npm Info" />
4
+ </a>
5
+ </p>
6
+
7
+ <p align="center">
8
+ <a href="https://npm.im/generate-password-lite" target="_blank">
9
+ <img src="https://img.shields.io/npm/v/generate-password-lite.svg" alt="npm Version" />
10
+ </a>
11
+ <a href="https://npm.im/generate-password-lite" target="_blank">
12
+ <img src="https://img.shields.io/npm/dm/generate-password-lite.svg" alt="npm Downloads" />
13
+ </a>
14
+ <a href="https://bundlephobia.com/package/generate-password-lite" target="_blank">
15
+ <img src="https://img.shields.io/bundlephobia/minzip/generate-password-lite" 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/generate-password-lite.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
+ # generate-password-lite
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 [`js-generate-password`](https://www.npmjs.com/package/js-generate-password). The two packages are the same library use whichever name you already depend on.
47
+
48
+ ## Installation
49
+
50
+ ```bash
51
+ npm install generate-password-lite
52
+ ```
53
+
54
+ ```bash
55
+ yarn add generate-password-lite
56
+ ```
57
+
58
+ ```bash
59
+ pnpm add generate-password-lite
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 'generate-password-lite'
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 'generate-password-lite'
83
+ ```
84
+
85
+ ### CommonJS
86
+
87
+ ```js
88
+ const { GeneratePassword } = require('generate-password-lite')
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 'generate-password-lite'
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 'generate-password-lite'
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 'generate-password-lite'
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 'generate-password-lite'
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
package/dist/index.cjs CHANGED
@@ -1 +1,201 @@
1
- "use strict";const e=e=>Math.floor(Math.random()*e),n=n=>{let r="",s="",{length:o,lowercase:l,uppercase:m,numbers:h,symbols:a,minLengthLowercase:i,minLengthUppercase:c,minLengthNumbers:g,minLengthSymbols:L,exclude:p}=n,u="abcdefghijklmnopqrstuvwxyz";u=t(u,p);let b="ABCDEFGHIJKLMNOPQRSTUVWXYZ";b=t(b,p);let d="0123456789";d=t(d,p);let w="!#$%&'()*+,-./:;<=>?@[]^_{|}~";w=t(w,p);let y=u.length;for(let n=0;n<i;n++)s+=u.charAt(e(y));y=b.length;for(let n=0;n<c;n++)s+=b.charAt(e(y));y=d.length;for(let n=0;n<g;n++)s+=d.charAt(e(y));y=w.length;for(let n=0;n<L;n++)s+=w.charAt(e(y));r+=(e=>{let n=e.split("");for(let e=n.length-1;e>0;e--){let t=Math.floor(Math.random()*(e+1)),r=n[e];n[e]=n[t],n[t]=r}return n.join("")})(s);let f="";f=l?f+u:f,f=m?f+b:f,f=h?f+d:f,f=a?f+w:f;let v=o-r.length;y=f.length;for(let n=0;n<v;n++)r+=f.charAt(e(y));return r},t=(e,n)=>(n?.split("").forEach((n=>{e=e.replace(n,"")})),e),r=e=>{const{length:n,lowercase:t,uppercase:r,numbers:s,symbols:o,minLengthLowercase:l,minLengthUppercase:m,minLengthNumbers:h,minLengthSymbols:a}=e;if(l+m+h+a>n)throw new Error("Sum of Minimum Characters in the passwords greater than the length of the Password to be generated.");if(0==t&&0==r&&0==s&&0==o)throw new Error("Alphabets, Numbers and Symbols are all set to False. No valid Characters to generate the Password.")},s=e=>(e.length=void 0===e.length?10:e.length,e.lowercase=void 0===e.lowercase||e.lowercase,e.uppercase=void 0===e.uppercase||e.uppercase,e.numbers=void 0===e.numbers||e.numbers,e.symbols=void 0!==e.symbols&&e.symbols,e.lowercase?e.minLengthLowercase=void 0===e.minLengthLowercase?1:e.minLengthLowercase:e.minLengthLowercase=0,e.uppercase?e.minLengthUppercase=void 0===e.minLengthUppercase?1:e.minLengthUppercase:e.minLengthUppercase=0,e.numbers?e.minLengthNumbers=void 0===e.minLengthNumbers?1:e.minLengthNumbers:e.minLengthNumbers=0,e.symbols?e.minLengthSymbols=void 0===e.minLengthSymbols?1:e.minLengthSymbols:e.minLengthSymbols=0,e.exclude=void 0===e.exclude?"":e.exclude,e);exports.GeneratePassword=function(e){let t;return e?(e=s(e),r(e),t=n(e),t):(e=s({length:10,lowercase:!0,uppercase:!0,numbers:!0,symbols:!1,exclude:"",minLengthLowercase:1,minLengthUppercase:1,minLengthNumbers:1,minLengthSymbols:0}),r(e),t=n(e),t)};
1
+ /**
2
+ * generate-password-lite v1.1.1
3
+ * Tiny, dependency-free, cryptographically secure password generator for JavaScript and TypeScript projects.
4
+ * @license MIT
5
+ */
6
+ "use strict";
7
+ var __defProp = Object.defineProperty;
8
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
9
+ var __getOwnPropNames = Object.getOwnPropertyNames;
10
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
11
+ var __export = (target, all) => {
12
+ for (var name in all)
13
+ __defProp(target, name, { get: all[name], enumerable: true });
14
+ };
15
+ var __copyProps = (to, from, except, desc) => {
16
+ if (from && typeof from === "object" || typeof from === "function") {
17
+ for (let key of __getOwnPropNames(from))
18
+ if (!__hasOwnProp.call(to, key) && key !== except)
19
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
20
+ }
21
+ return to;
22
+ };
23
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
24
+
25
+ // src/index.ts
26
+ var index_exports = {};
27
+ __export(index_exports, {
28
+ GeneratePassword: () => GeneratePassword,
29
+ default: () => index_default
30
+ });
31
+ module.exports = __toCommonJS(index_exports);
32
+
33
+ // src/lib/generate-random-number.ts
34
+ var UINT32_RANGE = 4294967296;
35
+ var getWebCrypto = () => {
36
+ const webCrypto = globalThis.crypto;
37
+ if (!webCrypto || typeof webCrypto.getRandomValues !== "function") {
38
+ throw new Error(
39
+ "generate-password-lite: no Web Crypto implementation found. A secure random source is required to generate passwords. Use Node.js 18+, a modern browser, or polyfill `globalThis.crypto`."
40
+ );
41
+ }
42
+ return webCrypto;
43
+ };
44
+ var generateRandomNumbers = (upperLimit) => {
45
+ if (!Number.isInteger(upperLimit) || upperLimit <= 0) {
46
+ throw new RangeError(
47
+ `generate-password-lite: upperLimit must be a positive integer, received ${upperLimit}.`
48
+ );
49
+ }
50
+ const webCrypto = getWebCrypto();
51
+ const buffer = new Uint32Array(1);
52
+ const cutoff = Math.floor(UINT32_RANGE / upperLimit) * upperLimit;
53
+ let value;
54
+ do {
55
+ webCrypto.getRandomValues(buffer);
56
+ value = buffer[0];
57
+ } while (value >= cutoff);
58
+ return value % upperLimit;
59
+ };
60
+
61
+ // src/lib/shuffle-string.ts
62
+ var shuffleString = (str) => {
63
+ const chars = [...str];
64
+ for (let i = chars.length - 1; i > 0; i--) {
65
+ const j = generateRandomNumbers(i + 1);
66
+ const tmp = chars[i];
67
+ chars[i] = chars[j];
68
+ chars[j] = tmp;
69
+ }
70
+ return chars.join("");
71
+ };
72
+
73
+ // src/lib/password-generator.ts
74
+ var LOWERCASE_CHARS = "abcdefghijklmnopqrstuvwxyz";
75
+ var UPPERCASE_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
76
+ var NUMBER_CHARS = "0123456789";
77
+ var SYMBOL_CHARS = "!#$%&'()*+,-./:;<=>?@[]^_{|}~";
78
+ var removeExceptions = (str, exclude) => {
79
+ if (!exclude) return str;
80
+ const banned = /* @__PURE__ */ new Set([...exclude]);
81
+ return [...str].filter((char) => !banned.has(char)).join("");
82
+ };
83
+ var pickFrom = (pool, count, name) => {
84
+ if (count === 0) return "";
85
+ if (pool.length === 0) {
86
+ throw new Error(
87
+ `generate-password-lite: \`exclude\` removed every ${name} character, but \`minLength${name[0].toUpperCase()}${name.slice(1)}\` is ${count}.`
88
+ );
89
+ }
90
+ let picked = "";
91
+ for (let i = 0; i < count; i++) {
92
+ picked += pool.charAt(generateRandomNumbers(pool.length));
93
+ }
94
+ return picked;
95
+ };
96
+ var generatePswd = (options) => {
97
+ const {
98
+ length,
99
+ lowercase,
100
+ uppercase,
101
+ numbers,
102
+ symbols,
103
+ minLengthLowercase,
104
+ minLengthUppercase,
105
+ minLengthNumbers,
106
+ minLengthSymbols,
107
+ exclude
108
+ } = options;
109
+ const lowercaseChar = removeExceptions(LOWERCASE_CHARS, exclude);
110
+ const uppercaseChar = removeExceptions(UPPERCASE_CHARS, exclude);
111
+ const numberChar = removeExceptions(NUMBER_CHARS, exclude);
112
+ const symbolChar = removeExceptions(SYMBOL_CHARS, exclude);
113
+ const guaranteed = pickFrom(lowercaseChar, minLengthLowercase, "lowercase") + pickFrom(uppercaseChar, minLengthUppercase, "uppercase") + pickFrom(numberChar, minLengthNumbers, "numbers") + pickFrom(symbolChar, minLengthSymbols, "symbols");
114
+ const characterPool = (lowercase ? lowercaseChar : "") + (uppercase ? uppercaseChar : "") + (numbers ? numberChar : "") + (symbols ? symbolChar : "");
115
+ const remaining = length - guaranteed.length;
116
+ if (remaining > 0 && characterPool.length === 0) {
117
+ throw new Error(
118
+ "generate-password-lite: `exclude` removed every character of every enabled class. No valid characters to generate the password from."
119
+ );
120
+ }
121
+ let filler = "";
122
+ for (let i = 0; i < remaining; i++) {
123
+ filler += characterPool.charAt(generateRandomNumbers(characterPool.length));
124
+ }
125
+ return shuffleString(guaranteed + filler);
126
+ };
127
+
128
+ // src/lib/check-options-error.ts
129
+ var assertCount = (value, name) => {
130
+ if (!Number.isInteger(value) || value < 0) {
131
+ throw new Error(
132
+ `generate-password-lite: \`${name}\` must be a non-negative integer, received ${value}.`
133
+ );
134
+ }
135
+ };
136
+ var checkError = (options) => {
137
+ const {
138
+ length,
139
+ lowercase,
140
+ uppercase,
141
+ numbers,
142
+ symbols,
143
+ minLengthLowercase,
144
+ minLengthUppercase,
145
+ minLengthNumbers,
146
+ minLengthSymbols
147
+ } = options;
148
+ if (!Number.isInteger(length) || length < 1) {
149
+ throw new Error(
150
+ `generate-password-lite: \`length\` must be a positive integer, received ${length}.`
151
+ );
152
+ }
153
+ assertCount(minLengthLowercase, "minLengthLowercase");
154
+ assertCount(minLengthUppercase, "minLengthUppercase");
155
+ assertCount(minLengthNumbers, "minLengthNumbers");
156
+ assertCount(minLengthSymbols, "minLengthSymbols");
157
+ if (!lowercase && !uppercase && !numbers && !symbols) {
158
+ throw new Error(
159
+ "generate-password-lite: `lowercase`, `uppercase`, `numbers` and `symbols` are all false. No valid characters to generate the password from."
160
+ );
161
+ }
162
+ const minimumTotal = minLengthLowercase + minLengthUppercase + minLengthNumbers + minLengthSymbols;
163
+ if (minimumTotal > length) {
164
+ throw new Error(
165
+ `generate-password-lite: the sum of the minimum character counts (${minimumTotal}) is greater than \`length\` (${length}).`
166
+ );
167
+ }
168
+ };
169
+
170
+ // src/lib/process-options.ts
171
+ var withDefault = (value, fallback) => typeof value === "undefined" ? fallback : value;
172
+ var processOption = (options) => {
173
+ const lowercase = withDefault(options.lowercase, true);
174
+ const uppercase = withDefault(options.uppercase, true);
175
+ const numbers = withDefault(options.numbers, true);
176
+ const symbols = withDefault(options.symbols, false);
177
+ return {
178
+ length: withDefault(options.length, 10),
179
+ lowercase,
180
+ uppercase,
181
+ numbers,
182
+ symbols,
183
+ exclude: withDefault(options.exclude, ""),
184
+ minLengthLowercase: lowercase ? withDefault(options.minLengthLowercase, 1) : 0,
185
+ minLengthUppercase: uppercase ? withDefault(options.minLengthUppercase, 1) : 0,
186
+ minLengthNumbers: numbers ? withDefault(options.minLengthNumbers, 1) : 0,
187
+ minLengthSymbols: symbols ? withDefault(options.minLengthSymbols, 1) : 0
188
+ };
189
+ };
190
+
191
+ // src/index.ts
192
+ function GeneratePassword(options = {}) {
193
+ const resolved = processOption(options);
194
+ checkError(resolved);
195
+ return generatePswd(resolved);
196
+ }
197
+ var index_default = GeneratePassword;
198
+ // Annotate the CommonJS export names for ESM import in node:
199
+ 0 && (module.exports = {
200
+ GeneratePassword
201
+ });
@@ -0,0 +1,67 @@
1
+ /**
2
+ * generate-password-lite v1.1.1
3
+ * Tiny, dependency-free, cryptographically secure password generator for JavaScript and TypeScript projects.
4
+ * @license MIT
5
+ */
6
+ export interface GenerateOptions {
7
+ /**
8
+ * Length of the generated password.
9
+ * @default 10
10
+ */
11
+ length?: number;
12
+ /**
13
+ * Should the password include lowercase characters.
14
+ * @default true
15
+ */
16
+ lowercase?: boolean;
17
+ /**
18
+ * Should the password include uppercase characters.
19
+ * @default true
20
+ */
21
+ uppercase?: boolean;
22
+ /**
23
+ * Should the password include numbers.
24
+ * @default true
25
+ */
26
+ numbers?: boolean;
27
+ /**
28
+ * Should the password include symbol characters.
29
+ * @default false
30
+ */
31
+ symbols?: boolean;
32
+ /**
33
+ * Characters to exclude from the password.
34
+ * @default ""
35
+ */
36
+ exclude?: string;
37
+ /**
38
+ * Minimum number of lowercase characters. Forced to 0 when `lowercase` is false.
39
+ * @default 1 when `lowercase` is true, otherwise 0
40
+ */
41
+ minLengthLowercase?: number;
42
+ /**
43
+ * Minimum number of uppercase characters. Forced to 0 when `uppercase` is false.
44
+ * @default 1 when `uppercase` is true, otherwise 0
45
+ */
46
+ minLengthUppercase?: number;
47
+ /**
48
+ * Minimum number of numeric characters. Forced to 0 when `numbers` is false.
49
+ * @default 1 when `numbers` is true, otherwise 0
50
+ */
51
+ minLengthNumbers?: number;
52
+ /**
53
+ * Minimum number of symbol characters. Forced to 0 when `symbols` is false.
54
+ * @default 1 when `symbols` is true, otherwise 0
55
+ */
56
+ minLengthSymbols?: number;
57
+ }
58
+ /**
59
+ * Generates a random password using a cryptographically secure random source.
60
+ *
61
+ * @param options - Optional overrides. Every unset option falls back to its
62
+ * documented default.
63
+ * @returns The generated password.
64
+ */
65
+ declare function GeneratePassword(options?: GenerateOptions): string;
66
+ export { GeneratePassword, type GenerateOptions };
67
+ export default GeneratePassword;
@@ -0,0 +1,67 @@
1
+ /**
2
+ * generate-password-lite v1.1.1
3
+ * Tiny, dependency-free, cryptographically secure password generator for JavaScript and TypeScript projects.
4
+ * @license MIT
5
+ */
6
+ export interface GenerateOptions {
7
+ /**
8
+ * Length of the generated password.
9
+ * @default 10
10
+ */
11
+ length?: number;
12
+ /**
13
+ * Should the password include lowercase characters.
14
+ * @default true
15
+ */
16
+ lowercase?: boolean;
17
+ /**
18
+ * Should the password include uppercase characters.
19
+ * @default true
20
+ */
21
+ uppercase?: boolean;
22
+ /**
23
+ * Should the password include numbers.
24
+ * @default true
25
+ */
26
+ numbers?: boolean;
27
+ /**
28
+ * Should the password include symbol characters.
29
+ * @default false
30
+ */
31
+ symbols?: boolean;
32
+ /**
33
+ * Characters to exclude from the password.
34
+ * @default ""
35
+ */
36
+ exclude?: string;
37
+ /**
38
+ * Minimum number of lowercase characters. Forced to 0 when `lowercase` is false.
39
+ * @default 1 when `lowercase` is true, otherwise 0
40
+ */
41
+ minLengthLowercase?: number;
42
+ /**
43
+ * Minimum number of uppercase characters. Forced to 0 when `uppercase` is false.
44
+ * @default 1 when `uppercase` is true, otherwise 0
45
+ */
46
+ minLengthUppercase?: number;
47
+ /**
48
+ * Minimum number of numeric characters. Forced to 0 when `numbers` is false.
49
+ * @default 1 when `numbers` is true, otherwise 0
50
+ */
51
+ minLengthNumbers?: number;
52
+ /**
53
+ * Minimum number of symbol characters. Forced to 0 when `symbols` is false.
54
+ * @default 1 when `symbols` is true, otherwise 0
55
+ */
56
+ minLengthSymbols?: number;
57
+ }
58
+ /**
59
+ * Generates a random password using a cryptographically secure random source.
60
+ *
61
+ * @param options - Optional overrides. Every unset option falls back to its
62
+ * documented default.
63
+ * @returns The generated password.
64
+ */
65
+ declare function GeneratePassword(options?: GenerateOptions): string;
66
+ export { GeneratePassword, type GenerateOptions };
67
+ export default GeneratePassword;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,67 @@
1
- import { GenerateOptions } from './types';
1
+ /**
2
+ * generate-password-lite v1.1.1
3
+ * Tiny, dependency-free, cryptographically secure password generator for JavaScript and TypeScript projects.
4
+ * @license MIT
5
+ */
6
+ export interface GenerateOptions {
7
+ /**
8
+ * Length of the generated password.
9
+ * @default 10
10
+ */
11
+ length?: number;
12
+ /**
13
+ * Should the password include lowercase characters.
14
+ * @default true
15
+ */
16
+ lowercase?: boolean;
17
+ /**
18
+ * Should the password include uppercase characters.
19
+ * @default true
20
+ */
21
+ uppercase?: boolean;
22
+ /**
23
+ * Should the password include numbers.
24
+ * @default true
25
+ */
26
+ numbers?: boolean;
27
+ /**
28
+ * Should the password include symbol characters.
29
+ * @default false
30
+ */
31
+ symbols?: boolean;
32
+ /**
33
+ * Characters to exclude from the password.
34
+ * @default ""
35
+ */
36
+ exclude?: string;
37
+ /**
38
+ * Minimum number of lowercase characters. Forced to 0 when `lowercase` is false.
39
+ * @default 1 when `lowercase` is true, otherwise 0
40
+ */
41
+ minLengthLowercase?: number;
42
+ /**
43
+ * Minimum number of uppercase characters. Forced to 0 when `uppercase` is false.
44
+ * @default 1 when `uppercase` is true, otherwise 0
45
+ */
46
+ minLengthUppercase?: number;
47
+ /**
48
+ * Minimum number of numeric characters. Forced to 0 when `numbers` is false.
49
+ * @default 1 when `numbers` is true, otherwise 0
50
+ */
51
+ minLengthNumbers?: number;
52
+ /**
53
+ * Minimum number of symbol characters. Forced to 0 when `symbols` is false.
54
+ * @default 1 when `symbols` is true, otherwise 0
55
+ */
56
+ minLengthSymbols?: number;
57
+ }
58
+ /**
59
+ * Generates a random password using a cryptographically secure random source.
60
+ *
61
+ * @param options - Optional overrides. Every unset option falls back to its
62
+ * documented default.
63
+ * @returns The generated password.
64
+ */
2
65
  declare function GeneratePassword(options?: GenerateOptions): string;
3
66
  export { GeneratePassword, type GenerateOptions };
67
+ export default GeneratePassword;
package/dist/index.mjs CHANGED
@@ -1 +1,175 @@
1
- const e=e=>Math.floor(Math.random()*e),n=n=>{let r="",s="",{length:o,lowercase:l,uppercase:m,numbers:h,symbols:a,minLengthLowercase:i,minLengthUppercase:c,minLengthNumbers:g,minLengthSymbols:L,exclude:p}=n,u="abcdefghijklmnopqrstuvwxyz";u=t(u,p);let b="ABCDEFGHIJKLMNOPQRSTUVWXYZ";b=t(b,p);let d="0123456789";d=t(d,p);let w="!#$%&'()*+,-./:;<=>?@[]^_{|}~";w=t(w,p);let f=u.length;for(let n=0;n<i;n++)s+=u.charAt(e(f));f=b.length;for(let n=0;n<c;n++)s+=b.charAt(e(f));f=d.length;for(let n=0;n<g;n++)s+=d.charAt(e(f));f=w.length;for(let n=0;n<L;n++)s+=w.charAt(e(f));r+=(e=>{let n=e.split("");for(let e=n.length-1;e>0;e--){let t=Math.floor(Math.random()*(e+1)),r=n[e];n[e]=n[t],n[t]=r}return n.join("")})(s);let y="";y=l?y+u:y,y=m?y+b:y,y=h?y+d:y,y=a?y+w:y;let v=o-r.length;f=y.length;for(let n=0;n<v;n++)r+=y.charAt(e(f));return r},t=(e,n)=>(n?.split("").forEach((n=>{e=e.replace(n,"")})),e),r=e=>{const{length:n,lowercase:t,uppercase:r,numbers:s,symbols:o,minLengthLowercase:l,minLengthUppercase:m,minLengthNumbers:h,minLengthSymbols:a}=e;if(l+m+h+a>n)throw new Error("Sum of Minimum Characters in the passwords greater than the length of the Password to be generated.");if(0==t&&0==r&&0==s&&0==o)throw new Error("Alphabets, Numbers and Symbols are all set to False. No valid Characters to generate the Password.")},s=e=>(e.length=void 0===e.length?10:e.length,e.lowercase=void 0===e.lowercase||e.lowercase,e.uppercase=void 0===e.uppercase||e.uppercase,e.numbers=void 0===e.numbers||e.numbers,e.symbols=void 0!==e.symbols&&e.symbols,e.lowercase?e.minLengthLowercase=void 0===e.minLengthLowercase?1:e.minLengthLowercase:e.minLengthLowercase=0,e.uppercase?e.minLengthUppercase=void 0===e.minLengthUppercase?1:e.minLengthUppercase:e.minLengthUppercase=0,e.numbers?e.minLengthNumbers=void 0===e.minLengthNumbers?1:e.minLengthNumbers:e.minLengthNumbers=0,e.symbols?e.minLengthSymbols=void 0===e.minLengthSymbols?1:e.minLengthSymbols:e.minLengthSymbols=0,e.exclude=void 0===e.exclude?"":e.exclude,e);function o(e){let t;if(e)return e=s(e),r(e),t=n(e),t;return e=s({length:10,lowercase:!0,uppercase:!0,numbers:!0,symbols:!1,exclude:"",minLengthLowercase:1,minLengthUppercase:1,minLengthNumbers:1,minLengthSymbols:0}),r(e),t=n(e),t}export{o as GeneratePassword};
1
+ /**
2
+ * generate-password-lite v1.1.1
3
+ * Tiny, dependency-free, cryptographically secure password generator for JavaScript and TypeScript projects.
4
+ * @license MIT
5
+ */
6
+
7
+ // src/lib/generate-random-number.ts
8
+ var UINT32_RANGE = 4294967296;
9
+ var getWebCrypto = () => {
10
+ const webCrypto = globalThis.crypto;
11
+ if (!webCrypto || typeof webCrypto.getRandomValues !== "function") {
12
+ throw new Error(
13
+ "generate-password-lite: no Web Crypto implementation found. A secure random source is required to generate passwords. Use Node.js 18+, a modern browser, or polyfill `globalThis.crypto`."
14
+ );
15
+ }
16
+ return webCrypto;
17
+ };
18
+ var generateRandomNumbers = (upperLimit) => {
19
+ if (!Number.isInteger(upperLimit) || upperLimit <= 0) {
20
+ throw new RangeError(
21
+ `generate-password-lite: upperLimit must be a positive integer, received ${upperLimit}.`
22
+ );
23
+ }
24
+ const webCrypto = getWebCrypto();
25
+ const buffer = new Uint32Array(1);
26
+ const cutoff = Math.floor(UINT32_RANGE / upperLimit) * upperLimit;
27
+ let value;
28
+ do {
29
+ webCrypto.getRandomValues(buffer);
30
+ value = buffer[0];
31
+ } while (value >= cutoff);
32
+ return value % upperLimit;
33
+ };
34
+
35
+ // src/lib/shuffle-string.ts
36
+ var shuffleString = (str) => {
37
+ const chars = [...str];
38
+ for (let i = chars.length - 1; i > 0; i--) {
39
+ const j = generateRandomNumbers(i + 1);
40
+ const tmp = chars[i];
41
+ chars[i] = chars[j];
42
+ chars[j] = tmp;
43
+ }
44
+ return chars.join("");
45
+ };
46
+
47
+ // src/lib/password-generator.ts
48
+ var LOWERCASE_CHARS = "abcdefghijklmnopqrstuvwxyz";
49
+ var UPPERCASE_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
50
+ var NUMBER_CHARS = "0123456789";
51
+ var SYMBOL_CHARS = "!#$%&'()*+,-./:;<=>?@[]^_{|}~";
52
+ var removeExceptions = (str, exclude) => {
53
+ if (!exclude) return str;
54
+ const banned = /* @__PURE__ */ new Set([...exclude]);
55
+ return [...str].filter((char) => !banned.has(char)).join("");
56
+ };
57
+ var pickFrom = (pool, count, name) => {
58
+ if (count === 0) return "";
59
+ if (pool.length === 0) {
60
+ throw new Error(
61
+ `generate-password-lite: \`exclude\` removed every ${name} character, but \`minLength${name[0].toUpperCase()}${name.slice(1)}\` is ${count}.`
62
+ );
63
+ }
64
+ let picked = "";
65
+ for (let i = 0; i < count; i++) {
66
+ picked += pool.charAt(generateRandomNumbers(pool.length));
67
+ }
68
+ return picked;
69
+ };
70
+ var generatePswd = (options) => {
71
+ const {
72
+ length,
73
+ lowercase,
74
+ uppercase,
75
+ numbers,
76
+ symbols,
77
+ minLengthLowercase,
78
+ minLengthUppercase,
79
+ minLengthNumbers,
80
+ minLengthSymbols,
81
+ exclude
82
+ } = options;
83
+ const lowercaseChar = removeExceptions(LOWERCASE_CHARS, exclude);
84
+ const uppercaseChar = removeExceptions(UPPERCASE_CHARS, exclude);
85
+ const numberChar = removeExceptions(NUMBER_CHARS, exclude);
86
+ const symbolChar = removeExceptions(SYMBOL_CHARS, exclude);
87
+ const guaranteed = pickFrom(lowercaseChar, minLengthLowercase, "lowercase") + pickFrom(uppercaseChar, minLengthUppercase, "uppercase") + pickFrom(numberChar, minLengthNumbers, "numbers") + pickFrom(symbolChar, minLengthSymbols, "symbols");
88
+ const characterPool = (lowercase ? lowercaseChar : "") + (uppercase ? uppercaseChar : "") + (numbers ? numberChar : "") + (symbols ? symbolChar : "");
89
+ const remaining = length - guaranteed.length;
90
+ if (remaining > 0 && characterPool.length === 0) {
91
+ throw new Error(
92
+ "generate-password-lite: `exclude` removed every character of every enabled class. No valid characters to generate the password from."
93
+ );
94
+ }
95
+ let filler = "";
96
+ for (let i = 0; i < remaining; i++) {
97
+ filler += characterPool.charAt(generateRandomNumbers(characterPool.length));
98
+ }
99
+ return shuffleString(guaranteed + filler);
100
+ };
101
+
102
+ // src/lib/check-options-error.ts
103
+ var assertCount = (value, name) => {
104
+ if (!Number.isInteger(value) || value < 0) {
105
+ throw new Error(
106
+ `generate-password-lite: \`${name}\` must be a non-negative integer, received ${value}.`
107
+ );
108
+ }
109
+ };
110
+ var checkError = (options) => {
111
+ const {
112
+ length,
113
+ lowercase,
114
+ uppercase,
115
+ numbers,
116
+ symbols,
117
+ minLengthLowercase,
118
+ minLengthUppercase,
119
+ minLengthNumbers,
120
+ minLengthSymbols
121
+ } = options;
122
+ if (!Number.isInteger(length) || length < 1) {
123
+ throw new Error(
124
+ `generate-password-lite: \`length\` must be a positive integer, received ${length}.`
125
+ );
126
+ }
127
+ assertCount(minLengthLowercase, "minLengthLowercase");
128
+ assertCount(minLengthUppercase, "minLengthUppercase");
129
+ assertCount(minLengthNumbers, "minLengthNumbers");
130
+ assertCount(minLengthSymbols, "minLengthSymbols");
131
+ if (!lowercase && !uppercase && !numbers && !symbols) {
132
+ throw new Error(
133
+ "generate-password-lite: `lowercase`, `uppercase`, `numbers` and `symbols` are all false. No valid characters to generate the password from."
134
+ );
135
+ }
136
+ const minimumTotal = minLengthLowercase + minLengthUppercase + minLengthNumbers + minLengthSymbols;
137
+ if (minimumTotal > length) {
138
+ throw new Error(
139
+ `generate-password-lite: the sum of the minimum character counts (${minimumTotal}) is greater than \`length\` (${length}).`
140
+ );
141
+ }
142
+ };
143
+
144
+ // src/lib/process-options.ts
145
+ var withDefault = (value, fallback) => typeof value === "undefined" ? fallback : value;
146
+ var processOption = (options) => {
147
+ const lowercase = withDefault(options.lowercase, true);
148
+ const uppercase = withDefault(options.uppercase, true);
149
+ const numbers = withDefault(options.numbers, true);
150
+ const symbols = withDefault(options.symbols, false);
151
+ return {
152
+ length: withDefault(options.length, 10),
153
+ lowercase,
154
+ uppercase,
155
+ numbers,
156
+ symbols,
157
+ exclude: withDefault(options.exclude, ""),
158
+ minLengthLowercase: lowercase ? withDefault(options.minLengthLowercase, 1) : 0,
159
+ minLengthUppercase: uppercase ? withDefault(options.minLengthUppercase, 1) : 0,
160
+ minLengthNumbers: numbers ? withDefault(options.minLengthNumbers, 1) : 0,
161
+ minLengthSymbols: symbols ? withDefault(options.minLengthSymbols, 1) : 0
162
+ };
163
+ };
164
+
165
+ // src/index.ts
166
+ function GeneratePassword(options = {}) {
167
+ const resolved = processOption(options);
168
+ checkError(resolved);
169
+ return generatePswd(resolved);
170
+ }
171
+ var index_default = GeneratePassword;
172
+ export {
173
+ GeneratePassword,
174
+ index_default as default
175
+ };
package/package.json CHANGED
@@ -1,57 +1,71 @@
1
- {
2
- "name": "generate-password-lite",
3
- "version": "1.0.2",
4
- "private": false,
5
- "description": "Password Generator for using in javascript/typescript based projects.",
6
- "Author": "Ahmad Joya <joya.abn@gmail.com>",
7
- "scripts": {
8
- "build": "rollup -c"
9
- },
10
- "repository": {
11
- "type": "git",
12
- "url": "git+https://github.com/ahmadjoya/generate-password-lite.git"
13
- },
14
- "keywords": [
15
- "generate password",
16
- "generate-password-lite",
17
- "password generator",
18
- "password generator",
19
- "random password generator",
20
- "simple password generator",
21
- "easy password generator",
22
- "password",
23
- "generator",
24
- "unique",
25
- "nodejs",
26
- "nextjs",
27
- "react",
28
- "reactjs",
29
- "random"
30
- ],
31
- "license": "ISC",
32
- "bugs": {
33
- "url": "https://github.com/ahmadjoya/generate-password-lite/issues"
34
- },
35
- "homepage": "https://github.com/ahmadjoya/generate-password-lite#readme",
36
- "type": "module",
37
- "main": "dist/index.cjs",
38
- "module": "dist/index.mjs",
39
- "types": "dist/types.d.ts",
40
- "files": [
41
- "dist"
42
- ],
43
- "exports": {
44
- "import": "./dist/index.mjs",
45
- "require": "./dist/index.cjs",
46
- "types": "./dist/types.d.ts"
47
- },
48
- "bin": "./dist/index.js",
49
- "devDependencies": {
50
- "@rollup/plugin-commonjs": "^25.0.7",
51
- "@rollup/plugin-node-resolve": "^15.2.3",
52
- "@rollup/plugin-terser": "^0.4.4",
53
- "rollup": "^4.6.1",
54
- "rollup-plugin-dts": "^6.1.0",
55
- "rollup-plugin-typescript2": "^0.36.0"
56
- }
57
- }
1
+ {
2
+ "name": "generate-password-lite",
3
+ "version": "1.1.1",
4
+ "private": false,
5
+ "description": "Tiny, dependency-free, cryptographically secure password generator for JavaScript and TypeScript projects.",
6
+ "author": "Ahmad Joya <joya.abn@gmail.com>",
7
+ "license": "MIT",
8
+ "type": "module",
9
+ "sideEffects": false,
10
+ "main": "./dist/index.cjs",
11
+ "module": "./dist/index.mjs",
12
+ "types": "./dist/index.d.cts",
13
+ "exports": {
14
+ ".": {
15
+ "import": {
16
+ "types": "./dist/index.d.mts",
17
+ "default": "./dist/index.mjs"
18
+ },
19
+ "require": {
20
+ "types": "./dist/index.d.cts",
21
+ "default": "./dist/index.cjs"
22
+ }
23
+ },
24
+ "./package.json": "./package.json"
25
+ },
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "engines": {
30
+ "node": ">=18"
31
+ },
32
+ "scripts": {
33
+ "build": "node scripts/build.mjs",
34
+ "typecheck": "tsc --noEmit false -p tsconfig.json --emitDeclarationOnly false --noEmit",
35
+ "test": "node --test \"test/**/*.test.mjs\"",
36
+ "check": "npm run build && npm run test && publint",
37
+ "prepublishOnly": "npm run build",
38
+ "build:alias": "node scripts/pack-alias.mjs",
39
+ "publish:alias": "node scripts/pack-alias.mjs --publish"
40
+ },
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "git+https://github.com/ahmadjoya/generate-password-lite.git"
44
+ },
45
+ "keywords": [
46
+ "password",
47
+ "password-generator",
48
+ "generate-password",
49
+ "generate-password-lite",
50
+ "js-generate-password",
51
+ "random-password",
52
+ "secure-password",
53
+ "crypto",
54
+ "csprng",
55
+ "generator",
56
+ "random",
57
+ "typescript",
58
+ "nodejs",
59
+ "react",
60
+ "nextjs",
61
+ "browser"
62
+ ],
63
+ "bugs": {
64
+ "url": "https://github.com/ahmadjoya/generate-password-lite/issues"
65
+ },
66
+ "homepage": "https://github.com/ahmadjoya/generate-password-lite#readme",
67
+ "devDependencies": {
68
+ "@arethetypeswrong/cli": "^0.18.5",
69
+ "typescript": "^7.0.2"
70
+ }
71
+ }
@@ -1,2 +0,0 @@
1
- import { GenerateOptions } from '../types';
2
- export declare const checkError: (options: GenerateOptions) => void;
@@ -1 +0,0 @@
1
- export declare const generateRandomNumbers: (upperLimit: number) => number;
@@ -1,2 +0,0 @@
1
- import { GenerateOptions } from '../types';
2
- export declare const generatePswd: (options: GenerateOptions) => string;
@@ -1,2 +0,0 @@
1
- import { GenerateOptions } from '../types';
2
- export declare const processOption: (options: GenerateOptions) => GenerateOptions;
@@ -1 +0,0 @@
1
- export declare const shuffleString: (str: string) => string;
package/dist/types.d.ts DELETED
@@ -1,58 +0,0 @@
1
- interface GenerateOptions {
2
- /**
3
- * lenght of the password
4
- * @default 10
5
- */
6
- length?: number
7
- /**
8
- * Should the password include lowercase characters
9
- * @default true
10
- */
11
- lowercase?: boolean
12
- /**
13
- * Should the password include uppercase characters
14
- * @default true
15
- */
16
- uppercase?: boolean
17
- /**
18
- * Should the password include numbers
19
- * @default true
20
- */
21
- numbers?: boolean
22
- /**
23
- * Should the password include symbol characters
24
- * @default false
25
- */
26
- symbols?: boolean
27
- /**
28
- * List of characters to be excluded from the password
29
- * @default ""
30
- */
31
- exclude?: string
32
- /**
33
- * min lentgh of lowercase character
34
- * @default 1
35
- */
36
- minLengthLowercase?: number
37
- /**
38
- * min lentgh of uppercase character
39
- * @default 1
40
- */
41
- minLengthUppercase?: number
42
- /**
43
- * min length of numbers
44
- * @default 1
45
- */
46
- minLengthNumbers?: number
47
- /**
48
- * min length of symbols
49
- * @default 0
50
- */
51
- minLengthSymbols?: number
52
- }
53
-
54
- declare namespace GeneratePassword {}
55
-
56
- declare function GeneratePassword(options?: GenerateOptions): string
57
-
58
- export { type GenerateOptions, GeneratePassword };