@tushardev01/farm-password 1.0.11 → 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
@@ -25,8 +25,8 @@ genpass
25
25
  genpass -c
26
26
  ```
27
27
 
28
- genpass: logs random password
29
- genpass -c: logs random password and copies to clipboard
28
+ - `genpass`: logs random password
29
+ - `genpass -c`: logs random password and copies to clipboard
30
30
 
31
31
 
32
32
  ---
@@ -39,7 +39,7 @@ npm install @tushardev01/farm-password
39
39
 
40
40
  # Usage (JavaScript / ESM)
41
41
  ```bash
42
- 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';
43
43
 
44
44
  // Generate a password with default character sets
45
45
  const password1 = genPassword(16);
@@ -96,11 +96,18 @@ randomNumIn(x,y): number
96
96
 
97
97
  shuffle(arr: any[],inPlace: boolean): any[]
98
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
+
99
106
  ```
100
107
 
101
108
  ### TypeScript Support
102
109
  ```bash
103
- import { genPassword, shuffleMax, shuffle, randomNumIn } from '@tushardev01/farm-password';
110
+ import { genPassword, shuffleMax, shuffle, randomNumIn, sample, collect } from '@tushardev01/farm-password';
104
111
 
105
112
  const password: string = genPassword(16);
106
113
  const chars: string[] = shuffleMax(12, [
@@ -113,4 +120,4 @@ Types are included automatically via index.d.ts.
113
120
 
114
121
  ### License
115
122
 
116
- ISC © Tushar Kumar
123
+ ISC © Tushar Kumar
package/app.js CHANGED
@@ -7,6 +7,8 @@ import {
7
7
  genPassword,
8
8
  randomNumIn,
9
9
  shuffle,
10
+ sample,
11
+ collect
10
12
  } from './utility.js';
11
13
 
12
14
  export {
@@ -17,5 +19,7 @@ export {
17
19
  shuffleMax,
18
20
  genPassword,
19
21
  randomNumIn,
20
- shuffle
21
- };
22
+ shuffle,
23
+ sample,
24
+ collect
25
+ };
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,6 +1,6 @@
1
1
  {
2
2
  "name": "@tushardev01/farm-password",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "type": "module",
5
5
  "main": "./app.js",
6
6
  "exports": {
@@ -21,16 +21,23 @@
21
21
  "test": "echo \"Error: no test specified\" && exit 1"
22
22
  },
23
23
  "keywords": [
24
+ "utility",
25
+ "random",
26
+ "random-array",
27
+ "array-utils",
28
+ "array-sample",
29
+ "sampling",
30
+ "collect",
31
+ "shuffle",
32
+ "generator",
24
33
  "password",
25
34
  "password-generator",
26
- "cli",
27
35
  "security",
28
- "generator"
36
+ "cli",
37
+ "javascript",
38
+ "node",
39
+ "npm"
29
40
  ],
30
- "repository": {
31
- "type": "git",
32
- "url": "https://github.com/TusharKumar1007/farm-password"
33
- },
34
41
  "author": "Tushar Kumar",
35
42
  "license": "ISC",
36
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
+