@jaypie/fabricator 0.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/dist/Fabricator.d.ts +79 -0
- package/dist/constants.d.ts +6 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +974 -0
- package/dist/random.d.ts +38 -0
- package/dist/util/index.d.ts +4 -0
- package/dist/util/numericSeed.d.ts +8 -0
- package/dist/util/numericSeedArray.d.ts +9 -0
- package/dist/util/uuidToBytes.d.ts +7 -0
- package/dist/util/uuidToNumber.d.ts +7 -0
- package/package.json +42 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Faker } from '@faker-js/faker';
|
|
2
|
+
import { RandomFunction } from './random.js';
|
|
3
|
+
/**
|
|
4
|
+
* Fabricator class for generating test data with faker.js
|
|
5
|
+
* Manages an internal faker instance with optional seeding for deterministic output
|
|
6
|
+
*/
|
|
7
|
+
export declare class Fabricator {
|
|
8
|
+
private _faker;
|
|
9
|
+
private _random;
|
|
10
|
+
/**
|
|
11
|
+
* Creates a new Fabricator instance
|
|
12
|
+
* @param seed - Optional seed (string or number) for deterministic data generation
|
|
13
|
+
*/
|
|
14
|
+
constructor(seed?: string | number);
|
|
15
|
+
/**
|
|
16
|
+
* Gets the internal faker instance
|
|
17
|
+
*/
|
|
18
|
+
get faker(): Faker;
|
|
19
|
+
/**
|
|
20
|
+
* Gets the internal random function
|
|
21
|
+
*/
|
|
22
|
+
random: RandomFunction;
|
|
23
|
+
/**
|
|
24
|
+
* Generates a random word combination using one of three patterns:
|
|
25
|
+
* - adjective noun
|
|
26
|
+
* - adjective verb
|
|
27
|
+
* - noun verb
|
|
28
|
+
* @returns A string with two words following one of the patterns
|
|
29
|
+
*/
|
|
30
|
+
words(): string;
|
|
31
|
+
/**
|
|
32
|
+
* Generate namespace for complex data generation methods
|
|
33
|
+
*/
|
|
34
|
+
generate: {
|
|
35
|
+
/**
|
|
36
|
+
* Generates a person with firstName, middleName, lastName, and fullName
|
|
37
|
+
* Uses probabilistic logic for variations:
|
|
38
|
+
* - firstName: 0.021 (rare) chance to be a lastName instead
|
|
39
|
+
* - middleName: 0.146 (uncommon) chance to be missing, 0.021 (rare) chance to be lastName, 0.00307 (epic) chance to have two
|
|
40
|
+
* - lastName: 0.021 (rare) chance to have two hyphenated
|
|
41
|
+
* - fullName: always "first last", 0.146 (uncommon) chance to include middle
|
|
42
|
+
*
|
|
43
|
+
* @param id - Optional UUID to seed the subfaker. If not provided, generates a new UUID
|
|
44
|
+
* @returns Person object with firstName, middleName, lastName, and fullName
|
|
45
|
+
*/
|
|
46
|
+
person: (id?: string) => {
|
|
47
|
+
id: string;
|
|
48
|
+
firstName: string;
|
|
49
|
+
middleName: string | undefined;
|
|
50
|
+
lastName: string;
|
|
51
|
+
fullName: string;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
get airline(): import('@faker-js/faker').AirlineModule;
|
|
55
|
+
get animal(): import('@faker-js/faker').AnimalModule;
|
|
56
|
+
get color(): import('@faker-js/faker').ColorModule;
|
|
57
|
+
get commerce(): import('@faker-js/faker').CommerceModule;
|
|
58
|
+
get company(): import('@faker-js/faker').CompanyModule;
|
|
59
|
+
get database(): import('@faker-js/faker').DatabaseModule;
|
|
60
|
+
get datatype(): import('@faker-js/faker').DatatypeModule;
|
|
61
|
+
get date(): import('@faker-js/faker').DateModule;
|
|
62
|
+
get finance(): import('@faker-js/faker').FinanceModule;
|
|
63
|
+
get git(): import('@faker-js/faker').GitModule;
|
|
64
|
+
get hacker(): import('@faker-js/faker').HackerModule;
|
|
65
|
+
get helpers(): import('@faker-js/faker').HelpersModule;
|
|
66
|
+
get image(): import('@faker-js/faker').ImageModule;
|
|
67
|
+
get internet(): import('@faker-js/faker').InternetModule;
|
|
68
|
+
get location(): import('@faker-js/faker').LocationModule;
|
|
69
|
+
get lorem(): import('@faker-js/faker').LoremModule;
|
|
70
|
+
get music(): import('@faker-js/faker').MusicModule;
|
|
71
|
+
get number(): import('@faker-js/faker').NumberModule;
|
|
72
|
+
get person(): import('@faker-js/faker').PersonModule;
|
|
73
|
+
get phone(): import('@faker-js/faker').PhoneModule;
|
|
74
|
+
get science(): import('@faker-js/faker').ScienceModule;
|
|
75
|
+
get string(): import('@faker-js/faker').StringModule;
|
|
76
|
+
get system(): import('@faker-js/faker').SystemModule;
|
|
77
|
+
get vehicle(): import('@faker-js/faker').VehicleModule;
|
|
78
|
+
get word(): import('@faker-js/faker').WordModule;
|
|
79
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Fabricator } from './Fabricator.js';
|
|
2
|
+
export { Fabricator } from './Fabricator.js';
|
|
3
|
+
/**
|
|
4
|
+
* Creates and returns a new Fabricator instance with optional seeding
|
|
5
|
+
* @param seed - Optional seed (string or number) for deterministic data generation
|
|
6
|
+
* @returns A new Fabricator instance
|
|
7
|
+
*/
|
|
8
|
+
export declare function fabricator(seed?: string | number): Fabricator;
|
|
9
|
+
export { DEFAULT_MIN, DEFAULT_MAX, random } from './random.js';
|
|
10
|
+
export type { RandomOptions, RandomFunction } from './random.js';
|
|
11
|
+
export * from './util';
|
|
12
|
+
export * from './constants.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,974 @@
|
|
|
1
|
+
import { Faker as M, en as U } from "@faker-js/faker";
|
|
2
|
+
const W = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$/i;
|
|
3
|
+
function Q(r) {
|
|
4
|
+
return typeof r == "string" && W.test(r);
|
|
5
|
+
}
|
|
6
|
+
function y(r) {
|
|
7
|
+
if (!Q(r))
|
|
8
|
+
throw TypeError("Invalid UUID");
|
|
9
|
+
let t;
|
|
10
|
+
return Uint8Array.of((t = parseInt(r.slice(0, 8), 16)) >>> 24, t >>> 16 & 255, t >>> 8 & 255, t & 255, (t = parseInt(r.slice(9, 13), 16)) >>> 8, t & 255, (t = parseInt(r.slice(14, 18), 16)) >>> 8, t & 255, (t = parseInt(r.slice(19, 23), 16)) >>> 8, t & 255, (t = parseInt(r.slice(24, 36), 16)) / 1099511627776 & 255, t / 4294967296 & 255, t >>> 24 & 255, t >>> 16 & 255, t >>> 8 & 255, t & 255);
|
|
11
|
+
}
|
|
12
|
+
function P(r) {
|
|
13
|
+
if (!r)
|
|
14
|
+
return 0;
|
|
15
|
+
const t = y(r), e = Array.from(t.slice(-6)).map((n) => n.toString(16).padStart(2, "0")).join("");
|
|
16
|
+
return parseInt(e, 16);
|
|
17
|
+
}
|
|
18
|
+
const j = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
19
|
+
function w(r) {
|
|
20
|
+
return typeof r == "number" ? Math.abs(r) : j.test(r) ? P(r) : Math.abs(
|
|
21
|
+
r.split("").reduce((t, e) => {
|
|
22
|
+
const n = e.charCodeAt(0);
|
|
23
|
+
return (t << 5) - t + n | 0;
|
|
24
|
+
}, 0)
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
function V(r) {
|
|
28
|
+
if (!r)
|
|
29
|
+
return [0];
|
|
30
|
+
const t = y(r);
|
|
31
|
+
return Array.from(t);
|
|
32
|
+
}
|
|
33
|
+
const J = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
34
|
+
function S(r) {
|
|
35
|
+
if (!J.test(r))
|
|
36
|
+
return [w(r)];
|
|
37
|
+
try {
|
|
38
|
+
return V(r);
|
|
39
|
+
} catch {
|
|
40
|
+
try {
|
|
41
|
+
return [P(r)];
|
|
42
|
+
} catch {
|
|
43
|
+
return [w(r)];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
var Z = Object.defineProperty, z = (r, t, e) => t in r ? Z(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e, T = (r, t, e) => z(r, typeof t != "symbol" ? t + "" : t, e), m = class {
|
|
48
|
+
}, K = class v extends m {
|
|
49
|
+
constructor(t) {
|
|
50
|
+
var e;
|
|
51
|
+
super(), T(this, "_name"), T(this, "_rngFn"), this._name = (e = t.name) != null ? e : "function", this._rngFn = t;
|
|
52
|
+
}
|
|
53
|
+
get name() {
|
|
54
|
+
return this._name;
|
|
55
|
+
}
|
|
56
|
+
next() {
|
|
57
|
+
return this._rngFn();
|
|
58
|
+
}
|
|
59
|
+
clone() {
|
|
60
|
+
return new v(this._rngFn);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
function b(r) {
|
|
64
|
+
switch (typeof r) {
|
|
65
|
+
case "object":
|
|
66
|
+
if (r instanceof m)
|
|
67
|
+
return r;
|
|
68
|
+
break;
|
|
69
|
+
case "function":
|
|
70
|
+
return new K(r);
|
|
71
|
+
default:
|
|
72
|
+
return new nt(r);
|
|
73
|
+
}
|
|
74
|
+
throw new Error(`invalid RNG seed or instance "${r}"`);
|
|
75
|
+
}
|
|
76
|
+
function X(r, t) {
|
|
77
|
+
var e;
|
|
78
|
+
const n = `${r}`;
|
|
79
|
+
let i = 0, s = 0;
|
|
80
|
+
for (; s < n.length; )
|
|
81
|
+
t[255 & s] = 255 & (i ^= ((e = t[255 & s]) != null ? e : 0) * 19) + n.charCodeAt(s++);
|
|
82
|
+
return t.length ? t : [0];
|
|
83
|
+
}
|
|
84
|
+
function G(r, t) {
|
|
85
|
+
for (let e = t.length - 1; e > 0; e -= 1) {
|
|
86
|
+
const n = Math.floor(r.next() * (e + 1)), i = t[e];
|
|
87
|
+
t[e] = t[n], t[n] = i;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
function C(r, t, e) {
|
|
91
|
+
var n, i;
|
|
92
|
+
const s = /* @__PURE__ */ new Map(), a = t.length - 1, u = Array.from({ length: e });
|
|
93
|
+
for (let E = 0; E < e; E++) {
|
|
94
|
+
const h = a - E + 1, A = Math.floor(r.next() * h);
|
|
95
|
+
u[E] = t[(n = s.get(A)) != null ? n : A], s.set(A, (i = s.get(a - E)) != null ? i : a - E);
|
|
96
|
+
}
|
|
97
|
+
return u;
|
|
98
|
+
}
|
|
99
|
+
var tt = 281474976710656, et = 4503599627370496, rt = 9007199254740992, nt = class B extends m {
|
|
100
|
+
constructor(t = crypto.randomUUID()) {
|
|
101
|
+
super(), T(this, "_seed"), T(this, "i"), T(this, "j"), T(this, "S"), this._seed = t;
|
|
102
|
+
const e = X(t, []), n = [], i = e.length;
|
|
103
|
+
this.i = 0, this.j = 0, this.S = n;
|
|
104
|
+
let s = 0;
|
|
105
|
+
for (; s <= 255; )
|
|
106
|
+
n[s] = s++;
|
|
107
|
+
for (let a = 0, u = 0; a <= 255; a++) {
|
|
108
|
+
const E = n[a];
|
|
109
|
+
u = 255 & u + e[a % i] + E, n[a] = n[u], n[u] = E;
|
|
110
|
+
}
|
|
111
|
+
this.g(256);
|
|
112
|
+
}
|
|
113
|
+
get name() {
|
|
114
|
+
return "arc4";
|
|
115
|
+
}
|
|
116
|
+
next() {
|
|
117
|
+
let t = this.g(6), e = tt, n = 0;
|
|
118
|
+
for (; t < et; )
|
|
119
|
+
t = (t + n) * 256, e *= 256, n = this.g(1);
|
|
120
|
+
for (; t >= rt; )
|
|
121
|
+
t /= 2, e /= 2, n >>>= 1;
|
|
122
|
+
return (t + n) / e;
|
|
123
|
+
}
|
|
124
|
+
g(t) {
|
|
125
|
+
const { S: e } = this;
|
|
126
|
+
let { i: n, j: i } = this, s = 0;
|
|
127
|
+
for (; t--; ) {
|
|
128
|
+
n = 255 & n + 1;
|
|
129
|
+
const a = e[n];
|
|
130
|
+
e[i] = a, i = 255 & i + a, e[n] = e[i], s = s * 256 + e[255 & e[n] + a];
|
|
131
|
+
}
|
|
132
|
+
return this.i = n, this.j = i, s;
|
|
133
|
+
}
|
|
134
|
+
clone() {
|
|
135
|
+
return new B(this._seed);
|
|
136
|
+
}
|
|
137
|
+
}, ot = class Y extends m {
|
|
138
|
+
get name() {
|
|
139
|
+
return "Math.random";
|
|
140
|
+
}
|
|
141
|
+
next() {
|
|
142
|
+
return Math.random();
|
|
143
|
+
}
|
|
144
|
+
clone() {
|
|
145
|
+
return new Y();
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
function R(r) {
|
|
149
|
+
return new it(r);
|
|
150
|
+
}
|
|
151
|
+
var it = class {
|
|
152
|
+
constructor(r) {
|
|
153
|
+
T(this, "n"), T(this, "isInt", () => {
|
|
154
|
+
if (Number.isInteger(this.n))
|
|
155
|
+
return this;
|
|
156
|
+
throw new Error(`Expected number to be an integer, got ${this.n}`);
|
|
157
|
+
}), T(this, "isPositive", () => {
|
|
158
|
+
if (this.n > 0)
|
|
159
|
+
return this;
|
|
160
|
+
throw new Error(`Expected number to be positive, got ${this.n}`);
|
|
161
|
+
}), T(this, "lessThan", (t) => {
|
|
162
|
+
if (this.n < t)
|
|
163
|
+
return this;
|
|
164
|
+
throw new Error(`Expected number to be less than ${t}, got ${this.n}`);
|
|
165
|
+
}), T(this, "lessThanOrEqual", (t) => {
|
|
166
|
+
if (this.n <= t)
|
|
167
|
+
return this;
|
|
168
|
+
throw new Error(
|
|
169
|
+
`Expected number to be less than or equal to ${t}, got ${this.n}`
|
|
170
|
+
);
|
|
171
|
+
}), T(this, "greaterThanOrEqual", (t) => {
|
|
172
|
+
if (this.n >= t)
|
|
173
|
+
return this;
|
|
174
|
+
throw new Error(
|
|
175
|
+
`Expected number to be greater than or equal to ${t}, got ${this.n}`
|
|
176
|
+
);
|
|
177
|
+
}), T(this, "greaterThan", (t) => {
|
|
178
|
+
if (this.n > t)
|
|
179
|
+
return this;
|
|
180
|
+
throw new Error(`Expected number to be greater than ${t}, got ${this.n}`);
|
|
181
|
+
}), this.n = r;
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
function st(r, t = 1) {
|
|
185
|
+
R(t).isInt().isPositive();
|
|
186
|
+
const e = r.irwinHall(t);
|
|
187
|
+
return () => e() / t;
|
|
188
|
+
}
|
|
189
|
+
function at(r, t = 0.5) {
|
|
190
|
+
return R(t).greaterThanOrEqual(0).lessThanOrEqual(1), () => Math.min(1, Math.floor(r.next() + t));
|
|
191
|
+
}
|
|
192
|
+
function Et(r, t = 1, e = 0.5) {
|
|
193
|
+
return R(t).isInt().isPositive(), R(e).greaterThanOrEqual(0).lessThan(1), () => {
|
|
194
|
+
let n = 0, i = 0;
|
|
195
|
+
for (; n++ < t; )
|
|
196
|
+
r.next() < e && i++;
|
|
197
|
+
return i;
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
function ut(r, t = 1) {
|
|
201
|
+
return R(t).isPositive(), () => -Math.log(1 - r.next()) / t;
|
|
202
|
+
}
|
|
203
|
+
function ht(r, t = 0.5) {
|
|
204
|
+
R(t).greaterThan(0).lessThan(1);
|
|
205
|
+
const e = 1 / Math.log(1 - t);
|
|
206
|
+
return () => Math.floor(1 + Math.log(r.next()) * e);
|
|
207
|
+
}
|
|
208
|
+
function ct(r, t = 1) {
|
|
209
|
+
return R(t).isInt().greaterThanOrEqual(0), () => {
|
|
210
|
+
let e = 0;
|
|
211
|
+
for (let n = 0; n < t; ++n)
|
|
212
|
+
e += r.next();
|
|
213
|
+
return e;
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
function ft(r, t = 0, e = 1) {
|
|
217
|
+
const n = r.normal(t, e);
|
|
218
|
+
return () => Math.exp(n());
|
|
219
|
+
}
|
|
220
|
+
function lt(r, t = 0, e = 1) {
|
|
221
|
+
return () => {
|
|
222
|
+
let n, i, s;
|
|
223
|
+
do
|
|
224
|
+
n = r.next() * 2 - 1, i = r.next() * 2 - 1, s = n * n + i * i;
|
|
225
|
+
while (!s || s > 1);
|
|
226
|
+
return t + e * i * Math.sqrt(-2 * Math.log(s) / s);
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
function Tt(r, t = 1) {
|
|
230
|
+
R(t).greaterThanOrEqual(0);
|
|
231
|
+
const e = 1 / t;
|
|
232
|
+
return () => 1 / Math.pow(1 - r.next(), e);
|
|
233
|
+
}
|
|
234
|
+
var At = [
|
|
235
|
+
0,
|
|
236
|
+
0,
|
|
237
|
+
0.6931471805599453,
|
|
238
|
+
1.791759469228055,
|
|
239
|
+
3.1780538303479458,
|
|
240
|
+
4.787491742782046,
|
|
241
|
+
6.579251212010101,
|
|
242
|
+
8.525161361065415,
|
|
243
|
+
10.60460290274525,
|
|
244
|
+
12.801827480081469
|
|
245
|
+
], Rt = (r) => At[r], Ot = 0.9189385332046727;
|
|
246
|
+
function _t(r, t = 1) {
|
|
247
|
+
if (R(t).isPositive(), t < 10) {
|
|
248
|
+
const e = Math.exp(-t);
|
|
249
|
+
return () => {
|
|
250
|
+
let n = e, i = 0, s = r.next();
|
|
251
|
+
for (; s > n; )
|
|
252
|
+
s = s - n, n = t * n / ++i;
|
|
253
|
+
return i;
|
|
254
|
+
};
|
|
255
|
+
} else {
|
|
256
|
+
const e = Math.sqrt(t), n = 0.931 + 2.53 * e, i = -0.059 + 0.02483 * n, s = 1.1239 + 1.1328 / (n - 3.4), a = 0.9277 - 3.6224 / (n - 2);
|
|
257
|
+
return () => {
|
|
258
|
+
for (var u; ; ) {
|
|
259
|
+
let E, h = r.next();
|
|
260
|
+
if (h <= 0.86 * a)
|
|
261
|
+
return E = h / a - 0.43, Math.floor(
|
|
262
|
+
(2 * i / (0.5 - Math.abs(E)) + n) * E + t + 0.445
|
|
263
|
+
);
|
|
264
|
+
h >= a ? E = r.next() - 0.5 : (E = h / a - 0.93, E = (E < 0 ? -0.5 : 0.5) - E, h = r.next() * a);
|
|
265
|
+
const A = 0.5 - Math.abs(E);
|
|
266
|
+
if (A < 0.013 && h > A)
|
|
267
|
+
continue;
|
|
268
|
+
const c = Math.floor((2 * i / A + n) * E + t + 0.445);
|
|
269
|
+
if (h = h * s / (i / (A * A) + n), c >= 10) {
|
|
270
|
+
const _ = (c + 0.5) * Math.log(t / c) - t - Ot + c - (0.08333333333333333 - (0.002777777777777778 - 1 / (1260 * c * c)) / (c * c)) / c;
|
|
271
|
+
if (Math.log(h * e) <= _)
|
|
272
|
+
return c;
|
|
273
|
+
} else if (c >= 0) {
|
|
274
|
+
const _ = (u = Rt(c)) != null ? u : 0;
|
|
275
|
+
if (Math.log(h) <= c * Math.log(t) - t - _)
|
|
276
|
+
return c;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
function Nt(r, t = 0, e = 1) {
|
|
283
|
+
return () => r.next() * (e - t) + t;
|
|
284
|
+
}
|
|
285
|
+
function pt(r) {
|
|
286
|
+
return () => r.next() >= 0.5;
|
|
287
|
+
}
|
|
288
|
+
function gt(r, t = 0, e = 1) {
|
|
289
|
+
return e === void 0 && (e = t === void 0 ? 1 : t, t = 0), R(t).isInt(), R(e).isInt(), () => Math.floor(r.next() * (e - t + 1) + t);
|
|
290
|
+
}
|
|
291
|
+
function It(r, t, e) {
|
|
292
|
+
return R(t).greaterThan(0), R(e).greaterThan(0), () => {
|
|
293
|
+
const n = 1 - r.next();
|
|
294
|
+
return t * Math.pow(-Math.log(n), 1 / e);
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
var mt = class F {
|
|
298
|
+
constructor(t = new ot()) {
|
|
299
|
+
T(this, "_rng"), T(this, "_cache", {}), this._rng = b(t);
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* @member {RNG} rng - Underlying pseudo-random number generator.
|
|
303
|
+
*/
|
|
304
|
+
get rng() {
|
|
305
|
+
return this._rng;
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Creates a new `Random` instance, optionally specifying parameters to
|
|
309
|
+
* set a new seed.
|
|
310
|
+
*/
|
|
311
|
+
clone(t = this.rng.clone()) {
|
|
312
|
+
return new F(t);
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Sets the underlying pseudorandom number generator.
|
|
316
|
+
*
|
|
317
|
+
* @example
|
|
318
|
+
* ```ts
|
|
319
|
+
* import random from 'random'
|
|
320
|
+
*
|
|
321
|
+
* random.use('example-seed')
|
|
322
|
+
* // or
|
|
323
|
+
* random.use(Math.random)
|
|
324
|
+
* ```
|
|
325
|
+
*/
|
|
326
|
+
use(t) {
|
|
327
|
+
this._rng = b(t), this._cache = {};
|
|
328
|
+
}
|
|
329
|
+
// --------------------------------------------------------------------------
|
|
330
|
+
// Uniform utility functions
|
|
331
|
+
// --------------------------------------------------------------------------
|
|
332
|
+
/**
|
|
333
|
+
* Convenience wrapper around `this.rng.next()`
|
|
334
|
+
*
|
|
335
|
+
* Returns a floating point number in [0, 1).
|
|
336
|
+
*
|
|
337
|
+
* @return {number}
|
|
338
|
+
*/
|
|
339
|
+
next() {
|
|
340
|
+
return this._rng.next();
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Samples a uniform random floating point number, optionally specifying
|
|
344
|
+
* lower and upper bounds.
|
|
345
|
+
*
|
|
346
|
+
* Convenience wrapper around `random.uniform()`
|
|
347
|
+
*
|
|
348
|
+
* @param {number} [min=0] - Lower bound (float, inclusive)
|
|
349
|
+
* @param {number} [max=1] - Upper bound (float, exclusive)
|
|
350
|
+
*/
|
|
351
|
+
float(t, e) {
|
|
352
|
+
return this.uniform(t, e)();
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Samples a uniform random integer, optionally specifying lower and upper
|
|
356
|
+
* bounds.
|
|
357
|
+
*
|
|
358
|
+
* Convenience wrapper around `random.uniformInt()`
|
|
359
|
+
*
|
|
360
|
+
* @param {number} [min=0] - Lower bound (integer, inclusive)
|
|
361
|
+
* @param {number} [max=1] - Upper bound (integer, inclusive)
|
|
362
|
+
*/
|
|
363
|
+
int(t, e) {
|
|
364
|
+
return this.uniformInt(t, e)();
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Samples a uniform random integer, optionally specifying lower and upper
|
|
368
|
+
* bounds.
|
|
369
|
+
*
|
|
370
|
+
* Convenience wrapper around `random.uniformInt()`
|
|
371
|
+
*
|
|
372
|
+
* @alias `random.int`
|
|
373
|
+
*
|
|
374
|
+
* @param {number} [min=0] - Lower bound (integer, inclusive)
|
|
375
|
+
* @param {number} [max=1] - Upper bound (integer, inclusive)
|
|
376
|
+
*/
|
|
377
|
+
integer(t, e) {
|
|
378
|
+
return this.uniformInt(t, e)();
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Samples a uniform random boolean value.
|
|
382
|
+
*
|
|
383
|
+
* Convenience wrapper around `random.uniformBoolean()`
|
|
384
|
+
*
|
|
385
|
+
* @alias `random.boolean`
|
|
386
|
+
*/
|
|
387
|
+
bool() {
|
|
388
|
+
return this.uniformBoolean()();
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Samples a uniform random boolean value.
|
|
392
|
+
*
|
|
393
|
+
* Convenience wrapper around `random.uniformBoolean()`
|
|
394
|
+
*/
|
|
395
|
+
boolean() {
|
|
396
|
+
return this.uniformBoolean()();
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Returns an item chosen uniformly at random from the given array.
|
|
400
|
+
*
|
|
401
|
+
* Convenience wrapper around `random.uniformInt()`
|
|
402
|
+
*
|
|
403
|
+
* @param {Array<T>} [array] - Input array
|
|
404
|
+
*/
|
|
405
|
+
choice(t) {
|
|
406
|
+
if (!Array.isArray(t))
|
|
407
|
+
throw new TypeError(
|
|
408
|
+
`Random.choice expected input to be an array, got ${typeof t}`
|
|
409
|
+
);
|
|
410
|
+
const e = t.length;
|
|
411
|
+
if (e > 0) {
|
|
412
|
+
const n = this.uniformInt(0, e - 1)();
|
|
413
|
+
return t[n];
|
|
414
|
+
} else
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Returns a random subset of k items from the given array (without replacement).
|
|
419
|
+
*
|
|
420
|
+
* @param {Array<T>} [array] - Input array
|
|
421
|
+
*/
|
|
422
|
+
sample(t, e) {
|
|
423
|
+
if (!Array.isArray(t))
|
|
424
|
+
throw new TypeError(
|
|
425
|
+
`Random.sample expected input to be an array, got ${typeof t}`
|
|
426
|
+
);
|
|
427
|
+
if (e < 0 || e > t.length)
|
|
428
|
+
throw new Error(
|
|
429
|
+
`Random.sample: k must be between 0 and array.length (${t.length}), got ${e}`
|
|
430
|
+
);
|
|
431
|
+
return C(this.rng, t, e);
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Generates a thunk which returns samples of size k from the given array.
|
|
435
|
+
*
|
|
436
|
+
* This is for convenience only; there is no gain in efficiency.
|
|
437
|
+
*
|
|
438
|
+
* @param {Array<T>} [array] - Input array
|
|
439
|
+
*/
|
|
440
|
+
sampler(t, e) {
|
|
441
|
+
if (!Array.isArray(t))
|
|
442
|
+
throw new TypeError(
|
|
443
|
+
`Random.sampler expected input to be an array, got ${typeof t}`
|
|
444
|
+
);
|
|
445
|
+
if (e < 0 || e > t.length)
|
|
446
|
+
throw new Error(
|
|
447
|
+
`Random.sampler: k must be between 0 and array.length (${t.length}), got ${e}`
|
|
448
|
+
);
|
|
449
|
+
const n = this.rng;
|
|
450
|
+
return () => C(n, t, e);
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Returns a shuffled copy of the given array.
|
|
454
|
+
*
|
|
455
|
+
* @param {Array<T>} [array] - Input array
|
|
456
|
+
*/
|
|
457
|
+
shuffle(t) {
|
|
458
|
+
if (!Array.isArray(t))
|
|
459
|
+
throw new TypeError(
|
|
460
|
+
`Random.shuffle expected input to be an array, got ${typeof t}`
|
|
461
|
+
);
|
|
462
|
+
const e = [...t];
|
|
463
|
+
return G(this.rng, e), e;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Generates a thunk which returns shuffled copies of the given array.
|
|
467
|
+
*
|
|
468
|
+
* @param {Array<T>} [array] - Input array
|
|
469
|
+
*/
|
|
470
|
+
shuffler(t) {
|
|
471
|
+
if (!Array.isArray(t))
|
|
472
|
+
throw new TypeError(
|
|
473
|
+
`Random.shuffler expected input to be an array, got ${typeof t}`
|
|
474
|
+
);
|
|
475
|
+
const e = this.rng, n = [...t];
|
|
476
|
+
return () => (G(e, n), [...n]);
|
|
477
|
+
}
|
|
478
|
+
// --------------------------------------------------------------------------
|
|
479
|
+
// Uniform distributions
|
|
480
|
+
// --------------------------------------------------------------------------
|
|
481
|
+
/**
|
|
482
|
+
* Generates a [Continuous uniform distribution](https://en.wikipedia.org/wiki/Uniform_distribution_(continuous)).
|
|
483
|
+
*
|
|
484
|
+
* @param {number} [min=0] - Lower bound (float, inclusive)
|
|
485
|
+
* @param {number} [max=1] - Upper bound (float, exclusive)
|
|
486
|
+
*/
|
|
487
|
+
uniform(t, e) {
|
|
488
|
+
return this._memoize("uniform", Nt, t, e);
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Generates a [Discrete uniform distribution](https://en.wikipedia.org/wiki/Discrete_uniform_distribution).
|
|
492
|
+
*
|
|
493
|
+
* @param {number} [min=0] - Lower bound (integer, inclusive)
|
|
494
|
+
* @param {number} [max=1] - Upper bound (integer, inclusive)
|
|
495
|
+
*/
|
|
496
|
+
uniformInt(t, e) {
|
|
497
|
+
return this._memoize("uniformInt", gt, t, e);
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Generates a [Discrete uniform distribution](https://en.wikipedia.org/wiki/Discrete_uniform_distribution),
|
|
501
|
+
* with two possible outcomes, `true` or `false.
|
|
502
|
+
*
|
|
503
|
+
* This method is analogous to flipping a coin.
|
|
504
|
+
*/
|
|
505
|
+
uniformBoolean() {
|
|
506
|
+
return this._memoize("uniformBoolean", pt);
|
|
507
|
+
}
|
|
508
|
+
// --------------------------------------------------------------------------
|
|
509
|
+
// Normal distributions
|
|
510
|
+
// --------------------------------------------------------------------------
|
|
511
|
+
/**
|
|
512
|
+
* Generates a [Normal distribution](https://en.wikipedia.org/wiki/Normal_distribution).
|
|
513
|
+
*
|
|
514
|
+
* @param {number} [mu=0] - Mean
|
|
515
|
+
* @param {number} [sigma=1] - Standard deviation
|
|
516
|
+
*/
|
|
517
|
+
normal(t, e) {
|
|
518
|
+
return lt(this, t, e);
|
|
519
|
+
}
|
|
520
|
+
/**
|
|
521
|
+
* Generates a [Log-normal distribution](https://en.wikipedia.org/wiki/Log-normal_distribution).
|
|
522
|
+
*
|
|
523
|
+
* @param {number} [mu=0] - Mean of underlying normal distribution
|
|
524
|
+
* @param {number} [sigma=1] - Standard deviation of underlying normal distribution
|
|
525
|
+
*/
|
|
526
|
+
logNormal(t, e) {
|
|
527
|
+
return ft(this, t, e);
|
|
528
|
+
}
|
|
529
|
+
// --------------------------------------------------------------------------
|
|
530
|
+
// Bernoulli distributions
|
|
531
|
+
// --------------------------------------------------------------------------
|
|
532
|
+
/**
|
|
533
|
+
* Generates a [Bernoulli distribution](https://en.wikipedia.org/wiki/Bernoulli_distribution).
|
|
534
|
+
*
|
|
535
|
+
* @param {number} [p=0.5] - Success probability of each trial.
|
|
536
|
+
*/
|
|
537
|
+
bernoulli(t) {
|
|
538
|
+
return at(this, t);
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Generates a [Binomial distribution](https://en.wikipedia.org/wiki/Binomial_distribution).
|
|
542
|
+
*
|
|
543
|
+
* @param {number} [n=1] - Number of trials.
|
|
544
|
+
* @param {number} [p=0.5] - Success probability of each trial.
|
|
545
|
+
*/
|
|
546
|
+
binomial(t, e) {
|
|
547
|
+
return Et(this, t, e);
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* Generates a [Geometric distribution](https://en.wikipedia.org/wiki/Geometric_distribution).
|
|
551
|
+
*
|
|
552
|
+
* @param {number} [p=0.5] - Success probability of each trial.
|
|
553
|
+
*/
|
|
554
|
+
geometric(t) {
|
|
555
|
+
return ht(this, t);
|
|
556
|
+
}
|
|
557
|
+
// --------------------------------------------------------------------------
|
|
558
|
+
// Poisson distributions
|
|
559
|
+
// --------------------------------------------------------------------------
|
|
560
|
+
/**
|
|
561
|
+
* Generates a [Poisson distribution](https://en.wikipedia.org/wiki/Poisson_distribution).
|
|
562
|
+
*
|
|
563
|
+
* @param {number} [lambda=1] - Mean (lambda > 0)
|
|
564
|
+
*/
|
|
565
|
+
poisson(t) {
|
|
566
|
+
return _t(this, t);
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* Generates an [Exponential distribution](https://en.wikipedia.org/wiki/Exponential_distribution).
|
|
570
|
+
*
|
|
571
|
+
* @param {number} [lambda=1] - Inverse mean (lambda > 0)
|
|
572
|
+
*/
|
|
573
|
+
exponential(t) {
|
|
574
|
+
return ut(this, t);
|
|
575
|
+
}
|
|
576
|
+
// --------------------------------------------------------------------------
|
|
577
|
+
// Misc distributions
|
|
578
|
+
// --------------------------------------------------------------------------
|
|
579
|
+
/**
|
|
580
|
+
* Generates an [Irwin Hall distribution](https://en.wikipedia.org/wiki/Irwin%E2%80%93Hall_distribution).
|
|
581
|
+
*
|
|
582
|
+
* @param {number} [n=1] - Number of uniform samples to sum (n >= 0)
|
|
583
|
+
*/
|
|
584
|
+
irwinHall(t) {
|
|
585
|
+
return ct(this, t);
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* Generates a [Bates distribution](https://en.wikipedia.org/wiki/Bates_distribution).
|
|
589
|
+
*
|
|
590
|
+
* @param {number} [n=1] - Number of uniform samples to average (n >= 1)
|
|
591
|
+
*/
|
|
592
|
+
bates(t) {
|
|
593
|
+
return st(this, t);
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* Generates a [Pareto distribution](https://en.wikipedia.org/wiki/Pareto_distribution).
|
|
597
|
+
*
|
|
598
|
+
* @param {number} [alpha=1] - Alpha
|
|
599
|
+
*/
|
|
600
|
+
pareto(t) {
|
|
601
|
+
return Tt(this, t);
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* Generates a [Weibull distribution](https://en.wikipedia.org/wiki/Weibull_distribution).
|
|
605
|
+
*
|
|
606
|
+
* @param {number} [lambda] - Lambda, the scale parameter
|
|
607
|
+
* @param {number} [k] - k, the shape parameter
|
|
608
|
+
*/
|
|
609
|
+
weibull(t, e) {
|
|
610
|
+
return It(this, t, e);
|
|
611
|
+
}
|
|
612
|
+
// --------------------------------------------------------------------------
|
|
613
|
+
// Internal
|
|
614
|
+
// --------------------------------------------------------------------------
|
|
615
|
+
/**
|
|
616
|
+
* Memoizes distributions to ensure they're only created when necessary.
|
|
617
|
+
*
|
|
618
|
+
* Returns a thunk which that returns independent, identically distributed
|
|
619
|
+
* samples from the specified distribution.
|
|
620
|
+
*
|
|
621
|
+
* @internal
|
|
622
|
+
*
|
|
623
|
+
* @param {string} label - Name of distribution
|
|
624
|
+
* @param {function} getter - Function which generates a new distribution
|
|
625
|
+
* @param {...*} args - Distribution-specific arguments
|
|
626
|
+
*/
|
|
627
|
+
_memoize(t, e, ...n) {
|
|
628
|
+
const i = `${n.join(";")}`;
|
|
629
|
+
let s = this._cache[t];
|
|
630
|
+
return (s === void 0 || s.key !== i) && (s = {
|
|
631
|
+
key: i,
|
|
632
|
+
distribution: e(this, ...n)
|
|
633
|
+
}, this._cache[t] = s), s.distribution;
|
|
634
|
+
}
|
|
635
|
+
}, x = new mt();
|
|
636
|
+
const f = {
|
|
637
|
+
CODE: {
|
|
638
|
+
BAD_GATEWAY: 502,
|
|
639
|
+
BAD_REQUEST: 400,
|
|
640
|
+
FORBIDDEN: 403,
|
|
641
|
+
GATEWAY_TIMEOUT: 504,
|
|
642
|
+
GONE: 410,
|
|
643
|
+
INTERNAL_ERROR: 500,
|
|
644
|
+
METHOD_NOT_ALLOWED: 405,
|
|
645
|
+
NOT_FOUND: 404,
|
|
646
|
+
TEAPOT: 418,
|
|
647
|
+
TOO_MANY_REQUESTS: 429,
|
|
648
|
+
UNAUTHORIZED: 401,
|
|
649
|
+
UNAVAILABLE: 503
|
|
650
|
+
}
|
|
651
|
+
}, o = {
|
|
652
|
+
MESSAGE: {
|
|
653
|
+
BAD_GATEWAY: "An unexpected error occurred on an upstream resource",
|
|
654
|
+
BAD_REQUEST: "The request was not properly formatted",
|
|
655
|
+
CONFIGURATION_ERROR: "The application responding to the request encountered a configuration error",
|
|
656
|
+
CORS_ERROR: "The requesting origin is not authorized to this resource",
|
|
657
|
+
FORBIDDEN: "Access to this resource is not authorized",
|
|
658
|
+
GATEWAY_TIMEOUT: "The connection timed out waiting for an upstream resource",
|
|
659
|
+
GONE: "The requested resource is no longer available",
|
|
660
|
+
ILLOGICAL: "The application encountered an illogical condition while processing the request",
|
|
661
|
+
INTERNAL_ERROR: "An unexpected error occurred and the request was unable to complete",
|
|
662
|
+
METHOD_NOT_ALLOWED: "The requested method is not allowed",
|
|
663
|
+
NOT_FOUND: "The requested resource was not found",
|
|
664
|
+
NOT_IMPLEMENTED: "The request was understood but the resource is not implemented",
|
|
665
|
+
REJECTED: "The request was rejected prior to processing",
|
|
666
|
+
TEAPOT: "This resource is a teapot incapable of processing the request",
|
|
667
|
+
TOO_MANY_REQUESTS: "The requesting origin exceeded the request limit",
|
|
668
|
+
UNAUTHORIZED: "The request did not include valid authentication credentials",
|
|
669
|
+
UNAVAILABLE: "The requested resource is temporarily unavailable",
|
|
670
|
+
UNHANDLED: "An unhandled error occurred and the request was unable to complete",
|
|
671
|
+
UNREACHABLE_CODE: "The application encountered an unreachable condition while processing the request"
|
|
672
|
+
},
|
|
673
|
+
TITLE: {
|
|
674
|
+
BAD_GATEWAY: "Bad Gateway",
|
|
675
|
+
BAD_REQUEST: "Bad Request",
|
|
676
|
+
CONFIGURATION_ERROR: "Internal Configuration Error",
|
|
677
|
+
CORS_ERROR: "Unauthorized Origin",
|
|
678
|
+
FORBIDDEN: "Forbidden",
|
|
679
|
+
GATEWAY_TIMEOUT: "Gateway Timeout",
|
|
680
|
+
GONE: "Gone",
|
|
681
|
+
INTERNAL_ERROR: "Internal Application Error",
|
|
682
|
+
METHOD_NOT_ALLOWED: "Method Not Allowed",
|
|
683
|
+
NOT_FOUND: "Not Found",
|
|
684
|
+
NOT_IMPLEMENTED: "Not Implemented",
|
|
685
|
+
REJECTED: "Request Rejected",
|
|
686
|
+
TEAPOT: "Teapot",
|
|
687
|
+
TOO_MANY_REQUESTS: "Too Many Requests",
|
|
688
|
+
UNAUTHORIZED: "Service Unauthorized",
|
|
689
|
+
UNAVAILABLE: "Service Unavailable"
|
|
690
|
+
},
|
|
691
|
+
TYPE: {
|
|
692
|
+
BAD_GATEWAY: "BAD_GATEWAY",
|
|
693
|
+
BAD_REQUEST: "BAD_REQUEST",
|
|
694
|
+
CONFIGURATION_ERROR: "CONFIGURATION_ERROR",
|
|
695
|
+
CORS_ERROR: "CORS_ERROR",
|
|
696
|
+
FORBIDDEN: "FORBIDDEN",
|
|
697
|
+
GATEWAY_TIMEOUT: "GATEWAY_TIMEOUT",
|
|
698
|
+
GONE: "GONE",
|
|
699
|
+
ILLOGICAL: "ILLOGICAL",
|
|
700
|
+
INTERNAL_ERROR: "INTERNAL_ERROR",
|
|
701
|
+
METHOD_NOT_ALLOWED: "METHOD_NOT_ALLOWED",
|
|
702
|
+
NOT_FOUND: "NOT_FOUND",
|
|
703
|
+
NOT_IMPLEMENTED: "NOT_IMPLEMENTED",
|
|
704
|
+
REJECTED: "REJECTED",
|
|
705
|
+
TEAPOT: "TEAPOT",
|
|
706
|
+
TOO_MANY_REQUESTS: "TOO_MANY_REQUESTS",
|
|
707
|
+
UNAUTHORIZED: "UNAUTHORIZED",
|
|
708
|
+
UNAVAILABLE: "UNAVAILABLE",
|
|
709
|
+
UNHANDLED: "UNHANDLED",
|
|
710
|
+
UNKNOWN_TYPE: "UNKNOWN_TYPE",
|
|
711
|
+
UNREACHABLE_CODE: "UNREACHABLE_CODE"
|
|
712
|
+
}
|
|
713
|
+
}, Dt = "JaypieError";
|
|
714
|
+
class dt extends Error {
|
|
715
|
+
constructor(t = o.MESSAGE.INTERNAL_ERROR, { status: e = f.CODE.INTERNAL_ERROR, title: n = o.TITLE.INTERNAL_ERROR } = {}, { _type: i = o.TYPE.UNKNOWN_TYPE } = {}) {
|
|
716
|
+
super(t), this.title = n, this.detail = t, this.status = e, this.name = Dt, this.isProjectError = !0, this.isJaypieError = !0, this._type = i, this.body = () => ({
|
|
717
|
+
errors: [
|
|
718
|
+
{
|
|
719
|
+
status: this.status,
|
|
720
|
+
title: this.title,
|
|
721
|
+
detail: this.detail
|
|
722
|
+
}
|
|
723
|
+
]
|
|
724
|
+
}), this.json = () => ({
|
|
725
|
+
status: this.status,
|
|
726
|
+
title: this.title,
|
|
727
|
+
detail: this.detail
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
const Lt = {
|
|
732
|
+
apply: (r, t, e) => new r(...e)
|
|
733
|
+
};
|
|
734
|
+
function l(r, t, e, n) {
|
|
735
|
+
return new Proxy(class extends dt {
|
|
736
|
+
constructor(i = r) {
|
|
737
|
+
super(i, { status: t, title: e }, { _type: n });
|
|
738
|
+
}
|
|
739
|
+
}, Lt);
|
|
740
|
+
}
|
|
741
|
+
l(o.MESSAGE.BAD_GATEWAY, f.CODE.BAD_GATEWAY, o.TITLE.BAD_GATEWAY, o.TYPE.BAD_GATEWAY);
|
|
742
|
+
l(o.MESSAGE.BAD_REQUEST, f.CODE.BAD_REQUEST, o.TITLE.BAD_REQUEST, o.TYPE.BAD_REQUEST);
|
|
743
|
+
l(o.MESSAGE.FORBIDDEN, f.CODE.FORBIDDEN, o.TITLE.FORBIDDEN, o.TYPE.FORBIDDEN);
|
|
744
|
+
l(o.MESSAGE.GATEWAY_TIMEOUT, f.CODE.GATEWAY_TIMEOUT, o.TITLE.GATEWAY_TIMEOUT, o.TYPE.GATEWAY_TIMEOUT);
|
|
745
|
+
l(o.MESSAGE.GONE, f.CODE.GONE, o.TITLE.GONE, o.TYPE.GONE);
|
|
746
|
+
l(o.MESSAGE.INTERNAL_ERROR, f.CODE.INTERNAL_ERROR, o.TITLE.INTERNAL_ERROR, o.TYPE.INTERNAL_ERROR);
|
|
747
|
+
l(o.MESSAGE.METHOD_NOT_ALLOWED, f.CODE.METHOD_NOT_ALLOWED, o.TITLE.METHOD_NOT_ALLOWED, o.TYPE.METHOD_NOT_ALLOWED);
|
|
748
|
+
l(o.MESSAGE.NOT_FOUND, f.CODE.NOT_FOUND, o.TITLE.NOT_FOUND, o.TYPE.NOT_FOUND);
|
|
749
|
+
l(o.MESSAGE.TEAPOT, f.CODE.TEAPOT, o.TITLE.TEAPOT, o.TYPE.TEAPOT);
|
|
750
|
+
l(o.MESSAGE.TOO_MANY_REQUESTS, f.CODE.TOO_MANY_REQUESTS, o.TITLE.TOO_MANY_REQUESTS, o.TYPE.TOO_MANY_REQUESTS);
|
|
751
|
+
l(o.MESSAGE.UNAUTHORIZED, f.CODE.UNAUTHORIZED, o.TITLE.UNAUTHORIZED, o.TYPE.UNAUTHORIZED);
|
|
752
|
+
l(o.MESSAGE.UNAVAILABLE, f.CODE.UNAVAILABLE, o.TITLE.UNAVAILABLE, o.TYPE.UNAVAILABLE);
|
|
753
|
+
const Mt = l(o.MESSAGE.CONFIGURATION_ERROR, f.CODE.INTERNAL_ERROR, o.TITLE.CONFIGURATION_ERROR, o.TYPE.CONFIGURATION_ERROR);
|
|
754
|
+
l(o.MESSAGE.CORS_ERROR, f.CODE.UNAUTHORIZED, o.TITLE.CORS_ERROR, o.TYPE.CORS_ERROR);
|
|
755
|
+
l(o.MESSAGE.ILLOGICAL, f.CODE.INTERNAL_ERROR, o.TITLE.INTERNAL_ERROR, o.TYPE.ILLOGICAL);
|
|
756
|
+
l(o.MESSAGE.NOT_IMPLEMENTED, f.CODE.BAD_REQUEST, o.TITLE.NOT_IMPLEMENTED, o.TYPE.NOT_IMPLEMENTED);
|
|
757
|
+
l(o.MESSAGE.REJECTED, f.CODE.FORBIDDEN, o.TITLE.REJECTED, o.TYPE.REJECTED);
|
|
758
|
+
l(o.MESSAGE.UNHANDLED, f.CODE.INTERNAL_ERROR, o.TITLE.INTERNAL_ERROR, o.TYPE.UNHANDLED);
|
|
759
|
+
l(o.MESSAGE.UNREACHABLE_CODE, f.CODE.INTERNAL_ERROR, o.TITLE.INTERNAL_ERROR, o.TYPE.UNREACHABLE_CODE);
|
|
760
|
+
const Ut = 0, wt = 1, St = (r, t, e) => {
|
|
761
|
+
let n = r;
|
|
762
|
+
return typeof t == "number" && (n = Math.max(n, t)), typeof e == "number" && (n = Math.min(n, e)), n;
|
|
763
|
+
}, bt = (r, t) => {
|
|
764
|
+
if (typeof r == "number" && typeof t == "number" && r > t)
|
|
765
|
+
throw new Mt(
|
|
766
|
+
`Invalid bounds: min (${r}) cannot be greater than max (${t})`
|
|
767
|
+
);
|
|
768
|
+
};
|
|
769
|
+
function Gt(r) {
|
|
770
|
+
const t = r !== void 0 ? String(r) : void 0, e = x.clone(t), n = /* @__PURE__ */ new Map();
|
|
771
|
+
return ({
|
|
772
|
+
min: s,
|
|
773
|
+
max: a,
|
|
774
|
+
mean: u,
|
|
775
|
+
stddev: E,
|
|
776
|
+
integer: h = !1,
|
|
777
|
+
start: A = 0,
|
|
778
|
+
seed: c,
|
|
779
|
+
precision: _,
|
|
780
|
+
currency: $ = !1
|
|
781
|
+
} = {}) => {
|
|
782
|
+
const g = c ? n.get(c) || (() => {
|
|
783
|
+
const O = x.clone(c);
|
|
784
|
+
return n.set(c, O), O;
|
|
785
|
+
})() : e;
|
|
786
|
+
let N = a;
|
|
787
|
+
typeof s == "number" && typeof N != "number" && (N = s * 2), bt(s, N);
|
|
788
|
+
const H = () => {
|
|
789
|
+
if (h) return 0;
|
|
790
|
+
const O = [];
|
|
791
|
+
return typeof _ == "number" && _ >= 0 && O.push(_), $ && O.push(2), O.length > 0 ? Math.min(...O) : void 0;
|
|
792
|
+
}, D = (O) => {
|
|
793
|
+
const I = H();
|
|
794
|
+
if (typeof I == "number") {
|
|
795
|
+
const p = Math.pow(10, I);
|
|
796
|
+
return Math.round(O * p) / p;
|
|
797
|
+
}
|
|
798
|
+
return O;
|
|
799
|
+
};
|
|
800
|
+
if (typeof u == "number" && typeof E == "number") {
|
|
801
|
+
const I = g.normal(u, E)(), p = St(I, s, a);
|
|
802
|
+
if (typeof s == "number" && typeof a == "number" && p !== I) {
|
|
803
|
+
const q = h ? g.int(s, N) : g.float(s, N);
|
|
804
|
+
return D(A + q);
|
|
805
|
+
}
|
|
806
|
+
return D(
|
|
807
|
+
A + (h ? Math.round(p) : p)
|
|
808
|
+
);
|
|
809
|
+
}
|
|
810
|
+
const d = typeof s == "number" ? s : Ut, L = typeof N == "number" ? N : wt, k = h ? g.int(d, L) : g.float(d, L);
|
|
811
|
+
return D(A + k);
|
|
812
|
+
};
|
|
813
|
+
}
|
|
814
|
+
class Ct {
|
|
815
|
+
/**
|
|
816
|
+
* Creates a new Fabricator instance
|
|
817
|
+
* @param seed - Optional seed (string or number) for deterministic data generation
|
|
818
|
+
*/
|
|
819
|
+
constructor(t) {
|
|
820
|
+
if (this.random = (e) => this._random(e), this.generate = {
|
|
821
|
+
/**
|
|
822
|
+
* Generates a person with firstName, middleName, lastName, and fullName
|
|
823
|
+
* Uses probabilistic logic for variations:
|
|
824
|
+
* - firstName: 0.021 (rare) chance to be a lastName instead
|
|
825
|
+
* - middleName: 0.146 (uncommon) chance to be missing, 0.021 (rare) chance to be lastName, 0.00307 (epic) chance to have two
|
|
826
|
+
* - lastName: 0.021 (rare) chance to have two hyphenated
|
|
827
|
+
* - fullName: always "first last", 0.146 (uncommon) chance to include middle
|
|
828
|
+
*
|
|
829
|
+
* @param id - Optional UUID to seed the subfaker. If not provided, generates a new UUID
|
|
830
|
+
* @returns Person object with firstName, middleName, lastName, and fullName
|
|
831
|
+
*/
|
|
832
|
+
person: (e) => {
|
|
833
|
+
const n = e ?? this._faker.string.uuid(), i = new M({ locale: U }), s = S(n);
|
|
834
|
+
i.seed(s);
|
|
835
|
+
const a = i.number.float() < 0.021 ? i.person.lastName() : i.person.firstName();
|
|
836
|
+
let u;
|
|
837
|
+
const E = i.number.float();
|
|
838
|
+
E < 307e-5 ? u = `${i.person.firstName()} ${i.person.firstName()}` : E < 307e-5 + 0.021 ? u = i.person.lastName() : E < 307e-5 + 0.021 + 0.146 ? u = void 0 : u = i.person.firstName();
|
|
839
|
+
const h = i.number.float() < 0.021 ? `${i.person.lastName()}-${i.person.lastName()}` : i.person.lastName(), c = i.number.float() < 0.146 && u ? `${a} ${u} ${h}` : `${a} ${h}`;
|
|
840
|
+
return {
|
|
841
|
+
id: n,
|
|
842
|
+
firstName: a,
|
|
843
|
+
middleName: u,
|
|
844
|
+
lastName: h,
|
|
845
|
+
fullName: c
|
|
846
|
+
};
|
|
847
|
+
}
|
|
848
|
+
}, this._faker = new M({ locale: U }), this._random = Gt(t), t !== void 0) {
|
|
849
|
+
const e = S(String(t));
|
|
850
|
+
this._faker.seed(e);
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
/**
|
|
854
|
+
* Gets the internal faker instance
|
|
855
|
+
*/
|
|
856
|
+
get faker() {
|
|
857
|
+
return this._faker;
|
|
858
|
+
}
|
|
859
|
+
/**
|
|
860
|
+
* Generates a random word combination using one of three patterns:
|
|
861
|
+
* - adjective noun
|
|
862
|
+
* - adjective verb
|
|
863
|
+
* - noun verb
|
|
864
|
+
* @returns A string with two words following one of the patterns
|
|
865
|
+
*/
|
|
866
|
+
words() {
|
|
867
|
+
const t = [
|
|
868
|
+
() => `${this._faker.word.adjective()} ${this._faker.word.noun()}`,
|
|
869
|
+
// adjective noun
|
|
870
|
+
() => `${this._faker.word.adjective()} ${this._faker.word.verb()}`,
|
|
871
|
+
// adjective verb
|
|
872
|
+
() => `${this._faker.word.noun()} ${this._faker.word.verb()}`
|
|
873
|
+
// noun verb
|
|
874
|
+
], e = t[this._faker.number.int({ min: 0, max: t.length - 1 })];
|
|
875
|
+
return e();
|
|
876
|
+
}
|
|
877
|
+
// Proxy all faker fields for direct access
|
|
878
|
+
get airline() {
|
|
879
|
+
return this._faker.airline;
|
|
880
|
+
}
|
|
881
|
+
get animal() {
|
|
882
|
+
return this._faker.animal;
|
|
883
|
+
}
|
|
884
|
+
get color() {
|
|
885
|
+
return this._faker.color;
|
|
886
|
+
}
|
|
887
|
+
get commerce() {
|
|
888
|
+
return this._faker.commerce;
|
|
889
|
+
}
|
|
890
|
+
get company() {
|
|
891
|
+
return this._faker.company;
|
|
892
|
+
}
|
|
893
|
+
get database() {
|
|
894
|
+
return this._faker.database;
|
|
895
|
+
}
|
|
896
|
+
get datatype() {
|
|
897
|
+
return this._faker.datatype;
|
|
898
|
+
}
|
|
899
|
+
get date() {
|
|
900
|
+
return this._faker.date;
|
|
901
|
+
}
|
|
902
|
+
get finance() {
|
|
903
|
+
return this._faker.finance;
|
|
904
|
+
}
|
|
905
|
+
get git() {
|
|
906
|
+
return this._faker.git;
|
|
907
|
+
}
|
|
908
|
+
get hacker() {
|
|
909
|
+
return this._faker.hacker;
|
|
910
|
+
}
|
|
911
|
+
get helpers() {
|
|
912
|
+
return this._faker.helpers;
|
|
913
|
+
}
|
|
914
|
+
get image() {
|
|
915
|
+
return this._faker.image;
|
|
916
|
+
}
|
|
917
|
+
get internet() {
|
|
918
|
+
return this._faker.internet;
|
|
919
|
+
}
|
|
920
|
+
get location() {
|
|
921
|
+
return this._faker.location;
|
|
922
|
+
}
|
|
923
|
+
get lorem() {
|
|
924
|
+
return this._faker.lorem;
|
|
925
|
+
}
|
|
926
|
+
get music() {
|
|
927
|
+
return this._faker.music;
|
|
928
|
+
}
|
|
929
|
+
get number() {
|
|
930
|
+
return this._faker.number;
|
|
931
|
+
}
|
|
932
|
+
get person() {
|
|
933
|
+
return this._faker.person;
|
|
934
|
+
}
|
|
935
|
+
get phone() {
|
|
936
|
+
return this._faker.phone;
|
|
937
|
+
}
|
|
938
|
+
get science() {
|
|
939
|
+
return this._faker.science;
|
|
940
|
+
}
|
|
941
|
+
get string() {
|
|
942
|
+
return this._faker.string;
|
|
943
|
+
}
|
|
944
|
+
get system() {
|
|
945
|
+
return this._faker.system;
|
|
946
|
+
}
|
|
947
|
+
get vehicle() {
|
|
948
|
+
return this._faker.vehicle;
|
|
949
|
+
}
|
|
950
|
+
get word() {
|
|
951
|
+
return this._faker.word;
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
const yt = {
|
|
955
|
+
UNCOMMON: 0.146,
|
|
956
|
+
RARE: 0.021,
|
|
957
|
+
EPIC: 307e-5,
|
|
958
|
+
LEGENDARY: 441e-6
|
|
959
|
+
};
|
|
960
|
+
function Pt(r) {
|
|
961
|
+
return new Ct(r);
|
|
962
|
+
}
|
|
963
|
+
export {
|
|
964
|
+
yt as CHANCE,
|
|
965
|
+
wt as DEFAULT_MAX,
|
|
966
|
+
Ut as DEFAULT_MIN,
|
|
967
|
+
Ct as Fabricator,
|
|
968
|
+
Pt as fabricator,
|
|
969
|
+
w as numericSeed,
|
|
970
|
+
S as numericSeedArray,
|
|
971
|
+
Gt as random,
|
|
972
|
+
V as uuidToBytes,
|
|
973
|
+
P as uuidToNumber
|
|
974
|
+
};
|
package/dist/random.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export interface RandomOptions {
|
|
2
|
+
min?: number;
|
|
3
|
+
max?: number;
|
|
4
|
+
mean?: number;
|
|
5
|
+
stddev?: number;
|
|
6
|
+
integer?: boolean;
|
|
7
|
+
start?: number;
|
|
8
|
+
seed?: string;
|
|
9
|
+
precision?: number;
|
|
10
|
+
currency?: boolean;
|
|
11
|
+
}
|
|
12
|
+
export type RandomFunction = {
|
|
13
|
+
(options?: RandomOptions): number;
|
|
14
|
+
};
|
|
15
|
+
export declare const DEFAULT_MIN = 0;
|
|
16
|
+
export declare const DEFAULT_MAX = 1;
|
|
17
|
+
/**
|
|
18
|
+
* Creates a random number generator with optional seeding
|
|
19
|
+
*
|
|
20
|
+
* @param defaultSeed - Seed (string, number, or undefined) for the default RNG
|
|
21
|
+
* @returns A function that generates random numbers based on provided options
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* const rng = random("default-seed");
|
|
25
|
+
*
|
|
26
|
+
* // Generate a random float between 0 and 1
|
|
27
|
+
* const basic = rng();
|
|
28
|
+
*
|
|
29
|
+
* // Generate an integer between 1 and 10
|
|
30
|
+
* const integer = rng({ min: 1, max: 10, integer: true });
|
|
31
|
+
*
|
|
32
|
+
* // Generate from normal distribution
|
|
33
|
+
* const normal = rng({ mean: 50, stddev: 10 });
|
|
34
|
+
*
|
|
35
|
+
* // Use consistent seeding
|
|
36
|
+
* const seeded = rng({ seed: "my-seed" });
|
|
37
|
+
*/
|
|
38
|
+
export declare function random(defaultSeed?: string | number): RandomFunction;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a string or number seed into a deterministic numeric value.
|
|
3
|
+
* If the input is a number, returns it directly.
|
|
4
|
+
* If the string is a UUID, uses uuidToNumber for conversion.
|
|
5
|
+
* @param seed - String or number to convert into a numeric seed
|
|
6
|
+
* @returns Absolute numeric value derived from the input
|
|
7
|
+
*/
|
|
8
|
+
export default function numericSeed(seed: string | number): number;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a string into an array of numeric values.
|
|
3
|
+
* If the string is a UUID, attempts to convert using uuidToBytes.
|
|
4
|
+
* Falls back to uuidToNumber if uuidToBytes fails.
|
|
5
|
+
* Finally falls back to numericSeed for non-UUID strings.
|
|
6
|
+
* @param seed - String to convert into numeric array
|
|
7
|
+
* @returns Array of numeric values derived from the input
|
|
8
|
+
*/
|
|
9
|
+
export default function numericSeedArray(seed: string): number[];
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a UUID string into a numeric value by using the last 6 bytes
|
|
3
|
+
* to ensure better uniqueness between different UUIDs
|
|
4
|
+
* @param uuid - UUID string to convert
|
|
5
|
+
* @returns Numeric value derived from the UUID
|
|
6
|
+
*/
|
|
7
|
+
export default function uuidToBytes(uuid: string): number[];
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a UUID string into a numeric value by using the last 6 bytes
|
|
3
|
+
* to ensure better uniqueness between different UUIDs
|
|
4
|
+
* @param uuid - UUID string to convert
|
|
5
|
+
* @returns Numeric value derived from the UUID
|
|
6
|
+
*/
|
|
7
|
+
export default function uuidToNumber(uuid: string): number;
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jaypie/fabricator",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts"
|
|
10
|
+
},
|
|
11
|
+
"./constants": {
|
|
12
|
+
"import": "./dist/constants.js",
|
|
13
|
+
"types": "./dist/constants.d.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"main": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "vite build",
|
|
23
|
+
"clean": "rimraf coverage dist node_modules package-lock.json",
|
|
24
|
+
"dev": "vite",
|
|
25
|
+
"test": "vitest run",
|
|
26
|
+
"test:watch": "vitest watch",
|
|
27
|
+
"typecheck": "tsc --noEmit"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@faker-js/faker": "^9.3.0",
|
|
31
|
+
"@jaypie/errors": "^1.1.7",
|
|
32
|
+
"random": "^5.4.1",
|
|
33
|
+
"uuid": "^13.0.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/node": "^24.2.1",
|
|
37
|
+
"@types/uuid": "^10.0.0"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
}
|
|
42
|
+
}
|