@geekmidas/testkit 10.0.0-alpha.40 → 10.0.0-alpha.42
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/faker.cjs +37 -0
- package/dist/faker.cjs.map +1 -1
- package/dist/faker.d.cts +24 -0
- package/dist/faker.d.cts.map +1 -1
- package/dist/faker.d.mts +24 -0
- package/dist/faker.d.mts.map +1 -1
- package/dist/faker.mjs +37 -1
- package/dist/faker.mjs.map +1 -1
- package/package.json +7 -7
package/dist/faker.cjs
CHANGED
|
@@ -192,6 +192,41 @@ function resetAllSequences() {
|
|
|
192
192
|
function price() {
|
|
193
193
|
return +faker.commerce.price();
|
|
194
194
|
}
|
|
195
|
+
/**
|
|
196
|
+
* A birthdate for someone of the given age today: exactly `min` years old, or
|
|
197
|
+
* anywhere from `min` to `max` inclusive.
|
|
198
|
+
*
|
|
199
|
+
* Through faker's own `date.birthdate`, so seeding faker makes it repeatable.
|
|
200
|
+
*
|
|
201
|
+
* @param min - The youngest age, in whole years
|
|
202
|
+
* @param max - The oldest age, in whole years (default: `min`)
|
|
203
|
+
* @returns The birthdate
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* ```typescript
|
|
207
|
+
* faker.age(18); // someone who is 18 today
|
|
208
|
+
* faker.age(18, 24); // someone aged 18 to 24
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
function age(min, max = min) {
|
|
212
|
+
if (min < 0 || max < min) throw new AgeRangeInvalid(min, max);
|
|
213
|
+
return _faker_js_faker.faker.date.birthdate({
|
|
214
|
+
mode: "age",
|
|
215
|
+
min,
|
|
216
|
+
max
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
/** An age range no one can be: a negative age, or an oldest below a youngest. */
|
|
220
|
+
var AgeRangeInvalid = class extends Error {
|
|
221
|
+
min;
|
|
222
|
+
max;
|
|
223
|
+
constructor(min, max) {
|
|
224
|
+
super(`faker.age(${min}, ${max}) asks for an age no one can be. Pass the youngest age first and the oldest second, neither negative: faker.age(18) or faker.age(18, 24).`);
|
|
225
|
+
this.min = min;
|
|
226
|
+
this.max = max;
|
|
227
|
+
this.name = "AgeRangeInvalid";
|
|
228
|
+
}
|
|
229
|
+
};
|
|
195
230
|
function coordinateInRadius(center, radius) {
|
|
196
231
|
const d = radius / 6378137;
|
|
197
232
|
const theta = 2 * Math.PI * Math.random();
|
|
@@ -260,12 +295,14 @@ const faker = Object.freeze(Object.assign({}, _faker_js_faker.faker, {
|
|
|
260
295
|
resetSequence,
|
|
261
296
|
resetAllSequences,
|
|
262
297
|
price,
|
|
298
|
+
age,
|
|
263
299
|
coordinates: {
|
|
264
300
|
within: coordinateInRadius,
|
|
265
301
|
outside: coordinateOutsideRadius
|
|
266
302
|
}
|
|
267
303
|
}));
|
|
268
304
|
//#endregion
|
|
305
|
+
exports.AgeRangeInvalid = AgeRangeInvalid;
|
|
269
306
|
exports.coordinateInRadius = coordinateInRadius;
|
|
270
307
|
exports.faker = faker;
|
|
271
308
|
exports.identifier = identifier;
|
package/dist/faker.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"faker.cjs","names":["baseFaker"],"sources":["../src/faker.ts"],"sourcesContent":["import { faker as baseFaker } from '@faker-js/faker';\n\n// NOTE: This is a simple way to extend `faker` with additional methods\n\n/**\n * Atomic counter implementation for thread-safe sequence generation.\n * Provides a clean abstraction for generating sequential numbers in tests.\n * While JavaScript is single-threaded, this class makes the intent explicit.\n *\n * @example\n * ```typescript\n * const counter = new AtomicCounter(100);\n * console.log(counter.increment()); // 101\n * console.log(counter.increment()); // 102\n * console.log(counter.get()); // 102\n * counter.reset(200);\n * console.log(counter.increment()); // 201\n * ```\n */\nclass AtomicCounter {\n\t/**\n\t * The current counter value.\n\t * @private\n\t */\n\tprivate value: number;\n\n\t/**\n\t * Creates a new atomic counter.\n\t * @param initialValue - The starting value (default: 0)\n\t */\n\tconstructor(initialValue = 0) {\n\t\tthis.value = initialValue;\n\t}\n\n\t/**\n\t * Increments the counter and returns the new value.\n\t * @returns The incremented value\n\t */\n\tincrement(): number {\n\t\t// In Node.js, JavaScript is single-threaded within the event loop,\n\t\t// so this operation is already atomic. However, this class provides\n\t\t// a cleaner abstraction and makes the intent explicit.\n\t\treturn ++this.value;\n\t}\n\n\t/**\n\t * Gets the current counter value without incrementing.\n\t * @returns The current value\n\t */\n\tget(): number {\n\t\treturn this.value;\n\t}\n\n\t/**\n\t * Resets the counter to a specific value.\n\t * @param value - The new value (default: 0)\n\t */\n\treset(value = 0): void {\n\t\tthis.value = value;\n\t}\n}\n\n/**\n * Generates random timestamp fields for database records.\n * Creates a createdAt date in the past and an updatedAt date between creation and now.\n * Milliseconds are set to 0 for cleaner database storage.\n *\n * @returns Object with createdAt and updatedAt Date fields\n *\n * @example\n * ```typescript\n * const { createdAt, updatedAt } = timestamps();\n * console.log(createdAt); // 2023-05-15T10:30:00.000Z\n * console.log(updatedAt); // 2023-11-20T14:45:00.000Z\n *\n * // Use in factory\n * const user = {\n * name: 'John Doe',\n * ...timestamps()\n * };\n * ```\n */\nexport function timestamps(): Timestamps {\n\tconst createdAt = faker.date.past();\n\tconst updatedAt = faker.date.between({\n\t\tfrom: createdAt,\n\t\tto: new Date(),\n\t});\n\n\tcreatedAt.setMilliseconds(0);\n\tupdatedAt.setMilliseconds(0);\n\n\treturn { createdAt, updatedAt };\n}\n\n/**\n * Generates a reverse domain name identifier.\n * Useful for creating unique identifiers that follow domain naming conventions.\n *\n * @param suffix - Optional suffix to append to the identifier\n * @returns A reverse domain name string (e.g., \"com.example.feature123\")\n *\n * @example\n * ```typescript\n * console.log(identifier()); // \"com.example.widget1\"\n * console.log(identifier('user')); // \"org.acme.user\"\n * console.log(identifier('api')); // \"net.demo.api\"\n * ```\n */\nexport function identifier(suffix?: string): string {\n\treturn [\n\t\tfaker.internet.domainSuffix(),\n\t\tfaker.internet.domainWord(),\n\t\tsuffix ? suffix : faker.internet.domainWord() + sequence('identifier'),\n\t].join('.');\n}\n\n/**\n * Storage for named sequence counters.\n * Each sequence maintains its own independent counter.\n * @private\n */\nconst sequences = new Map<string, AtomicCounter>();\n\n/**\n * Generates sequential numbers for a named sequence.\n * Useful for creating unique IDs or numbered test data.\n * Each named sequence maintains its own counter.\n *\n * @param name - The sequence name (default: 'default')\n * @returns The next number in the sequence\n *\n * @example\n * ```typescript\n * console.log(sequence()); // 1\n * console.log(sequence()); // 2\n * console.log(sequence('user')); // 1\n * console.log(sequence('user')); // 2\n * console.log(sequence()); // 3\n *\n * // Use in factories\n * const email = `user${sequence('email')}@example.com`;\n * ```\n */\nexport function sequence(name = 'default'): number {\n\tif (!sequences.has(name)) {\n\t\tsequences.set(name, new AtomicCounter());\n\t}\n\n\tconst counter = sequences.get(name) as AtomicCounter;\n\treturn counter.increment();\n}\n\n/**\n * Resets a named sequence counter to a specific value.\n * Useful for resetting sequences between test suites.\n *\n * @param name - The sequence name to reset (default: 'default')\n * @param value - The new starting value (default: 0)\n *\n * @example\n * ```typescript\n * sequence('user'); // 1\n * sequence('user'); // 2\n * resetSequence('user');\n * sequence('user'); // 1\n *\n * resetSequence('order', 1000);\n * sequence('order'); // 1001\n * ```\n */\nexport function resetSequence(name = 'default', value = 0): void {\n\tif (sequences.has(name)) {\n\t\tconst counter = sequences.get(name) as AtomicCounter;\n\t\tcounter.reset(value);\n\t} else {\n\t\tsequences.set(name, new AtomicCounter(value));\n\t}\n}\n\n/**\n * Resets all sequence counters.\n * Useful for cleaning up between test suites to ensure predictable sequences.\n *\n * @example\n * ```typescript\n * // In test setup\n * beforeEach(() => {\n * resetAllSequences();\n * });\n *\n * it('starts sequences from 1', () => {\n * expect(sequence()).toBe(1);\n * expect(sequence('user')).toBe(1);\n * });\n * ```\n */\nexport function resetAllSequences(): void {\n\tsequences.clear();\n}\n\n/**\n * Generates a random price as a number.\n * Converts faker's string price to a numeric value.\n *\n * @returns A random price number\n *\n * @example\n * ```typescript\n * const productPrice = price(); // 29.99\n * const total = price() * quantity; // Numeric calculation\n * ```\n */\nfunction price(): number {\n\treturn +faker.commerce.price();\n}\n\ntype Coordinate = {\n\tlat: number;\n\tlng: number;\n};\n\nexport function coordinateInRadius(\n\tcenter: Coordinate,\n\tradius: number,\n): Coordinate {\n\t// Earth's radius in meters\n\tconst earth = 6378137;\n\t// Convert radius from meters to degrees\n\tconst d = radius / earth;\n\n\t// Random bearing and distance\n\tconst theta = 2 * Math.PI * Math.random();\n\tconst r = d * Math.sqrt(Math.random());\n\n\tconst lat1 = (center.lat * Math.PI) / 180;\n\tconst lng1 = (center.lng * Math.PI) / 180;\n\n\tconst lat2 = Math.asin(\n\t\tMath.sin(lat1) * Math.cos(r) +\n\t\t\tMath.cos(lat1) * Math.sin(r) * Math.cos(theta),\n\t);\n\tconst lng2 =\n\t\tlng1 +\n\t\tMath.atan2(\n\t\t\tMath.sin(theta) * Math.sin(r) * Math.cos(lat1),\n\t\t\tMath.cos(r) - Math.sin(lat1) * Math.sin(lat2),\n\t\t);\n\n\treturn {\n\t\tlat: (lat2 * 180) / Math.PI,\n\t\tlng: (lng2 * 180) / Math.PI,\n\t};\n}\n\nfunction coordinateOutsideRadius(\n\tcenter: Coordinate,\n\tminRadiusMeters: number,\n\tmaxRadiusMeters: number,\n): Coordinate {\n\t// Earth's radius in meters\n\tconst earth = 6378137;\n\n\t// Convert radii from meters to radians\n\tconst minD = minRadiusMeters / earth;\n\tconst maxD = maxRadiusMeters / earth;\n\n\t// Random bearing\n\tconst theta = 2 * Math.PI * Math.random();\n\n\t// Random distance in annular ring (uniform distribution by area)\n\t// For uniform distribution in annulus: r = sqrt(r_min² + (r_max² - r_min²) * random)\n\tconst r = Math.sqrt(\n\t\tminD * minD + (maxD * maxD - minD * minD) * Math.random(),\n\t);\n\n\tconst lat1 = (center.lat * Math.PI) / 180;\n\tconst lng1 = (center.lng * Math.PI) / 180;\n\n\tconst lat2 = Math.asin(\n\t\tMath.sin(lat1) * Math.cos(r) +\n\t\t\tMath.cos(lat1) * Math.sin(r) * Math.cos(theta),\n\t);\n\tconst lng2 =\n\t\tlng1 +\n\t\tMath.atan2(\n\t\t\tMath.sin(theta) * Math.sin(r) * Math.cos(lat1),\n\t\t\tMath.cos(r) - Math.sin(lat1) * Math.sin(lat2),\n\t\t);\n\n\t// Normalize longitude to [-180, 180]\n\tconst normalizedLng = (((lng2 * 180) / Math.PI + 540) % 360) - 180;\n\n\treturn {\n\t\tlat: (lat2 * 180) / Math.PI,\n\t\tlng: normalizedLng,\n\t};\n}\n\n/**\n * Enhanced faker instance with additional utility methods for testing.\n * Extends @faker-js/faker with custom methods for common test data generation patterns.\n *\n * @example\n * ```typescript\n * import { faker } from '@geekmidas/testkit';\n *\n * // Use standard faker methods\n * const name = faker.person.fullName();\n * const email = faker.internet.email();\n *\n * // Use custom extensions\n * const { createdAt, updatedAt } = faker.timestamps();\n * const id = faker.identifier('user');\n * const orderNumber = faker.sequence('order');\n * const productPrice = faker.price();\n * ```\n */\n/**\n * `faker.internet`, with every address lowercase.\n *\n * An auth server stores addresses lowercased — Better Auth does — so a test\n * that signs in as faker's `Ada.Lovelace@…` and reads back `ada.lovelace@…`\n * fails only when faker happened to capitalise: a flake, not a bug. The rest\n * of the module is faker's own, through the prototype.\n */\nconst internet: typeof baseFaker.internet = Object.assign(\n\tObject.create(baseFaker.internet),\n\t{\n\t\temail: (...args: Parameters<typeof baseFaker.internet.email>) =>\n\t\t\tbaseFaker.internet.email(...args).toLowerCase(),\n\t\texampleEmail: (\n\t\t\t...args: Parameters<typeof baseFaker.internet.exampleEmail>\n\t\t) => baseFaker.internet.exampleEmail(...args).toLowerCase(),\n\t},\n);\n\nexport const faker = Object.freeze(\n\tObject.assign({}, baseFaker, {\n\t\tinternet,\n\t\t// A prototype method, so the spread above leaves it behind. The modules\n\t\t// copied across still draw from `baseFaker`, so seeding it seeds them.\n\t\tseed: (seed?: number) => baseFaker.seed(seed),\n\t\ttimestamps,\n\t\tidentifier,\n\t\tsequence,\n\t\tresetSequence,\n\t\tresetAllSequences,\n\t\tprice,\n\t\tcoordinates: {\n\t\t\twithin: coordinateInRadius,\n\t\t\toutside: coordinateOutsideRadius,\n\t\t},\n\t}),\n);\n\n/**\n * Type definition for timestamp fields.\n * Used by the timestamps() function to generate date fields.\n */\nexport type Timestamps = {\n\t/** The creation date */\n\tcreatedAt: Date;\n\t/** The last update date */\n\tupdatedAt: Date;\n};\n\n/**\n * Type definition for the enhanced faker factory.\n * Includes all standard faker methods plus custom extensions.\n */\nexport type FakerFactory = typeof faker;\n"],"mappings":";;;;;;;;;;;;;;;;;;AAmBA,IAAM,gBAAN,MAAoB;;;;;CAKnB;;;;;CAMA,YAAY,eAAe,GAAG;EAC7B,KAAK,QAAQ;CACd;;;;;CAMA,YAAoB;EAInB,OAAO,EAAE,KAAK;CACf;;;;;CAMA,MAAc;EACb,OAAO,KAAK;CACb;;;;;CAMA,MAAM,QAAQ,GAAS;EACtB,KAAK,QAAQ;CACd;AACD;;;;;;;;;;;;;;;;;;;;;AAsBA,SAAgB,aAAyB;CACxC,MAAM,YAAY,MAAM,KAAK,KAAK;CAClC,MAAM,YAAY,MAAM,KAAK,QAAQ;EACpC,MAAM;EACN,oBAAI,IAAI,KAAK;CACd,CAAC;CAED,UAAU,gBAAgB,CAAC;CAC3B,UAAU,gBAAgB,CAAC;CAE3B,OAAO;EAAE;EAAW;CAAU;AAC/B;;;;;;;;;;;;;;;AAgBA,SAAgB,WAAW,QAAyB;CACnD,OAAO;EACN,MAAM,SAAS,aAAa;EAC5B,MAAM,SAAS,WAAW;EAC1B,SAAS,SAAS,MAAM,SAAS,WAAW,IAAI,SAAS,YAAY;CACtE,CAAC,CAAC,KAAK,GAAG;AACX;;;;;;AAOA,MAAM,4BAAY,IAAI,IAA2B;;;;;;;;;;;;;;;;;;;;;AAsBjD,SAAgB,SAAS,OAAO,WAAmB;CAClD,IAAI,CAAC,UAAU,IAAI,IAAI,GACtB,UAAU,IAAI,MAAM,IAAI,cAAc,CAAC;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AM,IAAI,KAAK,KACd,OAAO,QAAQ,OAAO,OAAO,OAAO,QAAQ,KAAK,OAAO,CACzD;CAEA,MAAM,OAAQ,OAAO,MAAM,KAAK,KAAM;CACtC,MAAM,OAAQ,OAAO,MAAM,KAAK,KAAM;CAEtC,MAAM,OAAO,KAAK,KACjB,KAAK,IAAI,IAAI,IAAI,KAAK,IAAI,CAAC,IAC1B,KAAK,IAAI,IAAI,IAAI,KAAK,IAAI,CAAC,IAAI,KAAK,IAAI,KAAK,CAC/C;CASA,MAAM,kBAPL,OACA,KAAK,MACJ,KAAK,IAAI,KAAK,IAAI,KAAK,IAAI,CAAC,IAAI,KAAK,IAAI,IAAI,GAC7C,KAAK,IAAI,CAAC,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,IAAI,IAAI,CAC7C,KAG+B,MAAO,KAAK,KAAK,OAAO,MAAO;CAE/D,OAAO;EACN,KAAM,OAAO,MAAO,KAAK;EACzB,KAAK;CACN;AACD;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,MAAM,WAAsC,OAAO,OAClD,OAAO,OAAOA,gBAAAA,MAAU,QAAQ,GAChC;CACC,QAAQ,GAAG,SACVA,gBAAAA,MAAU,SAAS,MAAM,GAAG,IAAI,CAAC,CAAC,YAAY;CAC/C,eACC,GAAG,SACCA,gBAAAA,MAAU,SAAS,aAAa,GAAG,IAAI,CAAC,CAAC,YAAY;AAC3D,CACD;AAEA,MAAa,QAAQ,OAAO,OAC3B,OAAO,OAAO,CAAC,GAAGA,gBAAAA,OAAW;CAC5B;CAGA,OAAO,SAAkBA,gBAAAA,MAAU,KAAK,IAAI;CAC5C;CACA;CACA;CACA;CACA;CACA;CACA,aAAa;EACZ,QAAQ;EACR,SAAS;CACV;AACD,CAAC,CACF"}
|
|
1
|
+
{"version":3,"file":"faker.cjs","names":["baseFaker"],"sources":["../src/faker.ts"],"sourcesContent":["import { faker as baseFaker } from '@faker-js/faker';\n\n// NOTE: This is a simple way to extend `faker` with additional methods\n\n/**\n * Atomic counter implementation for thread-safe sequence generation.\n * Provides a clean abstraction for generating sequential numbers in tests.\n * While JavaScript is single-threaded, this class makes the intent explicit.\n *\n * @example\n * ```typescript\n * const counter = new AtomicCounter(100);\n * console.log(counter.increment()); // 101\n * console.log(counter.increment()); // 102\n * console.log(counter.get()); // 102\n * counter.reset(200);\n * console.log(counter.increment()); // 201\n * ```\n */\nclass AtomicCounter {\n\t/**\n\t * The current counter value.\n\t * @private\n\t */\n\tprivate value: number;\n\n\t/**\n\t * Creates a new atomic counter.\n\t * @param initialValue - The starting value (default: 0)\n\t */\n\tconstructor(initialValue = 0) {\n\t\tthis.value = initialValue;\n\t}\n\n\t/**\n\t * Increments the counter and returns the new value.\n\t * @returns The incremented value\n\t */\n\tincrement(): number {\n\t\t// In Node.js, JavaScript is single-threaded within the event loop,\n\t\t// so this operation is already atomic. However, this class provides\n\t\t// a cleaner abstraction and makes the intent explicit.\n\t\treturn ++this.value;\n\t}\n\n\t/**\n\t * Gets the current counter value without incrementing.\n\t * @returns The current value\n\t */\n\tget(): number {\n\t\treturn this.value;\n\t}\n\n\t/**\n\t * Resets the counter to a specific value.\n\t * @param value - The new value (default: 0)\n\t */\n\treset(value = 0): void {\n\t\tthis.value = value;\n\t}\n}\n\n/**\n * Generates random timestamp fields for database records.\n * Creates a createdAt date in the past and an updatedAt date between creation and now.\n * Milliseconds are set to 0 for cleaner database storage.\n *\n * @returns Object with createdAt and updatedAt Date fields\n *\n * @example\n * ```typescript\n * const { createdAt, updatedAt } = timestamps();\n * console.log(createdAt); // 2023-05-15T10:30:00.000Z\n * console.log(updatedAt); // 2023-11-20T14:45:00.000Z\n *\n * // Use in factory\n * const user = {\n * name: 'John Doe',\n * ...timestamps()\n * };\n * ```\n */\nexport function timestamps(): Timestamps {\n\tconst createdAt = faker.date.past();\n\tconst updatedAt = faker.date.between({\n\t\tfrom: createdAt,\n\t\tto: new Date(),\n\t});\n\n\tcreatedAt.setMilliseconds(0);\n\tupdatedAt.setMilliseconds(0);\n\n\treturn { createdAt, updatedAt };\n}\n\n/**\n * Generates a reverse domain name identifier.\n * Useful for creating unique identifiers that follow domain naming conventions.\n *\n * @param suffix - Optional suffix to append to the identifier\n * @returns A reverse domain name string (e.g., \"com.example.feature123\")\n *\n * @example\n * ```typescript\n * console.log(identifier()); // \"com.example.widget1\"\n * console.log(identifier('user')); // \"org.acme.user\"\n * console.log(identifier('api')); // \"net.demo.api\"\n * ```\n */\nexport function identifier(suffix?: string): string {\n\treturn [\n\t\tfaker.internet.domainSuffix(),\n\t\tfaker.internet.domainWord(),\n\t\tsuffix ? suffix : faker.internet.domainWord() + sequence('identifier'),\n\t].join('.');\n}\n\n/**\n * Storage for named sequence counters.\n * Each sequence maintains its own independent counter.\n * @private\n */\nconst sequences = new Map<string, AtomicCounter>();\n\n/**\n * Generates sequential numbers for a named sequence.\n * Useful for creating unique IDs or numbered test data.\n * Each named sequence maintains its own counter.\n *\n * @param name - The sequence name (default: 'default')\n * @returns The next number in the sequence\n *\n * @example\n * ```typescript\n * console.log(sequence()); // 1\n * console.log(sequence()); // 2\n * console.log(sequence('user')); // 1\n * console.log(sequence('user')); // 2\n * console.log(sequence()); // 3\n *\n * // Use in factories\n * const email = `user${sequence('email')}@example.com`;\n * ```\n */\nexport function sequence(name = 'default'): number {\n\tif (!sequences.has(name)) {\n\t\tsequences.set(name, new AtomicCounter());\n\t}\n\n\tconst counter = sequences.get(name) as AtomicCounter;\n\treturn counter.increment();\n}\n\n/**\n * Resets a named sequence counter to a specific value.\n * Useful for resetting sequences between test suites.\n *\n * @param name - The sequence name to reset (default: 'default')\n * @param value - The new starting value (default: 0)\n *\n * @example\n * ```typescript\n * sequence('user'); // 1\n * sequence('user'); // 2\n * resetSequence('user');\n * sequence('user'); // 1\n *\n * resetSequence('order', 1000);\n * sequence('order'); // 1001\n * ```\n */\nexport function resetSequence(name = 'default', value = 0): void {\n\tif (sequences.has(name)) {\n\t\tconst counter = sequences.get(name) as AtomicCounter;\n\t\tcounter.reset(value);\n\t} else {\n\t\tsequences.set(name, new AtomicCounter(value));\n\t}\n}\n\n/**\n * Resets all sequence counters.\n * Useful for cleaning up between test suites to ensure predictable sequences.\n *\n * @example\n * ```typescript\n * // In test setup\n * beforeEach(() => {\n * resetAllSequences();\n * });\n *\n * it('starts sequences from 1', () => {\n * expect(sequence()).toBe(1);\n * expect(sequence('user')).toBe(1);\n * });\n * ```\n */\nexport function resetAllSequences(): void {\n\tsequences.clear();\n}\n\n/**\n * Generates a random price as a number.\n * Converts faker's string price to a numeric value.\n *\n * @returns A random price number\n *\n * @example\n * ```typescript\n * const productPrice = price(); // 29.99\n * const total = price() * quantity; // Numeric calculation\n * ```\n */\nfunction price(): number {\n\treturn +faker.commerce.price();\n}\n\n/**\n * A birthdate for someone of the given age today: exactly `min` years old, or\n * anywhere from `min` to `max` inclusive.\n *\n * Through faker's own `date.birthdate`, so seeding faker makes it repeatable.\n *\n * @param min - The youngest age, in whole years\n * @param max - The oldest age, in whole years (default: `min`)\n * @returns The birthdate\n *\n * @example\n * ```typescript\n * faker.age(18); // someone who is 18 today\n * faker.age(18, 24); // someone aged 18 to 24\n * ```\n */\nfunction age(min: number, max: number = min): Date {\n\tif (min < 0 || max < min) throw new AgeRangeInvalid(min, max);\n\n\treturn baseFaker.date.birthdate({ mode: 'age', min, max });\n}\n\n/** An age range no one can be: a negative age, or an oldest below a youngest. */\nexport class AgeRangeInvalid extends Error {\n\tconstructor(\n\t\treadonly min: number,\n\t\treadonly max: number,\n\t) {\n\t\tsuper(\n\t\t\t`faker.age(${min}, ${max}) asks for an age no one can be. Pass the ` +\n\t\t\t\t'youngest age first and the oldest second, neither negative: ' +\n\t\t\t\t'faker.age(18) or faker.age(18, 24).',\n\t\t);\n\t\tthis.name = 'AgeRangeInvalid';\n\t}\n}\n\ntype Coordinate = {\n\tlat: number;\n\tlng: number;\n};\n\nexport function coordinateInRadius(\n\tcenter: Coordinate,\n\tradius: number,\n): Coordinate {\n\t// Earth's radius in meters\n\tconst earth = 6378137;\n\t// Convert radius from meters to degrees\n\tconst d = radius / earth;\n\n\t// Random bearing and distance\n\tconst theta = 2 * Math.PI * Math.random();\n\tconst r = d * Math.sqrt(Math.random());\n\n\tconst lat1 = (center.lat * Math.PI) / 180;\n\tconst lng1 = (center.lng * Math.PI) / 180;\n\n\tconst lat2 = Math.asin(\n\t\tMath.sin(lat1) * Math.cos(r) +\n\t\t\tMath.cos(lat1) * Math.sin(r) * Math.cos(theta),\n\t);\n\tconst lng2 =\n\t\tlng1 +\n\t\tMath.atan2(\n\t\t\tMath.sin(theta) * Math.sin(r) * Math.cos(lat1),\n\t\t\tMath.cos(r) - Math.sin(lat1) * Math.sin(lat2),\n\t\t);\n\n\treturn {\n\t\tlat: (lat2 * 180) / Math.PI,\n\t\tlng: (lng2 * 180) / Math.PI,\n\t};\n}\n\nfunction coordinateOutsideRadius(\n\tcenter: Coordinate,\n\tminRadiusMeters: number,\n\tmaxRadiusMeters: number,\n): Coordinate {\n\t// Earth's radius in meters\n\tconst earth = 6378137;\n\n\t// Convert radii from meters to radians\n\tconst minD = minRadiusMeters / earth;\n\tconst maxD = maxRadiusMeters / earth;\n\n\t// Random bearing\n\tconst theta = 2 * Math.PI * Math.random();\n\n\t// Random distance in annular ring (uniform distribution by area)\n\t// For uniform distribution in annulus: r = sqrt(r_min² + (r_max² - r_min²) * random)\n\tconst r = Math.sqrt(\n\t\tminD * minD + (maxD * maxD - minD * minD) * Math.random(),\n\t);\n\n\tconst lat1 = (center.lat * Math.PI) / 180;\n\tconst lng1 = (center.lng * Math.PI) / 180;\n\n\tconst lat2 = Math.asin(\n\t\tMath.sin(lat1) * Math.cos(r) +\n\t\t\tMath.cos(lat1) * Math.sin(r) * Math.cos(theta),\n\t);\n\tconst lng2 =\n\t\tlng1 +\n\t\tMath.atan2(\n\t\t\tMath.sin(theta) * Math.sin(r) * Math.cos(lat1),\n\t\t\tMath.cos(r) - Math.sin(lat1) * Math.sin(lat2),\n\t\t);\n\n\t// Normalize longitude to [-180, 180]\n\tconst normalizedLng = (((lng2 * 180) / Math.PI + 540) % 360) - 180;\n\n\treturn {\n\t\tlat: (lat2 * 180) / Math.PI,\n\t\tlng: normalizedLng,\n\t};\n}\n\n/**\n * Enhanced faker instance with additional utility methods for testing.\n * Extends @faker-js/faker with custom methods for common test data generation patterns.\n *\n * @example\n * ```typescript\n * import { faker } from '@geekmidas/testkit';\n *\n * // Use standard faker methods\n * const name = faker.person.fullName();\n * const email = faker.internet.email();\n *\n * // Use custom extensions\n * const { createdAt, updatedAt } = faker.timestamps();\n * const id = faker.identifier('user');\n * const orderNumber = faker.sequence('order');\n * const productPrice = faker.price();\n * ```\n */\n/**\n * `faker.internet`, with every address lowercase.\n *\n * An auth server stores addresses lowercased — Better Auth does — so a test\n * that signs in as faker's `Ada.Lovelace@…` and reads back `ada.lovelace@…`\n * fails only when faker happened to capitalise: a flake, not a bug. The rest\n * of the module is faker's own, through the prototype.\n */\nconst internet: typeof baseFaker.internet = Object.assign(\n\tObject.create(baseFaker.internet),\n\t{\n\t\temail: (...args: Parameters<typeof baseFaker.internet.email>) =>\n\t\t\tbaseFaker.internet.email(...args).toLowerCase(),\n\t\texampleEmail: (\n\t\t\t...args: Parameters<typeof baseFaker.internet.exampleEmail>\n\t\t) => baseFaker.internet.exampleEmail(...args).toLowerCase(),\n\t},\n);\n\nexport const faker = Object.freeze(\n\tObject.assign({}, baseFaker, {\n\t\tinternet,\n\t\t// A prototype method, so the spread above leaves it behind. The modules\n\t\t// copied across still draw from `baseFaker`, so seeding it seeds them.\n\t\tseed: (seed?: number) => baseFaker.seed(seed),\n\t\ttimestamps,\n\t\tidentifier,\n\t\tsequence,\n\t\tresetSequence,\n\t\tresetAllSequences,\n\t\tprice,\n\t\tage,\n\t\tcoordinates: {\n\t\t\twithin: coordinateInRadius,\n\t\t\toutside: coordinateOutsideRadius,\n\t\t},\n\t}),\n);\n\n/**\n * Type definition for timestamp fields.\n * Used by the timestamps() function to generate date fields.\n */\nexport type Timestamps = {\n\t/** The creation date */\n\tcreatedAt: Date;\n\t/** The last update date */\n\tupdatedAt: Date;\n};\n\n/**\n * Type definition for the enhanced faker factory.\n * Includes all standard faker methods plus custom extensions.\n */\nexport type FakerFactory = typeof faker;\n"],"mappings":";;;;;;;;;;;;;;;;;;AAmBA,IAAM,gBAAN,MAAoB;;;;;CAKnB;;;;;CAMA,YAAY,eAAe,GAAG;EAC7B,KAAK,QAAQ;CACd;;;;;CAMA,YAAoB;EAInB,OAAO,EAAE,KAAK;CACf;;;;;CAMA,MAAc;EACb,OAAO,KAAK;CACb;;;;;CAMA,MAAM,QAAQ,GAAS;EACtB,KAAK,QAAQ;CACd;AACD;;;;;;;;;;;;;;;;;;;;;AAsBA,SAAgB,aAAyB;CACxC,MAAM,YAAY,MAAM,KAAK,KAAK;CAClC,MAAM,YAAY,MAAM,KAAK,QAAQ;EACpC,MAAM;EACN,oBAAI,IAAI,KAAK;CACd,CAAC;CAED,UAAU,gBAAgB,CAAC;CAC3B,UAAU,gBAAgB,CAAC;CAE3B,OAAO;EAAE;EAAW;CAAU;AAC/B;;;;;;;;;;;;;;;AAgBA,SAAgB,WAAW,QAAyB;CACnD,OAAO;EACN,MAAM,SAAS,aAAa;EAC5B,MAAM,SAAS,WAAW;EAC1B,SAAS,SAAS,MAAM,SAAS,WAAW,IAAI,SAAS,YAAY;CACtE,CAAC,CAAC,KAAK,GAAG;AACX;;;;;;AAOA,MAAM,4BAAY,IAAI,IAA2B;;;;;;;;;;;;;;;;;;;;;AAsBjD,SAAgB,SAAS,OAAO,WAAmB;CAClD,IAAI,CAAC,UAAU,IAAI,IAAI,GACtB,UAAU,IAAI,MAAM,IAAI,cAAc,CAAC;CAIxC,OADgB,UAAU,IAAI,IACjB,CAAC,CAAC,UAAU;AAC1B;;;;;;;;;;;;;;;;;;;AAoBA,SAAgB,cAAc,OAAO,WAAW,QAAQ,GAAS;CAChE,IAAI,UAAU,IAAI,IAAI,GAErB,UAD0B,IAAI,IACxB,C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CACA;CACA;CACA,aAAa;EACZ,QAAQ;EACR,SAAS;CACV;AACD,CAAC,CACF"}
|
package/dist/faker.d.cts
CHANGED
|
@@ -106,6 +106,29 @@ export declare function resetAllSequences(): void;
|
|
|
106
106
|
* ```
|
|
107
107
|
*/
|
|
108
108
|
declare function price(): number;
|
|
109
|
+
/**
|
|
110
|
+
* A birthdate for someone of the given age today: exactly `min` years old, or
|
|
111
|
+
* anywhere from `min` to `max` inclusive.
|
|
112
|
+
*
|
|
113
|
+
* Through faker's own `date.birthdate`, so seeding faker makes it repeatable.
|
|
114
|
+
*
|
|
115
|
+
* @param min - The youngest age, in whole years
|
|
116
|
+
* @param max - The oldest age, in whole years (default: `min`)
|
|
117
|
+
* @returns The birthdate
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* faker.age(18); // someone who is 18 today
|
|
122
|
+
* faker.age(18, 24); // someone aged 18 to 24
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
declare function age(min: number, max?: number): Date;
|
|
126
|
+
/** An age range no one can be: a negative age, or an oldest below a youngest. */
|
|
127
|
+
export declare class AgeRangeInvalid extends Error {
|
|
128
|
+
readonly min: number;
|
|
129
|
+
readonly max: number;
|
|
130
|
+
constructor(min: number, max: number);
|
|
131
|
+
}
|
|
109
132
|
type Coordinate = {
|
|
110
133
|
lat: number;
|
|
111
134
|
lng: number;
|
|
@@ -121,6 +144,7 @@ export declare const faker: Readonly<import("@faker-js/faker").Faker & {
|
|
|
121
144
|
resetSequence: typeof resetSequence;
|
|
122
145
|
resetAllSequences: typeof resetAllSequences;
|
|
123
146
|
price: typeof price;
|
|
147
|
+
age: typeof age;
|
|
124
148
|
coordinates: {
|
|
125
149
|
within: typeof coordinateInRadius;
|
|
126
150
|
outside: typeof coordinateOutsideRadius;
|
package/dist/faker.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"faker.d.cts","names":[],"sources":["../src/faker.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;wBAkFgB,cAAc;;;;;;;;;;;;;;;wBA2Bd,WAAW;;;;;;;;;;;;;;;;;;;;;wBAmCX,SAAS;;;;;;;;;;;;;;;;;;;wBA2BT,cAAc,eAAkB;;;;;;;;;;;;;;;;;;wBA0BhC;;;;;;;;;;;;;iBAgBP;
|
|
1
|
+
{"version":3,"file":"faker.d.cts","names":[],"sources":["../src/faker.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;wBAkFgB,cAAc;;;;;;;;;;;;;;;wBA2Bd,WAAW;;;;;;;;;;;;;;;;;;;;;wBAmCX,SAAS;;;;;;;;;;;;;;;;;;;wBA2BT,cAAc,eAAkB;;;;;;;;;;;;;;;;;;wBA0BhC;;;;;;;;;;;;;iBAgBP;;;;;;;;;;;;;;;;;iBAoBA,IAAI,aAAa,eAAoB;;qBAOjC,wBAAwB;WAE1B;WACA;EAFV,YACU,aACA;;KAWN;EACJ;EACA;;wBAGe,mBACf,QAAQ,YACR,iBACE;iBA8BM,wBACR,QAAQ,YACR,yBACA,0BACE;qBA8EU,OAAK,mCAAA;;EAKF,OAAA;;;;;;;;;IASb,eAAM;IACN,gBAAO;;;;;;;YASE;;EAEX,WAAW;;EAEX,WAAW;;;;;;YAOA,sBAAsB"}
|
package/dist/faker.d.mts
CHANGED
|
@@ -106,6 +106,29 @@ export declare function resetAllSequences(): void;
|
|
|
106
106
|
* ```
|
|
107
107
|
*/
|
|
108
108
|
declare function price(): number;
|
|
109
|
+
/**
|
|
110
|
+
* A birthdate for someone of the given age today: exactly `min` years old, or
|
|
111
|
+
* anywhere from `min` to `max` inclusive.
|
|
112
|
+
*
|
|
113
|
+
* Through faker's own `date.birthdate`, so seeding faker makes it repeatable.
|
|
114
|
+
*
|
|
115
|
+
* @param min - The youngest age, in whole years
|
|
116
|
+
* @param max - The oldest age, in whole years (default: `min`)
|
|
117
|
+
* @returns The birthdate
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* faker.age(18); // someone who is 18 today
|
|
122
|
+
* faker.age(18, 24); // someone aged 18 to 24
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
declare function age(min: number, max?: number): Date;
|
|
126
|
+
/** An age range no one can be: a negative age, or an oldest below a youngest. */
|
|
127
|
+
export declare class AgeRangeInvalid extends Error {
|
|
128
|
+
readonly min: number;
|
|
129
|
+
readonly max: number;
|
|
130
|
+
constructor(min: number, max: number);
|
|
131
|
+
}
|
|
109
132
|
type Coordinate = {
|
|
110
133
|
lat: number;
|
|
111
134
|
lng: number;
|
|
@@ -121,6 +144,7 @@ export declare const faker: Readonly<import("@faker-js/faker").Faker & {
|
|
|
121
144
|
resetSequence: typeof resetSequence;
|
|
122
145
|
resetAllSequences: typeof resetAllSequences;
|
|
123
146
|
price: typeof price;
|
|
147
|
+
age: typeof age;
|
|
124
148
|
coordinates: {
|
|
125
149
|
within: typeof coordinateInRadius;
|
|
126
150
|
outside: typeof coordinateOutsideRadius;
|
package/dist/faker.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"faker.d.mts","names":[],"sources":["../src/faker.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;wBAkFgB,cAAc;;;;;;;;;;;;;;;wBA2Bd,WAAW;;;;;;;;;;;;;;;;;;;;;wBAmCX,SAAS;;;;;;;;;;;;;;;;;;;wBA2BT,cAAc,eAAkB;;;;;;;;;;;;;;;;;;wBA0BhC;;;;;;;;;;;;;iBAgBP;
|
|
1
|
+
{"version":3,"file":"faker.d.mts","names":[],"sources":["../src/faker.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;wBAkFgB,cAAc;;;;;;;;;;;;;;;wBA2Bd,WAAW;;;;;;;;;;;;;;;;;;;;;wBAmCX,SAAS;;;;;;;;;;;;;;;;;;;wBA2BT,cAAc,eAAkB;;;;;;;;;;;;;;;;;;wBA0BhC;;;;;;;;;;;;;iBAgBP;;;;;;;;;;;;;;;;;iBAoBA,IAAI,aAAa,eAAoB;;qBAOjC,wBAAwB;WAE1B;WACA;EAFV,YACU,aACA;;KAWN;EACJ;EACA;;wBAGe,mBACf,QAAQ,YACR,iBACE;iBA8BM,wBACR,QAAQ,YACR,yBACA,0BACE;qBA8EU,OAAK,mCAAA;;EAKF,OAAA;;;;;;;;;IASb,eAAM;IACN,gBAAO;;;;;;;YASE;;EAEX,WAAW;;EAEX,WAAW;;;;;;YAOA,sBAAsB"}
|
package/dist/faker.mjs
CHANGED
|
@@ -191,6 +191,41 @@ function resetAllSequences() {
|
|
|
191
191
|
function price() {
|
|
192
192
|
return +faker.commerce.price();
|
|
193
193
|
}
|
|
194
|
+
/**
|
|
195
|
+
* A birthdate for someone of the given age today: exactly `min` years old, or
|
|
196
|
+
* anywhere from `min` to `max` inclusive.
|
|
197
|
+
*
|
|
198
|
+
* Through faker's own `date.birthdate`, so seeding faker makes it repeatable.
|
|
199
|
+
*
|
|
200
|
+
* @param min - The youngest age, in whole years
|
|
201
|
+
* @param max - The oldest age, in whole years (default: `min`)
|
|
202
|
+
* @returns The birthdate
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* ```typescript
|
|
206
|
+
* faker.age(18); // someone who is 18 today
|
|
207
|
+
* faker.age(18, 24); // someone aged 18 to 24
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
function age(min, max = min) {
|
|
211
|
+
if (min < 0 || max < min) throw new AgeRangeInvalid(min, max);
|
|
212
|
+
return faker$1.date.birthdate({
|
|
213
|
+
mode: "age",
|
|
214
|
+
min,
|
|
215
|
+
max
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
/** An age range no one can be: a negative age, or an oldest below a youngest. */
|
|
219
|
+
var AgeRangeInvalid = class extends Error {
|
|
220
|
+
min;
|
|
221
|
+
max;
|
|
222
|
+
constructor(min, max) {
|
|
223
|
+
super(`faker.age(${min}, ${max}) asks for an age no one can be. Pass the youngest age first and the oldest second, neither negative: faker.age(18) or faker.age(18, 24).`);
|
|
224
|
+
this.min = min;
|
|
225
|
+
this.max = max;
|
|
226
|
+
this.name = "AgeRangeInvalid";
|
|
227
|
+
}
|
|
228
|
+
};
|
|
194
229
|
function coordinateInRadius(center, radius) {
|
|
195
230
|
const d = radius / 6378137;
|
|
196
231
|
const theta = 2 * Math.PI * Math.random();
|
|
@@ -259,12 +294,13 @@ const faker = Object.freeze(Object.assign({}, faker$1, {
|
|
|
259
294
|
resetSequence,
|
|
260
295
|
resetAllSequences,
|
|
261
296
|
price,
|
|
297
|
+
age,
|
|
262
298
|
coordinates: {
|
|
263
299
|
within: coordinateInRadius,
|
|
264
300
|
outside: coordinateOutsideRadius
|
|
265
301
|
}
|
|
266
302
|
}));
|
|
267
303
|
//#endregion
|
|
268
|
-
export { coordinateInRadius, faker, identifier, resetAllSequences, resetSequence, sequence, timestamps };
|
|
304
|
+
export { AgeRangeInvalid, coordinateInRadius, faker, identifier, resetAllSequences, resetSequence, sequence, timestamps };
|
|
269
305
|
|
|
270
306
|
//# sourceMappingURL=faker.mjs.map
|
package/dist/faker.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"faker.mjs","names":["baseFaker"],"sources":["../src/faker.ts"],"sourcesContent":["import { faker as baseFaker } from '@faker-js/faker';\n\n// NOTE: This is a simple way to extend `faker` with additional methods\n\n/**\n * Atomic counter implementation for thread-safe sequence generation.\n * Provides a clean abstraction for generating sequential numbers in tests.\n * While JavaScript is single-threaded, this class makes the intent explicit.\n *\n * @example\n * ```typescript\n * const counter = new AtomicCounter(100);\n * console.log(counter.increment()); // 101\n * console.log(counter.increment()); // 102\n * console.log(counter.get()); // 102\n * counter.reset(200);\n * console.log(counter.increment()); // 201\n * ```\n */\nclass AtomicCounter {\n\t/**\n\t * The current counter value.\n\t * @private\n\t */\n\tprivate value: number;\n\n\t/**\n\t * Creates a new atomic counter.\n\t * @param initialValue - The starting value (default: 0)\n\t */\n\tconstructor(initialValue = 0) {\n\t\tthis.value = initialValue;\n\t}\n\n\t/**\n\t * Increments the counter and returns the new value.\n\t * @returns The incremented value\n\t */\n\tincrement(): number {\n\t\t// In Node.js, JavaScript is single-threaded within the event loop,\n\t\t// so this operation is already atomic. However, this class provides\n\t\t// a cleaner abstraction and makes the intent explicit.\n\t\treturn ++this.value;\n\t}\n\n\t/**\n\t * Gets the current counter value without incrementing.\n\t * @returns The current value\n\t */\n\tget(): number {\n\t\treturn this.value;\n\t}\n\n\t/**\n\t * Resets the counter to a specific value.\n\t * @param value - The new value (default: 0)\n\t */\n\treset(value = 0): void {\n\t\tthis.value = value;\n\t}\n}\n\n/**\n * Generates random timestamp fields for database records.\n * Creates a createdAt date in the past and an updatedAt date between creation and now.\n * Milliseconds are set to 0 for cleaner database storage.\n *\n * @returns Object with createdAt and updatedAt Date fields\n *\n * @example\n * ```typescript\n * const { createdAt, updatedAt } = timestamps();\n * console.log(createdAt); // 2023-05-15T10:30:00.000Z\n * console.log(updatedAt); // 2023-11-20T14:45:00.000Z\n *\n * // Use in factory\n * const user = {\n * name: 'John Doe',\n * ...timestamps()\n * };\n * ```\n */\nexport function timestamps(): Timestamps {\n\tconst createdAt = faker.date.past();\n\tconst updatedAt = faker.date.between({\n\t\tfrom: createdAt,\n\t\tto: new Date(),\n\t});\n\n\tcreatedAt.setMilliseconds(0);\n\tupdatedAt.setMilliseconds(0);\n\n\treturn { createdAt, updatedAt };\n}\n\n/**\n * Generates a reverse domain name identifier.\n * Useful for creating unique identifiers that follow domain naming conventions.\n *\n * @param suffix - Optional suffix to append to the identifier\n * @returns A reverse domain name string (e.g., \"com.example.feature123\")\n *\n * @example\n * ```typescript\n * console.log(identifier()); // \"com.example.widget1\"\n * console.log(identifier('user')); // \"org.acme.user\"\n * console.log(identifier('api')); // \"net.demo.api\"\n * ```\n */\nexport function identifier(suffix?: string): string {\n\treturn [\n\t\tfaker.internet.domainSuffix(),\n\t\tfaker.internet.domainWord(),\n\t\tsuffix ? suffix : faker.internet.domainWord() + sequence('identifier'),\n\t].join('.');\n}\n\n/**\n * Storage for named sequence counters.\n * Each sequence maintains its own independent counter.\n * @private\n */\nconst sequences = new Map<string, AtomicCounter>();\n\n/**\n * Generates sequential numbers for a named sequence.\n * Useful for creating unique IDs or numbered test data.\n * Each named sequence maintains its own counter.\n *\n * @param name - The sequence name (default: 'default')\n * @returns The next number in the sequence\n *\n * @example\n * ```typescript\n * console.log(sequence()); // 1\n * console.log(sequence()); // 2\n * console.log(sequence('user')); // 1\n * console.log(sequence('user')); // 2\n * console.log(sequence()); // 3\n *\n * // Use in factories\n * const email = `user${sequence('email')}@example.com`;\n * ```\n */\nexport function sequence(name = 'default'): number {\n\tif (!sequences.has(name)) {\n\t\tsequences.set(name, new AtomicCounter());\n\t}\n\n\tconst counter = sequences.get(name) as AtomicCounter;\n\treturn counter.increment();\n}\n\n/**\n * Resets a named sequence counter to a specific value.\n * Useful for resetting sequences between test suites.\n *\n * @param name - The sequence name to reset (default: 'default')\n * @param value - The new starting value (default: 0)\n *\n * @example\n * ```typescript\n * sequence('user'); // 1\n * sequence('user'); // 2\n * resetSequence('user');\n * sequence('user'); // 1\n *\n * resetSequence('order', 1000);\n * sequence('order'); // 1001\n * ```\n */\nexport function resetSequence(name = 'default', value = 0): void {\n\tif (sequences.has(name)) {\n\t\tconst counter = sequences.get(name) as AtomicCounter;\n\t\tcounter.reset(value);\n\t} else {\n\t\tsequences.set(name, new AtomicCounter(value));\n\t}\n}\n\n/**\n * Resets all sequence counters.\n * Useful for cleaning up between test suites to ensure predictable sequences.\n *\n * @example\n * ```typescript\n * // In test setup\n * beforeEach(() => {\n * resetAllSequences();\n * });\n *\n * it('starts sequences from 1', () => {\n * expect(sequence()).toBe(1);\n * expect(sequence('user')).toBe(1);\n * });\n * ```\n */\nexport function resetAllSequences(): void {\n\tsequences.clear();\n}\n\n/**\n * Generates a random price as a number.\n * Converts faker's string price to a numeric value.\n *\n * @returns A random price number\n *\n * @example\n * ```typescript\n * const productPrice = price(); // 29.99\n * const total = price() * quantity; // Numeric calculation\n * ```\n */\nfunction price(): number {\n\treturn +faker.commerce.price();\n}\n\ntype Coordinate = {\n\tlat: number;\n\tlng: number;\n};\n\nexport function coordinateInRadius(\n\tcenter: Coordinate,\n\tradius: number,\n): Coordinate {\n\t// Earth's radius in meters\n\tconst earth = 6378137;\n\t// Convert radius from meters to degrees\n\tconst d = radius / earth;\n\n\t// Random bearing and distance\n\tconst theta = 2 * Math.PI * Math.random();\n\tconst r = d * Math.sqrt(Math.random());\n\n\tconst lat1 = (center.lat * Math.PI) / 180;\n\tconst lng1 = (center.lng * Math.PI) / 180;\n\n\tconst lat2 = Math.asin(\n\t\tMath.sin(lat1) * Math.cos(r) +\n\t\t\tMath.cos(lat1) * Math.sin(r) * Math.cos(theta),\n\t);\n\tconst lng2 =\n\t\tlng1 +\n\t\tMath.atan2(\n\t\t\tMath.sin(theta) * Math.sin(r) * Math.cos(lat1),\n\t\t\tMath.cos(r) - Math.sin(lat1) * Math.sin(lat2),\n\t\t);\n\n\treturn {\n\t\tlat: (lat2 * 180) / Math.PI,\n\t\tlng: (lng2 * 180) / Math.PI,\n\t};\n}\n\nfunction coordinateOutsideRadius(\n\tcenter: Coordinate,\n\tminRadiusMeters: number,\n\tmaxRadiusMeters: number,\n): Coordinate {\n\t// Earth's radius in meters\n\tconst earth = 6378137;\n\n\t// Convert radii from meters to radians\n\tconst minD = minRadiusMeters / earth;\n\tconst maxD = maxRadiusMeters / earth;\n\n\t// Random bearing\n\tconst theta = 2 * Math.PI * Math.random();\n\n\t// Random distance in annular ring (uniform distribution by area)\n\t// For uniform distribution in annulus: r = sqrt(r_min² + (r_max² - r_min²) * random)\n\tconst r = Math.sqrt(\n\t\tminD * minD + (maxD * maxD - minD * minD) * Math.random(),\n\t);\n\n\tconst lat1 = (center.lat * Math.PI) / 180;\n\tconst lng1 = (center.lng * Math.PI) / 180;\n\n\tconst lat2 = Math.asin(\n\t\tMath.sin(lat1) * Math.cos(r) +\n\t\t\tMath.cos(lat1) * Math.sin(r) * Math.cos(theta),\n\t);\n\tconst lng2 =\n\t\tlng1 +\n\t\tMath.atan2(\n\t\t\tMath.sin(theta) * Math.sin(r) * Math.cos(lat1),\n\t\t\tMath.cos(r) - Math.sin(lat1) * Math.sin(lat2),\n\t\t);\n\n\t// Normalize longitude to [-180, 180]\n\tconst normalizedLng = (((lng2 * 180) / Math.PI + 540) % 360) - 180;\n\n\treturn {\n\t\tlat: (lat2 * 180) / Math.PI,\n\t\tlng: normalizedLng,\n\t};\n}\n\n/**\n * Enhanced faker instance with additional utility methods for testing.\n * Extends @faker-js/faker with custom methods for common test data generation patterns.\n *\n * @example\n * ```typescript\n * import { faker } from '@geekmidas/testkit';\n *\n * // Use standard faker methods\n * const name = faker.person.fullName();\n * const email = faker.internet.email();\n *\n * // Use custom extensions\n * const { createdAt, updatedAt } = faker.timestamps();\n * const id = faker.identifier('user');\n * const orderNumber = faker.sequence('order');\n * const productPrice = faker.price();\n * ```\n */\n/**\n * `faker.internet`, with every address lowercase.\n *\n * An auth server stores addresses lowercased — Better Auth does — so a test\n * that signs in as faker's `Ada.Lovelace@…` and reads back `ada.lovelace@…`\n * fails only when faker happened to capitalise: a flake, not a bug. The rest\n * of the module is faker's own, through the prototype.\n */\nconst internet: typeof baseFaker.internet = Object.assign(\n\tObject.create(baseFaker.internet),\n\t{\n\t\temail: (...args: Parameters<typeof baseFaker.internet.email>) =>\n\t\t\tbaseFaker.internet.email(...args).toLowerCase(),\n\t\texampleEmail: (\n\t\t\t...args: Parameters<typeof baseFaker.internet.exampleEmail>\n\t\t) => baseFaker.internet.exampleEmail(...args).toLowerCase(),\n\t},\n);\n\nexport const faker = Object.freeze(\n\tObject.assign({}, baseFaker, {\n\t\tinternet,\n\t\t// A prototype method, so the spread above leaves it behind. The modules\n\t\t// copied across still draw from `baseFaker`, so seeding it seeds them.\n\t\tseed: (seed?: number) => baseFaker.seed(seed),\n\t\ttimestamps,\n\t\tidentifier,\n\t\tsequence,\n\t\tresetSequence,\n\t\tresetAllSequences,\n\t\tprice,\n\t\tcoordinates: {\n\t\t\twithin: coordinateInRadius,\n\t\t\toutside: coordinateOutsideRadius,\n\t\t},\n\t}),\n);\n\n/**\n * Type definition for timestamp fields.\n * Used by the timestamps() function to generate date fields.\n */\nexport type Timestamps = {\n\t/** The creation date */\n\tcreatedAt: Date;\n\t/** The last update date */\n\tupdatedAt: Date;\n};\n\n/**\n * Type definition for the enhanced faker factory.\n * Includes all standard faker methods plus custom extensions.\n */\nexport type FakerFactory = typeof faker;\n"],"mappings":";;;;;;;;;;;;;;;;;AAmBA,IAAM,gBAAN,MAAoB;;;;;CAKnB;;;;;CAMA,YAAY,eAAe,GAAG;EAC7B,KAAK,QAAQ;CACd;;;;;CAMA,YAAoB;EAInB,OAAO,EAAE,KAAK;CACf;;;;;CAMA,MAAc;EACb,OAAO,KAAK;CACb;;;;;CAMA,MAAM,QAAQ,GAAS;EACtB,KAAK,QAAQ;CACd;AACD;;;;;;;;;;;;;;;;;;;;;AAsBA,SAAgB,aAAyB;CACxC,MAAM,YAAY,MAAM,KAAK,KAAK;CAClC,MAAM,YAAY,MAAM,KAAK,QAAQ;EACpC,MAAM;EACN,oBAAI,IAAI,KAAK;CACd,CAAC;CAED,UAAU,gBAAgB,CAAC;CAC3B,UAAU,gBAAgB,CAAC;CAE3B,OAAO;EAAE;EAAW;CAAU;AAC/B;;;;;;;;;;;;;;;AAgBA,SAAgB,WAAW,QAAyB;CACnD,OAAO;EACN,MAAM,SAAS,aAAa;EAC5B,MAAM,SAAS,WAAW;EAC1B,SAAS,SAAS,MAAM,SAAS,WAAW,IAAI,SAAS,YAAY;CACtE,CAAC,CAAC,KAAK,GAAG;AACX;;;;;;AAOA,MAAM,4BAAY,IAAI,IAA2B;;;;;;;;;;;;;;;;;;;;;AAsBjD,SAAgB,SAAS,OAAO,WAAmB;CAClD,IAAI,CAAC,UAAU,IAAI,IAAI,GACtB,UAAU,IAAI,MAAM,I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,OAAO;CAIxC,MAAM,IAAI,KAAK,KACd,OAAO,QAAQ,OAAO,OAAO,OAAO,QAAQ,KAAK,OAAO,CACzD;CAEA,MAAM,OAAQ,OAAO,MAAM,KAAK,KAAM;CACtC,MAAM,OAAQ,OAAO,MAAM,KAAK,KAAM;CAEtC,MAAM,OAAO,KAAK,KACjB,KAAK,IAAI,IAAI,IAAI,KAAK,IAAI,CAAC,IAC1B,KAAK,IAAI,IAAI,IAAI,KAAK,IAAI,CAAC,IAAI,KAAK,IAAI,KAAK,CAC/C;CASA,MAAM,kBAPL,OACA,KAAK,MACJ,KAAK,IAAI,KAAK,IAAI,KAAK,IAAI,CAAC,IAAI,KAAK,IAAI,IAAI,GAC7C,KAAK,IAAI,CAAC,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,IAAI,IAAI,CAC7C,KAG+B,MAAO,KAAK,KAAK,OAAO,MAAO;CAE/D,OAAO;EACN,KAAM,OAAO,MAAO,KAAK;EACzB,KAAK;CACN;AACD;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,MAAM,WAAsC,OAAO,OAClD,OAAO,OAAOA,QAAU,QAAQ,GAChC;CACC,QAAQ,GAAG,SACVA,QAAU,SAAS,MAAM,GAAG,IAAI,CAAC,CAAC,YAAY;CAC/C,eACC,GAAG,SACCA,QAAU,SAAS,aAAa,GAAG,IAAI,CAAC,CAAC,YAAY;AAC3D,CACD;AAEA,MAAa,QAAQ,OAAO,OAC3B,OAAO,OAAO,CAAC,GAAGA,SAAW;CAC5B;CAGA,OAAO,SAAkBA,QAAU,KAAK,IAAI;CAC5C;CACA;CACA;CACA;CACA;CACA;CACA,aAAa;EACZ,QAAQ;EACR,SAAS;CACV;AACD,CAAC,CACF"}
|
|
1
|
+
{"version":3,"file":"faker.mjs","names":["baseFaker"],"sources":["../src/faker.ts"],"sourcesContent":["import { faker as baseFaker } from '@faker-js/faker';\n\n// NOTE: This is a simple way to extend `faker` with additional methods\n\n/**\n * Atomic counter implementation for thread-safe sequence generation.\n * Provides a clean abstraction for generating sequential numbers in tests.\n * While JavaScript is single-threaded, this class makes the intent explicit.\n *\n * @example\n * ```typescript\n * const counter = new AtomicCounter(100);\n * console.log(counter.increment()); // 101\n * console.log(counter.increment()); // 102\n * console.log(counter.get()); // 102\n * counter.reset(200);\n * console.log(counter.increment()); // 201\n * ```\n */\nclass AtomicCounter {\n\t/**\n\t * The current counter value.\n\t * @private\n\t */\n\tprivate value: number;\n\n\t/**\n\t * Creates a new atomic counter.\n\t * @param initialValue - The starting value (default: 0)\n\t */\n\tconstructor(initialValue = 0) {\n\t\tthis.value = initialValue;\n\t}\n\n\t/**\n\t * Increments the counter and returns the new value.\n\t * @returns The incremented value\n\t */\n\tincrement(): number {\n\t\t// In Node.js, JavaScript is single-threaded within the event loop,\n\t\t// so this operation is already atomic. However, this class provides\n\t\t// a cleaner abstraction and makes the intent explicit.\n\t\treturn ++this.value;\n\t}\n\n\t/**\n\t * Gets the current counter value without incrementing.\n\t * @returns The current value\n\t */\n\tget(): number {\n\t\treturn this.value;\n\t}\n\n\t/**\n\t * Resets the counter to a specific value.\n\t * @param value - The new value (default: 0)\n\t */\n\treset(value = 0): void {\n\t\tthis.value = value;\n\t}\n}\n\n/**\n * Generates random timestamp fields for database records.\n * Creates a createdAt date in the past and an updatedAt date between creation and now.\n * Milliseconds are set to 0 for cleaner database storage.\n *\n * @returns Object with createdAt and updatedAt Date fields\n *\n * @example\n * ```typescript\n * const { createdAt, updatedAt } = timestamps();\n * console.log(createdAt); // 2023-05-15T10:30:00.000Z\n * console.log(updatedAt); // 2023-11-20T14:45:00.000Z\n *\n * // Use in factory\n * const user = {\n * name: 'John Doe',\n * ...timestamps()\n * };\n * ```\n */\nexport function timestamps(): Timestamps {\n\tconst createdAt = faker.date.past();\n\tconst updatedAt = faker.date.between({\n\t\tfrom: createdAt,\n\t\tto: new Date(),\n\t});\n\n\tcreatedAt.setMilliseconds(0);\n\tupdatedAt.setMilliseconds(0);\n\n\treturn { createdAt, updatedAt };\n}\n\n/**\n * Generates a reverse domain name identifier.\n * Useful for creating unique identifiers that follow domain naming conventions.\n *\n * @param suffix - Optional suffix to append to the identifier\n * @returns A reverse domain name string (e.g., \"com.example.feature123\")\n *\n * @example\n * ```typescript\n * console.log(identifier()); // \"com.example.widget1\"\n * console.log(identifier('user')); // \"org.acme.user\"\n * console.log(identifier('api')); // \"net.demo.api\"\n * ```\n */\nexport function identifier(suffix?: string): string {\n\treturn [\n\t\tfaker.internet.domainSuffix(),\n\t\tfaker.internet.domainWord(),\n\t\tsuffix ? suffix : faker.internet.domainWord() + sequence('identifier'),\n\t].join('.');\n}\n\n/**\n * Storage for named sequence counters.\n * Each sequence maintains its own independent counter.\n * @private\n */\nconst sequences = new Map<string, AtomicCounter>();\n\n/**\n * Generates sequential numbers for a named sequence.\n * Useful for creating unique IDs or numbered test data.\n * Each named sequence maintains its own counter.\n *\n * @param name - The sequence name (default: 'default')\n * @returns The next number in the sequence\n *\n * @example\n * ```typescript\n * console.log(sequence()); // 1\n * console.log(sequence()); // 2\n * console.log(sequence('user')); // 1\n * console.log(sequence('user')); // 2\n * console.log(sequence()); // 3\n *\n * // Use in factories\n * const email = `user${sequence('email')}@example.com`;\n * ```\n */\nexport function sequence(name = 'default'): number {\n\tif (!sequences.has(name)) {\n\t\tsequences.set(name, new AtomicCounter());\n\t}\n\n\tconst counter = sequences.get(name) as AtomicCounter;\n\treturn counter.increment();\n}\n\n/**\n * Resets a named sequence counter to a specific value.\n * Useful for resetting sequences between test suites.\n *\n * @param name - The sequence name to reset (default: 'default')\n * @param value - The new starting value (default: 0)\n *\n * @example\n * ```typescript\n * sequence('user'); // 1\n * sequence('user'); // 2\n * resetSequence('user');\n * sequence('user'); // 1\n *\n * resetSequence('order', 1000);\n * sequence('order'); // 1001\n * ```\n */\nexport function resetSequence(name = 'default', value = 0): void {\n\tif (sequences.has(name)) {\n\t\tconst counter = sequences.get(name) as AtomicCounter;\n\t\tcounter.reset(value);\n\t} else {\n\t\tsequences.set(name, new AtomicCounter(value));\n\t}\n}\n\n/**\n * Resets all sequence counters.\n * Useful for cleaning up between test suites to ensure predictable sequences.\n *\n * @example\n * ```typescript\n * // In test setup\n * beforeEach(() => {\n * resetAllSequences();\n * });\n *\n * it('starts sequences from 1', () => {\n * expect(sequence()).toBe(1);\n * expect(sequence('user')).toBe(1);\n * });\n * ```\n */\nexport function resetAllSequences(): void {\n\tsequences.clear();\n}\n\n/**\n * Generates a random price as a number.\n * Converts faker's string price to a numeric value.\n *\n * @returns A random price number\n *\n * @example\n * ```typescript\n * const productPrice = price(); // 29.99\n * const total = price() * quantity; // Numeric calculation\n * ```\n */\nfunction price(): number {\n\treturn +faker.commerce.price();\n}\n\n/**\n * A birthdate for someone of the given age today: exactly `min` years old, or\n * anywhere from `min` to `max` inclusive.\n *\n * Through faker's own `date.birthdate`, so seeding faker makes it repeatable.\n *\n * @param min - The youngest age, in whole years\n * @param max - The oldest age, in whole years (default: `min`)\n * @returns The birthdate\n *\n * @example\n * ```typescript\n * faker.age(18); // someone who is 18 today\n * faker.age(18, 24); // someone aged 18 to 24\n * ```\n */\nfunction age(min: number, max: number = min): Date {\n\tif (min < 0 || max < min) throw new AgeRangeInvalid(min, max);\n\n\treturn baseFaker.date.birthdate({ mode: 'age', min, max });\n}\n\n/** An age range no one can be: a negative age, or an oldest below a youngest. */\nexport class AgeRangeInvalid extends Error {\n\tconstructor(\n\t\treadonly min: number,\n\t\treadonly max: number,\n\t) {\n\t\tsuper(\n\t\t\t`faker.age(${min}, ${max}) asks for an age no one can be. Pass the ` +\n\t\t\t\t'youngest age first and the oldest second, neither negative: ' +\n\t\t\t\t'faker.age(18) or faker.age(18, 24).',\n\t\t);\n\t\tthis.name = 'AgeRangeInvalid';\n\t}\n}\n\ntype Coordinate = {\n\tlat: number;\n\tlng: number;\n};\n\nexport function coordinateInRadius(\n\tcenter: Coordinate,\n\tradius: number,\n): Coordinate {\n\t// Earth's radius in meters\n\tconst earth = 6378137;\n\t// Convert radius from meters to degrees\n\tconst d = radius / earth;\n\n\t// Random bearing and distance\n\tconst theta = 2 * Math.PI * Math.random();\n\tconst r = d * Math.sqrt(Math.random());\n\n\tconst lat1 = (center.lat * Math.PI) / 180;\n\tconst lng1 = (center.lng * Math.PI) / 180;\n\n\tconst lat2 = Math.asin(\n\t\tMath.sin(lat1) * Math.cos(r) +\n\t\t\tMath.cos(lat1) * Math.sin(r) * Math.cos(theta),\n\t);\n\tconst lng2 =\n\t\tlng1 +\n\t\tMath.atan2(\n\t\t\tMath.sin(theta) * Math.sin(r) * Math.cos(lat1),\n\t\t\tMath.cos(r) - Math.sin(lat1) * Math.sin(lat2),\n\t\t);\n\n\treturn {\n\t\tlat: (lat2 * 180) / Math.PI,\n\t\tlng: (lng2 * 180) / Math.PI,\n\t};\n}\n\nfunction coordinateOutsideRadius(\n\tcenter: Coordinate,\n\tminRadiusMeters: number,\n\tmaxRadiusMeters: number,\n): Coordinate {\n\t// Earth's radius in meters\n\tconst earth = 6378137;\n\n\t// Convert radii from meters to radians\n\tconst minD = minRadiusMeters / earth;\n\tconst maxD = maxRadiusMeters / earth;\n\n\t// Random bearing\n\tconst theta = 2 * Math.PI * Math.random();\n\n\t// Random distance in annular ring (uniform distribution by area)\n\t// For uniform distribution in annulus: r = sqrt(r_min² + (r_max² - r_min²) * random)\n\tconst r = Math.sqrt(\n\t\tminD * minD + (maxD * maxD - minD * minD) * Math.random(),\n\t);\n\n\tconst lat1 = (center.lat * Math.PI) / 180;\n\tconst lng1 = (center.lng * Math.PI) / 180;\n\n\tconst lat2 = Math.asin(\n\t\tMath.sin(lat1) * Math.cos(r) +\n\t\t\tMath.cos(lat1) * Math.sin(r) * Math.cos(theta),\n\t);\n\tconst lng2 =\n\t\tlng1 +\n\t\tMath.atan2(\n\t\t\tMath.sin(theta) * Math.sin(r) * Math.cos(lat1),\n\t\t\tMath.cos(r) - Math.sin(lat1) * Math.sin(lat2),\n\t\t);\n\n\t// Normalize longitude to [-180, 180]\n\tconst normalizedLng = (((lng2 * 180) / Math.PI + 540) % 360) - 180;\n\n\treturn {\n\t\tlat: (lat2 * 180) / Math.PI,\n\t\tlng: normalizedLng,\n\t};\n}\n\n/**\n * Enhanced faker instance with additional utility methods for testing.\n * Extends @faker-js/faker with custom methods for common test data generation patterns.\n *\n * @example\n * ```typescript\n * import { faker } from '@geekmidas/testkit';\n *\n * // Use standard faker methods\n * const name = faker.person.fullName();\n * const email = faker.internet.email();\n *\n * // Use custom extensions\n * const { createdAt, updatedAt } = faker.timestamps();\n * const id = faker.identifier('user');\n * const orderNumber = faker.sequence('order');\n * const productPrice = faker.price();\n * ```\n */\n/**\n * `faker.internet`, with every address lowercase.\n *\n * An auth server stores addresses lowercased — Better Auth does — so a test\n * that signs in as faker's `Ada.Lovelace@…` and reads back `ada.lovelace@…`\n * fails only when faker happened to capitalise: a flake, not a bug. The rest\n * of the module is faker's own, through the prototype.\n */\nconst internet: typeof baseFaker.internet = Object.assign(\n\tObject.create(baseFaker.internet),\n\t{\n\t\temail: (...args: Parameters<typeof baseFaker.internet.email>) =>\n\t\t\tbaseFaker.internet.email(...args).toLowerCase(),\n\t\texampleEmail: (\n\t\t\t...args: Parameters<typeof baseFaker.internet.exampleEmail>\n\t\t) => baseFaker.internet.exampleEmail(...args).toLowerCase(),\n\t},\n);\n\nexport const faker = Object.freeze(\n\tObject.assign({}, baseFaker, {\n\t\tinternet,\n\t\t// A prototype method, so the spread above leaves it behind. The modules\n\t\t// copied across still draw from `baseFaker`, so seeding it seeds them.\n\t\tseed: (seed?: number) => baseFaker.seed(seed),\n\t\ttimestamps,\n\t\tidentifier,\n\t\tsequence,\n\t\tresetSequence,\n\t\tresetAllSequences,\n\t\tprice,\n\t\tage,\n\t\tcoordinates: {\n\t\t\twithin: coordinateInRadius,\n\t\t\toutside: coordinateOutsideRadius,\n\t\t},\n\t}),\n);\n\n/**\n * Type definition for timestamp fields.\n * Used by the timestamps() function to generate date fields.\n */\nexport type Timestamps = {\n\t/** The creation date */\n\tcreatedAt: Date;\n\t/** The last update date */\n\tupdatedAt: Date;\n};\n\n/**\n * Type definition for the enhanced faker factory.\n * Includes all standard faker methods plus custom extensions.\n */\nexport type FakerFactory = typeof faker;\n"],"mappings":";;;;;;;;;;;;;;;;;AAmBA,IAAM,gBAAN,MAAoB;;;;;CAKnB;;;;;CAMA,YAAY,eAAe,GAAG;EAC7B,KAAK,QAAQ;CACd;;;;;CAMA,YAAoB;EAInB,OAAO,EAAE,KAAK;CACf;;;;;CAMA,MAAc;EACb,OAAO,KAAK;CACb;;;;;CAMA,MAAM,QAAQ,GAAS;EACtB,KAAK,QAAQ;CACd;AACD;;;;;;;;;;;;;;;;;;;;;AAsBA,SAAgB,aAAyB;CACxC,MAAM,YAAY,MAAM,KAAK,KAAK;CAClC,MAAM,YAAY,MAAM,KAAK,QAAQ;EACpC,MAAM;EACN,oBAAI,IAAI,KAAK;CACd,CAAC;CAED,UAAU,gBAAgB,CAAC;CAC3B,UAAU,gBAAgB,CAAC;CAE3B,OAAO;EAAE;EAAW;CAAU;AAC/B;;;;;;;;;;;;;;;AAgBA,SAAgB,WAAW,QAAyB;CACnD,OAAO;EACN,MAAM,SAAS,aAAa;EAC5B,MAAM,SAAS,WAAW;EAC1B,SAAS,SAAS,MAAM,SAAS,WAAW,IAAI,SAAS,YAAY;CACtE,CAAC,CAAC,KAAK,GAAG;AACX;;;;;;AAOA,MAAM,4BAAY,IAAI,IAA2B;;;;;;;;;;;;;;;;;;;;;AAsBjD,SAAgB,SAAS,OAAO,WAAmB;CAClD,IAAI,CAAC,UAAU,IAAI,IAAI,GACtB,UAAU,IAAI,MAAM,IAAI,cAAc,CAAC;CAIxC,OADgB,UAAU,IAAI,IACjB,CAAC,CAAC,UAAU;AAC1B;;;;;;;;;;;;;;;;;;;AAoBA,SAAgB,cAAc,OAAO,WAAW,QAAQ,GAAS;CAChE,IAAI,UAAU,IAAI,IAAI,GAErB,UAD0B,IAAI,IACxB,CAAC,CAAC,MAAM,KAAK;MAE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CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geekmidas/testkit",
|
|
3
|
-
"version": "10.0.0-alpha.
|
|
3
|
+
"version": "10.0.0-alpha.42",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -161,17 +161,17 @@
|
|
|
161
161
|
"dependencies": {
|
|
162
162
|
"@faker-js/faker": "~10.6.0",
|
|
163
163
|
"tough-cookie": "~6.0.2",
|
|
164
|
-
"@geekmidas/envkit": "^10.0.0-alpha.
|
|
165
|
-
"@geekmidas/
|
|
166
|
-
"@geekmidas/
|
|
164
|
+
"@geekmidas/envkit": "^10.0.0-alpha.42",
|
|
165
|
+
"@geekmidas/services": "^10.0.0-alpha.42",
|
|
166
|
+
"@geekmidas/logger": "^10.0.0-alpha.42"
|
|
167
167
|
},
|
|
168
168
|
"devDependencies": {
|
|
169
169
|
"@better-auth/test-utils": "~1.7.6",
|
|
170
170
|
"@types/pg": "~8.23.1",
|
|
171
171
|
"better-auth": "~1.7.6",
|
|
172
|
-
"@geekmidas/envkit": "^10.0.0-alpha.
|
|
173
|
-
"@geekmidas/logger": "^10.0.0-alpha.
|
|
174
|
-
"@geekmidas/services": "^10.0.0-alpha.
|
|
172
|
+
"@geekmidas/envkit": "^10.0.0-alpha.42",
|
|
173
|
+
"@geekmidas/logger": "^10.0.0-alpha.42",
|
|
174
|
+
"@geekmidas/services": "^10.0.0-alpha.42"
|
|
175
175
|
},
|
|
176
176
|
"repository": {
|
|
177
177
|
"type": "git",
|