@kottetall/random 0.0.3 → 0.0.5

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.
@@ -0,0 +1,10 @@
1
+ export declare const gender: {
2
+ readonly MALE: "male";
3
+ readonly FEMALE: "female";
4
+ readonly UNISEX: "unisex";
5
+ };
6
+ export declare const firstNameNeutral: readonly ["Alex", "Ari", "Ash", "Avery", "Bo", "Cai", "Kai", "Casey", "Chris", "Dani", "Eli", "Emi", "Jay", "Jesse", "Jordan", "Lee", "Lian", "Luca", "Luka", "Max", "Mika", "Mica", "Quinn", "Ren", "Rio", "Robin", "Sam", "Sky", "Sora", "Tao", "Taylor", "Toni", "Yuri", "Zion"];
7
+ export declare const firstNameMale: readonly ["Adam", "Adrian", "Ahmed", "Ahmad", "Alan", "Alejandro", "André", "Antonio", "Arjun", "Ben", "Carlos", "Daniel", "David", "Diego", "Ethan", "Felix", "Gabriel", "Hassan", "Hugo", "Ibrahim", "Ivan", "Jack", "James", "Jason", "John", "Joseph", "Julian", "Kevin", "Leo", "Lucas", "Luis", "Marco", "Mark", "Martin", "Mateo", "Michael", "Mohammed", "Muhammad", "Nico", "Noah", "Oliver", "Omar", "Oscar", "Paul", "Rafael", "Samir", "Sebastian", "Simon", "Thomas", "Victor", "William", "Yusuf", "Youssef", "Zayn"];
8
+ export declare const firstNameFemale: readonly ["Aisha", "Ayesha", "Alice", "Amelia", "Ana", "Anna", "Aya", "Bea", "Carla", "Clara", "Diana", "Elena", "Ella", "Emma", "Eva", "Fatima", "Hana", "Hanna", "Isabella", "Jade", "Julia", "Khadija", "Lara", "Leila", "Layla", "Lina", "Lucia", "Maria", "Maya", "Mina", "Nadia", "Naomi", "Nina", "Olivia", "Rania", "Rosa", "Sara", "Sarah", "Sofia", "Sophia", "Tara", "Yara", "Zara"];
9
+ export declare const lastname: readonly ["Ali", "Ahmed", "Anderson", "Brown", "Chen", "Garcia", "Hassan", "Ivanov", "Khan", "Kim", "Lee", "Li", "Lopez", "Martin", "Mohamed", "Nguyen", "Patel", "Rodriguez", "Singh", "Smith", "Wang", "Zhang", "Andersson", "Anderson", "Baker", "Bianchi", "Black", "Blanco", "Clark", "Costa", "Dubois", "Evans", "Fischer", "Fisher", "Fontana", "Gonzalez", "Hansen", "Johansson", "Jones", "Kowalski", "Larsen", "Müller", "Muller", "Novak", "Petrov", "Rossi", "Schmidt", "Schmidt", "Silva", "Taylor", "Walker", "White"];
10
+ //# sourceMappingURL=name.constant.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"name.constant.d.ts","sourceRoot":"","sources":["../../src/constants/name.constant.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,MAAM;;;;CAIT,CAAC;AAEX,eAAO,MAAM,gBAAgB,qRAmCnB,CAAC;AAEX,eAAO,MAAM,aAAa,qgBAuDhB,CAAC;AAEX,eAAO,MAAM,eAAe,iYA4ClB,CAAC;AAEX,eAAO,MAAM,QAAQ,sgBAqDX,CAAC"}
@@ -0,0 +1,199 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.lastname = exports.firstNameFemale = exports.firstNameMale = exports.firstNameNeutral = exports.gender = void 0;
4
+ exports.gender = {
5
+ MALE: "male",
6
+ FEMALE: "female",
7
+ UNISEX: "unisex",
8
+ };
9
+ exports.firstNameNeutral = [
10
+ "Alex",
11
+ "Ari",
12
+ "Ash",
13
+ "Avery",
14
+ "Bo",
15
+ "Cai",
16
+ "Kai",
17
+ "Casey",
18
+ "Chris",
19
+ "Dani",
20
+ "Eli",
21
+ "Emi",
22
+ "Jay",
23
+ "Jesse",
24
+ "Jordan",
25
+ "Lee",
26
+ "Lian",
27
+ "Luca",
28
+ "Luka",
29
+ "Max",
30
+ "Mika",
31
+ "Mica",
32
+ "Quinn",
33
+ "Ren",
34
+ "Rio",
35
+ "Robin",
36
+ "Sam",
37
+ "Sky",
38
+ "Sora",
39
+ "Tao",
40
+ "Taylor",
41
+ "Toni",
42
+ "Yuri",
43
+ "Zion",
44
+ ];
45
+ exports.firstNameMale = [
46
+ "Adam",
47
+ "Adrian",
48
+ "Ahmed",
49
+ "Ahmad",
50
+ "Alan",
51
+ "Alejandro",
52
+ "André",
53
+ "Antonio",
54
+ "Arjun",
55
+ "Ben",
56
+ "Carlos",
57
+ "Daniel",
58
+ "David",
59
+ "Diego",
60
+ "Ethan",
61
+ "Felix",
62
+ "Gabriel",
63
+ "Hassan",
64
+ "Hugo",
65
+ "Ibrahim",
66
+ "Ivan",
67
+ "Jack",
68
+ "James",
69
+ "Jason",
70
+ "John",
71
+ "Joseph",
72
+ "Julian",
73
+ "Kevin",
74
+ "Leo",
75
+ "Lucas",
76
+ "Luis",
77
+ "Marco",
78
+ "Mark",
79
+ "Martin",
80
+ "Mateo",
81
+ "Michael",
82
+ "Mohammed",
83
+ "Muhammad",
84
+ "Nico",
85
+ "Noah",
86
+ "Oliver",
87
+ "Omar",
88
+ "Oscar",
89
+ "Paul",
90
+ "Rafael",
91
+ "Samir",
92
+ "Sebastian",
93
+ "Simon",
94
+ "Thomas",
95
+ "Victor",
96
+ "William",
97
+ "Yusuf",
98
+ "Youssef",
99
+ "Zayn",
100
+ ];
101
+ exports.firstNameFemale = [
102
+ "Aisha",
103
+ "Ayesha",
104
+ "Alice",
105
+ "Amelia",
106
+ "Ana",
107
+ "Anna",
108
+ "Aya",
109
+ "Bea",
110
+ "Carla",
111
+ "Clara",
112
+ "Diana",
113
+ "Elena",
114
+ "Ella",
115
+ "Emma",
116
+ "Eva",
117
+ "Fatima",
118
+ "Hana",
119
+ "Hanna",
120
+ "Isabella",
121
+ "Jade",
122
+ "Julia",
123
+ "Khadija",
124
+ "Lara",
125
+ "Leila",
126
+ "Layla",
127
+ "Lina",
128
+ "Lucia",
129
+ "Maria",
130
+ "Maya",
131
+ "Mina",
132
+ "Nadia",
133
+ "Naomi",
134
+ "Nina",
135
+ "Olivia",
136
+ "Rania",
137
+ "Rosa",
138
+ "Sara",
139
+ "Sarah",
140
+ "Sofia",
141
+ "Sophia",
142
+ "Tara",
143
+ "Yara",
144
+ "Zara",
145
+ ];
146
+ exports.lastname = [
147
+ "Ali",
148
+ "Ahmed",
149
+ "Anderson",
150
+ "Brown",
151
+ "Chen",
152
+ "Garcia",
153
+ "Hassan",
154
+ "Ivanov",
155
+ "Khan",
156
+ "Kim",
157
+ "Lee",
158
+ "Li",
159
+ "Lopez",
160
+ "Martin",
161
+ "Mohamed",
162
+ "Nguyen",
163
+ "Patel",
164
+ "Rodriguez",
165
+ "Singh",
166
+ "Smith",
167
+ "Wang",
168
+ "Zhang",
169
+ "Andersson",
170
+ "Anderson",
171
+ "Baker",
172
+ "Bianchi",
173
+ "Black",
174
+ "Blanco",
175
+ "Clark",
176
+ "Costa",
177
+ "Dubois",
178
+ "Evans",
179
+ "Fischer",
180
+ "Fisher",
181
+ "Fontana",
182
+ "Gonzalez",
183
+ "Hansen",
184
+ "Johansson",
185
+ "Jones",
186
+ "Kowalski",
187
+ "Larsen",
188
+ "Müller",
189
+ "Muller",
190
+ "Novak",
191
+ "Petrov",
192
+ "Rossi",
193
+ "Schmidt",
194
+ "Schmidt",
195
+ "Silva",
196
+ "Taylor",
197
+ "Walker",
198
+ "White",
199
+ ];
package/lib/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Casing } from "./types/random.type";
2
2
  import { ObjectValues } from "./types/utils.type";
3
+ import { Gender } from "./types/name.type";
3
4
  export declare class Random {
4
5
  static intBetween(min: number, max: number): number;
5
6
  static boolean(): boolean;
@@ -47,5 +48,38 @@ export declare class Random {
47
48
  * @returns
48
49
  */
49
50
  static uuid(): `${string}-${string}-${string}-${string}-${string}`;
51
+ /**
52
+ * Has a n% chance of throwing a error
53
+ * @param probabilityOfError - The probability of a error beeing thrown - i.e 0.2 = 20% chance
54
+ * @param errorMessage - The errormessage to be used instead of the default one
55
+ * @param callback - The callback that should be run if there's no error
56
+ * @returns
57
+ */
58
+ static throwError(probabilityOfError: number, errorMessage?: string, callback?: Function): void;
59
+ /**
60
+ *
61
+ * @param minMs - Minimum milliseconds to delay
62
+ * @param maxMs - Maximum milliseconds to delay
63
+ * @param value - Optional return value
64
+ * @returns
65
+ */
66
+ static delay<T>(minMs: number, maxMs: number, value?: T): Promise<T | undefined>;
67
+ /**
68
+ * Returns a name
69
+ * @param nameGender - Only male, female or unisex. If omitted, the name could be either one
70
+ * @returns
71
+ */
72
+ static firstName(nameGender?: Gender): string;
73
+ /**
74
+ * Gives a surname/last name
75
+ * @returns
76
+ */
77
+ static lastName(): "Lee" | "Taylor" | "Ahmed" | "Hassan" | "Martin" | "Ali" | "Anderson" | "Brown" | "Chen" | "Garcia" | "Ivanov" | "Khan" | "Kim" | "Li" | "Lopez" | "Mohamed" | "Nguyen" | "Patel" | "Rodriguez" | "Singh" | "Smith" | "Wang" | "Zhang" | "Andersson" | "Baker" | "Bianchi" | "Black" | "Blanco" | "Clark" | "Costa" | "Dubois" | "Evans" | "Fischer" | "Fisher" | "Fontana" | "Gonzalez" | "Hansen" | "Johansson" | "Jones" | "Kowalski" | "Larsen" | "Müller" | "Muller" | "Novak" | "Petrov" | "Rossi" | "Schmidt" | "Silva" | "Walker" | "White";
78
+ /**
79
+ * Gives a full name - first and last name
80
+ * @param nameGender
81
+ * @returns
82
+ */
83
+ static fullName(nameGender?: Gender): string;
50
84
  }
51
85
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAWA,OAAO,EAAiB,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAqB,MAAM,oBAAoB,CAAC;AAErE,qBAAa,MAAM;IACjB,MAAM,CAAC,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM;IAY1C,MAAM,CAAC,OAAO;IAId;;;;OAIG;IACH,MAAM,CAAC,aAAa,CAAC,aAAa,CAAC,EAAE,MAAM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CAAC,EAAE,MAAM;IAYnC;;;OAGG;IACH,MAAM,CAAC,IAAI;IAIX;;;;;;OAMG;IACH,MAAM,CAAC,UAAU,CACf,kBAAkB,EAAE,MAAM,EAC1B,YAAY,CAAC,EAAE,MAAM,EACrB,QAAQ,CAAC,EAAE,QAAQ,GAClB,IAAI;IAkBP;;;;;;OAMG;WACU,KAAK,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,CAAC;IAS7D;;;;OAIG;IACH,MAAM,CAAC,SAAS,CAAC,UAAU,CAAC,EAAE,MAAM;IAepC;;;OAGG;IACH,MAAM,CAAC,QAAQ;IAIf;;;;OAIG;IACH,MAAM,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM;CAGpC"}
package/lib/index.js CHANGED
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Random = void 0;
4
4
  const chars_constant_1 = require("./constants/chars.constant");
5
5
  const random_constant_1 = require("./constants/random.constant");
6
+ const name_constant_1 = require("./constants/name.constant");
6
7
  class Random {
7
8
  static intBetween(min, max) {
8
9
  // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
@@ -93,5 +94,75 @@ class Random {
93
94
  static uuid() {
94
95
  return crypto.randomUUID();
95
96
  }
97
+ /**
98
+ * Has a n% chance of throwing a error
99
+ * @param probabilityOfError - The probability of a error beeing thrown - i.e 0.2 = 20% chance
100
+ * @param errorMessage - The errormessage to be used instead of the default one
101
+ * @param callback - The callback that should be run if there's no error
102
+ * @returns
103
+ */
104
+ static throwError(probabilityOfError, errorMessage, callback) {
105
+ if (probabilityOfError < 0 || probabilityOfError > 1) {
106
+ // Trying a warning instead of throwing a error since it would probably
107
+ // be masked by the actual intent - throwing a error sometimes
108
+ console.warn("The probability needs to be between 0 and 1");
109
+ return;
110
+ }
111
+ if (Math.random() < probabilityOfError) {
112
+ errorMessage ??= "This is a randomly triggered error";
113
+ throw new Error(errorMessage);
114
+ }
115
+ if (callback) {
116
+ callback();
117
+ }
118
+ }
119
+ /**
120
+ *
121
+ * @param minMs - Minimum milliseconds to delay
122
+ * @param maxMs - Maximum milliseconds to delay
123
+ * @param value - Optional return value
124
+ * @returns
125
+ */
126
+ static async delay(minMs, maxMs, value) {
127
+ return new Promise((resolve, reject) => {
128
+ const delayInMs = Random.intBetween(minMs, maxMs);
129
+ setTimeout(() => {
130
+ resolve(value);
131
+ }, delayInMs);
132
+ });
133
+ }
134
+ /**
135
+ * Returns a name
136
+ * @param nameGender - Only male, female or unisex. If omitted, the name could be either one
137
+ * @returns
138
+ */
139
+ static firstName(nameGender) {
140
+ const namePool = [];
141
+ if (!nameGender || nameGender === name_constant_1.gender.UNISEX) {
142
+ namePool.push(...name_constant_1.firstNameNeutral);
143
+ }
144
+ if (!nameGender || nameGender === name_constant_1.gender.MALE) {
145
+ namePool.push(...name_constant_1.firstNameMale);
146
+ }
147
+ if (!nameGender || nameGender === name_constant_1.gender.FEMALE) {
148
+ namePool.push(...name_constant_1.firstNameFemale);
149
+ }
150
+ return Random.fromArray(namePool);
151
+ }
152
+ /**
153
+ * Gives a surname/last name
154
+ * @returns
155
+ */
156
+ static lastName() {
157
+ return Random.fromArray([...name_constant_1.lastname]);
158
+ }
159
+ /**
160
+ * Gives a full name - first and last name
161
+ * @param nameGender
162
+ * @returns
163
+ */
164
+ static fullName(nameGender) {
165
+ return `${Random.firstName(nameGender)} ${Random.lastName()}`;
166
+ }
96
167
  }
97
168
  exports.Random = Random;
@@ -0,0 +1,3 @@
1
+ import { gender } from "../constants/name.constant";
2
+ export type Gender = (typeof gender)[keyof typeof gender];
3
+ //# sourceMappingURL=name.type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"name.type.d.ts","sourceRoot":"","sources":["../../src/types/name.type.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAC;AAEpD,MAAM,MAAM,MAAM,GAAG,CAAC,OAAO,MAAM,CAAC,CAAC,MAAM,OAAO,MAAM,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kottetall/random",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",
@@ -14,7 +14,22 @@
14
14
  "type": "git",
15
15
  "url": "git+https://github.com/kottetall/random.git"
16
16
  },
17
- "keywords": [],
17
+ "keywords": [
18
+ "random",
19
+ "random-utils",
20
+ "utility",
21
+ "utils",
22
+ "helpers",
23
+ "javascript",
24
+ "node",
25
+ "array",
26
+ "object",
27
+ "picker",
28
+ "random-picker",
29
+ "random-value",
30
+ "test data",
31
+ "mock data"
32
+ ],
18
33
  "author": "",
19
34
  "license": "ISC",
20
35
  "type": "commonjs",
package/readme.md ADDED
@@ -0,0 +1,67 @@
1
+ # Random
2
+
3
+ A lightweight utility library for generating pseudo-random values in JavaScript based of `Math.random()`.
4
+
5
+ This package was originally created for personal use in my own projects, but it is published as an open tool that anyone can use. It focuses on simple, practical helpers for working with randomness, such as picking random values from arrays or objects.
6
+
7
+ ## Features
8
+
9
+ - Get a random value from an array / object
10
+ - Get a random name - first, last, full
11
+ - Get a random letter
12
+ - Get a random truthy/falsy value
13
+ - And more...
14
+ - Simple and minimal API
15
+ - Zero dependencies
16
+ - Designed for everyday use in JavaScript and Node.js projects
17
+
18
+ ## Installation
19
+
20
+ ```bash
21
+ npm install @kottetall/random
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ ### Random names
27
+
28
+ ```js
29
+ import { Random } from "@kottetall/random";
30
+
31
+ console.log(Random.firstName()); // Jack
32
+ console.log(Random.firstName("female")); // Sofia
33
+ console.log(Random.lastName()); // White
34
+ console.log(Random.fullName()); // Jack Hassan
35
+ ```
36
+
37
+ ### Random value from an array
38
+
39
+ ```js
40
+ import { Random } from "@kottetall/random";
41
+
42
+ const items = ["apple", "banana", "orange"];
43
+ const result = Random.fromArray(items);
44
+
45
+ console.log(result);
46
+ ```
47
+
48
+ ### Random value from an object
49
+
50
+ ```js
51
+ import { Random } from "@kottetall/random";
52
+
53
+ const obj = {
54
+ a: 1,
55
+ b: 2,
56
+ c: 3,
57
+ };
58
+
59
+ const result = Random.fromObject(obj);
60
+
61
+ console.log(result);
62
+ ```
63
+
64
+ ## Why this package?
65
+
66
+ Sometimes you just need small, reusable helpers instead of rewriting the same logic in every project.
67
+ This package aims to provide clean and simple functions for common random-related tasks.