@tushardev01/farm-password 1.0.10 → 1.0.13

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/README.md CHANGED
@@ -5,7 +5,7 @@ Generates secure random passwords using lowercase, uppercase, numbers, and symbo
5
5
 
6
6
  ## Package Information
7
7
 
8
- 🔗 **npm Package**: [https://www.npmjs.com/package/@tushardev01/farm-password](https://www.npmjs.com/package/@tushardev01/farm-password)
8
+ 🔗 **npm Package**: [https://www.npmjs.com/package/@tushardev01/farm-password](https://www.npmjs.com/package/@tushardev01/farm-password)
9
9
 
10
10
  ---
11
11
 
@@ -17,15 +17,17 @@ Generates secure random passwords using lowercase, uppercase, numbers, and symbo
17
17
  - CLI support (`genpass`)
18
18
  - TypeScript typings included
19
19
 
20
- #### To use genpass:
20
+
21
+ #### To use genpass cli:
21
22
  ```bash
22
- npm i
23
- npm link
23
+ npm install -g @tushardev01/farm-password
24
+ genpass
25
+ genpass -c
24
26
  ```
25
27
 
26
- #### Usage:
27
- run ```bash genpass``` to log random password
28
- run ```bash genpass -c``` to log and copy to clipboard
28
+ - `genpass`: logs random password
29
+ - `genpass -c`: logs random password and copies to clipboard
30
+
29
31
 
30
32
  ---
31
33
 
@@ -37,7 +39,7 @@ npm install @tushardev01/farm-password
37
39
 
38
40
  # Usage (JavaScript / ESM)
39
41
  ```bash
40
- import { genPassword, shuffleMax, genLowerLetters, genUpperLetters, genNumbers, genSymbols, randomNumIn, shuffle } from '@tushardev01/farm-password';
42
+ import { genPassword, shuffleMax, genLowerLetters, genUpperLetters, genNumbers, genSymbols, randomNumIn, shuffle,collect, sample } from '@tushardev01/farm-password';
41
43
 
42
44
  // Generate a password with default character sets
43
45
  const password1 = genPassword(16);
@@ -70,12 +72,12 @@ This generates a password with default settings.
70
72
  ```bash
71
73
  genPassword(charLength?: number): string
72
74
  Generates a password using default arrays
73
- charLength ΓÇô optional, default is 25
75
+ charLength optional, default is 25
74
76
 
75
77
  shuffleMax(charLength?: number, lists: string[][]): string[]
76
78
  Randomly picks characters from multiple character arrays
77
- charLength ΓÇô optional, default 25
78
- lists ΓÇô array of string arrays, e.g., [genLowerLetters(), genNumbers()]
79
+ charLength optional, default 25
80
+ lists array of string arrays, e.g., [genLowerLetters(), genNumbers()]
79
81
 
80
82
  genLowerLetters(): string[]
81
83
  Returns lowercase letters: ['a','b',...,'z']
@@ -94,11 +96,18 @@ randomNumIn(x,y): number
94
96
 
95
97
  shuffle(arr: any[],inPlace: boolean): any[]
96
98
  Returns a new array or the same one with items shuffled
99
+
100
+ collect<T>(arr: T[], population?: number): T[]
101
+ Returns an array of randomly selected elements (duplicates allowed).
102
+
103
+ sample<T>(arr: T[], population?: number): T[] | undefined
104
+ Returns an array of randomly selected unique elements (no duplicates).
105
+
97
106
  ```
98
107
 
99
108
  ### TypeScript Support
100
109
  ```bash
101
- import { genPassword, shuffleMax, shuffle, randomNumIn } from '@tushardev01/farm-password';
110
+ import { genPassword, shuffleMax, shuffle, randomNumIn, sample, collect } from '@tushardev01/farm-password';
102
111
 
103
112
  const password: string = genPassword(16);
104
113
  const chars: string[] = shuffleMax(12, [
@@ -111,5 +120,4 @@ Types are included automatically via index.d.ts.
111
120
 
112
121
  ### License
113
122
 
114
- ISC © Tushar Kumar
115
-
123
+ ISC © Tushar Kumar
package/app.js CHANGED
@@ -1,6 +1,3 @@
1
- #!/usr/bin/env node
2
-
3
- import clipboard from 'clipboardy';
4
1
  import {
5
2
  genLowerLetters,
6
3
  genUpperLetters,
@@ -10,20 +7,10 @@ import {
10
7
  genPassword,
11
8
  randomNumIn,
12
9
  shuffle,
10
+ sample,
11
+ collect
13
12
  } from './utility.js';
14
13
 
15
- const flag = process.argv[2]
16
- const isCalledByGenPass = process.argv[1].includes("@tushardev01");
17
-
18
- if (isCalledByGenPass) {
19
- const password = genPassword();
20
- console.log(password);
21
-
22
- if (flag && flag === '-c') {
23
- clipboard.writeSync(password)
24
- console.log("Copied to clipboard!!");
25
- }
26
- }
27
14
  export {
28
15
  genLowerLetters,
29
16
  genUpperLetters,
@@ -32,5 +19,7 @@ export {
32
19
  shuffleMax,
33
20
  genPassword,
34
21
  randomNumIn,
35
- shuffle
36
- };
22
+ shuffle,
23
+ sample,
24
+ collect
25
+ };
package/cli.js ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env node
2
+
3
+ import clipboard from 'clipboardy';
4
+ import { genPassword } from './utility.js';
5
+
6
+ const args = process.argv.slice(2);
7
+ const copy = args.includes('-c');
8
+
9
+ const password = genPassword();
10
+ console.log(password);
11
+
12
+ if (copy) {
13
+ clipboard.writeSync(password);
14
+ console.log('Copied to clipboard!!');
15
+ }
16
+
package/index.d.ts CHANGED
@@ -49,6 +49,31 @@ export function shuffleMax(
49
49
  lists?: string[][]
50
50
  ): string[];
51
51
 
52
+
53
+ /**
54
+ * A utility function to collect a specified number of random elements from an array,
55
+ * allowing duplicates in the collected array.
56
+ *
57
+ * @param arr The array to collect random elements from.
58
+ * @param population The number of random elements to collect. Defaults to 5.
59
+ * @returns An array of random elements, which may include duplicates.
60
+ */
61
+ declare function collect<T>(arr: T[], population?: number): T[];
62
+
63
+ /**
64
+ * A utility function to sample a specified number of unique random elements from an array.
65
+ *
66
+ * Throws an error if there aren't enough unique elements in the array to meet the requested population.
67
+ *
68
+ * @param arr The array to sample unique random elements from.
69
+ * @param population The number of unique random elements to sample. Defaults to 5.
70
+ * @returns An array of unique random elements, or `undefined` if an error occurs.
71
+ * @throws Error If the number of unique elements in the array is less than the requested population.
72
+ */
73
+ declare function sample<T>(arr: T[], population?: number): T[] | undefined;
74
+
75
+ export { collect, sample };
76
+
52
77
  /**
53
78
  * Generates a password by randomly selecting characters from predefined lists (lowercase, uppercase, symbols, numbers),
54
79
  * then shuffling them to create a secure, random password.
@@ -57,4 +82,4 @@ export function shuffleMax(
57
82
  */
58
83
  export function genPassword(
59
84
  charLength?: number
60
- ): string;
85
+ ): string;
package/package.json CHANGED
@@ -1,16 +1,17 @@
1
1
  {
2
2
  "name": "@tushardev01/farm-password",
3
- "version": "1.0.10",
3
+ "version": "1.0.13",
4
4
  "type": "module",
5
5
  "main": "./app.js",
6
6
  "exports": {
7
7
  ".": "./app.js"
8
8
  },
9
9
  "bin": {
10
- "genpass": "./app.js"
10
+ "genpass": "./cli.js"
11
11
  },
12
12
  "types": "./index.d.ts",
13
13
  "files": [
14
+ "cli.js",
14
15
  "app.js",
15
16
  "utility.js",
16
17
  "README.md",
@@ -20,16 +21,23 @@
20
21
  "test": "echo \"Error: no test specified\" && exit 1"
21
22
  },
22
23
  "keywords": [
24
+ "utility",
25
+ "random",
26
+ "random-array",
27
+ "array-utils",
28
+ "array-sample",
29
+ "sampling",
30
+ "collect",
31
+ "shuffle",
32
+ "generator",
23
33
  "password",
24
34
  "password-generator",
25
- "cli",
26
35
  "security",
27
- "generator"
36
+ "cli",
37
+ "javascript",
38
+ "node",
39
+ "npm"
28
40
  ],
29
- "repository": {
30
- "type": "git",
31
- "url": "https://github.com/TusharKumar1007/farm-password"
32
- },
33
41
  "author": "Tushar Kumar",
34
42
  "license": "ISC",
35
43
  "description": "Generates passwords and provides handy character arrays.",
package/utility.js CHANGED
@@ -78,14 +78,75 @@ export const shuffleMax = (charLength = 25, lists) => {
78
78
  return arr;
79
79
  };
80
80
 
81
+ /**
82
+ * Collects a specified number of random elements from an array (with possible duplicates).
83
+ *
84
+ * @param {Array} arr - The array from which to collect random elements.
85
+ * @param {number} [population=5] - The number of random elements to collect. Defaults to 5.
86
+ *
87
+ * @returns {Array} A new array containing the randomly collected elements. The array may contain duplicates.
88
+ *
89
+ * @example
90
+ * const fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
91
+ * const collected = collect(fruits, 3); // Could return ['banana', 'apple', 'banana']
92
+ */
93
+ export const collect = (arr, population = 5) => {
94
+ const collectedArray = [];
95
+ for (let i = 0; i < population; i++) {
96
+ const randomNumber = randomNumIn(0, arr.length - 1);
97
+ collectedArray.push(arr[randomNumber]);
98
+ }
99
+ return collectedArray;
100
+ }
101
+
102
+ /**
103
+ * Samples a specified number of unique random elements from an array (no duplicates).
104
+ *
105
+ * If the array has fewer unique elements than the requested population, an error will be thrown.
106
+ *
107
+ * @param {Array} arr - The array from which to sample unique random elements.
108
+ * @param {number} [population=5] - The number of unique random elements to sample. Defaults to 5.
109
+ *
110
+ * @returns {Array|undefined} An array of unique sampled elements, or `undefined` if an error occurs.
111
+ *
112
+ * @throws {Error} Throws an error if there are not enough unique elements in the array to meet the requested population.
113
+ *
114
+ * @example
115
+ * const fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
116
+ * const sampled = sample(fruits, 3); // Could return ['banana', 'apple', 'cherry']
117
+ *
118
+ * @example
119
+ * const fruits = ['apple', 'banana', 'apple', 'banana'];
120
+ * sample(fruits, 3); // Throws an error: "Array length without duplicates must be greater than population."
121
+ */
122
+ export const sample = (arr, population = 5) => {
123
+ const set = new Set();
124
+ try {
125
+ if (new Set(arr).size < population) {
126
+ throw new Error("Array length without duplicates must be greater than population. Use collect function instead.");
127
+ }
128
+ while (set.size !== population) {
129
+ const randomNumber = randomNumIn(0, arr.length - 1);
130
+ set.add(arr[randomNumber]);
131
+ }
132
+
133
+ return [...set];
134
+ } catch (e) {
135
+ console.error(e);
136
+ return undefined;
137
+ }
138
+ }
139
+
140
+
81
141
  /**
82
142
  * Generates a password by randomly selecting characters from predefined lists (lowercase, uppercase, symbols, numbers),
83
143
  * then shuffling them to create a secure, random password.
84
144
  * @param {number} [charLength=25] The length of the password to generate. Default is 25 characters.
85
145
  * @returns {string} The generated random password.
86
146
  */
87
- const passLists=[genLowerLetters(),genNumbers(),genSymbols(),genUpperLetters()];
147
+ const passLists = [genLowerLetters(), genNumbers(), genSymbols(), genUpperLetters()];
88
148
  export const genPassword = (charLength = 25) => {
89
149
  const password = shuffle(shuffleMax(charLength, passLists)).join('');
90
150
  return password;
91
151
  };
152
+