@jackfranklin/test-data-bot
Advanced tools
Comparing version 1.4.0 to 2.0.0
@@ -1,34 +0,23 @@ | ||
/// <reference types="faker" /> | ||
export declare type SequenceFunction = (counter: number) => unknown; | ||
export interface SequenceGenerator { | ||
export interface SequenceGenerator<T> { | ||
generatorType: 'sequence'; | ||
userProvidedFunction: SequenceFunction; | ||
call: (userProvidedFunction: SequenceFunction, counter: number) => unknown; | ||
call: (counter: number) => T; | ||
} | ||
export interface FakerGenerator { | ||
generatorType: 'faker'; | ||
call: (fake: Faker.FakerStatic) => any; | ||
} | ||
export interface PerBuildGenerator { | ||
export interface PerBuildGenerator<T> { | ||
generatorType: 'perBuild'; | ||
func: () => any; | ||
call: (f: () => any) => any; | ||
call: () => T; | ||
} | ||
export interface OneOfGenerator { | ||
export interface OneOfGenerator<T> { | ||
generatorType: 'oneOf'; | ||
options: any[]; | ||
call: <T>(options: T[]) => T; | ||
call: () => T; | ||
} | ||
export declare type FieldGenerator = FakerGenerator | SequenceGenerator | OneOfGenerator | PerBuildGenerator; | ||
export declare type Field = string | number | null | FieldGenerator | { | ||
[x: string]: Field | any; | ||
} | any[]; | ||
export declare type FieldGenerator<T> = SequenceGenerator<T> | OneOfGenerator<T> | PerBuildGenerator<T>; | ||
export declare type Field<T = any> = T | FieldGenerator<T> | FieldsConfiguration<T>; | ||
export declare type FieldsConfiguration<FactoryResultType> = { | ||
readonly [x in keyof FactoryResultType]: Field; | ||
readonly [Key in keyof FactoryResultType]: Field<FactoryResultType[Key]>; | ||
}; | ||
export interface Overrides { | ||
[x: string]: Field; | ||
} | ||
export declare type Overrides<FactoryResultType = any> = { | ||
[Key in keyof FactoryResultType]?: Field<FactoryResultType[Key]>; | ||
}; | ||
export interface BuildTimeConfig<FactoryResultType> { | ||
overrides?: Overrides; | ||
overrides?: Overrides<FactoryResultType>; | ||
map?: (builtThing: FactoryResultType) => FactoryResultType; | ||
@@ -39,3 +28,3 @@ traits?: string | string[]; | ||
readonly [traitName: string]: { | ||
overrides?: Overrides; | ||
overrides?: Overrides<FactoryResultType>; | ||
postBuild?: (builtThing: FactoryResultType) => FactoryResultType; | ||
@@ -51,8 +40,11 @@ }; | ||
export declare const build: <FactoryResultType>(factoryNameOrConfig: string | BuildConfiguration<FactoryResultType>, configObject?: BuildConfiguration<FactoryResultType> | undefined) => (buildTimeConfig?: BuildTimeConfig<FactoryResultType> | undefined) => FactoryResultType; | ||
export declare const oneOf: <T>(...options: T[]) => OneOfGenerator; | ||
export declare const bool: () => OneOfGenerator; | ||
export declare const sequence: (userProvidedFunction?: SequenceFunction) => SequenceGenerator; | ||
export declare const perBuild: <T>(func: () => T) => PerBuildGenerator; | ||
export declare type FakerUserArgs = (fake: Faker.FakerStatic) => any; | ||
export declare const fake: (userDefinedUsage: FakerUserArgs) => FakerGenerator; | ||
export declare const oneOf: <T>(...options: T[]) => OneOfGenerator<T>; | ||
export declare const bool: () => OneOfGenerator<boolean>; | ||
declare type Sequence = { | ||
(): SequenceGenerator<number>; | ||
<T>(userProvidedFunction: (count: number) => T): SequenceGenerator<T>; | ||
}; | ||
export declare const sequence: Sequence; | ||
export declare const perBuild: <T>(func: () => T) => PerBuildGenerator<T>; | ||
export {}; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.fake = exports.perBuild = exports.sequence = exports.bool = exports.oneOf = exports.build = void 0; | ||
const faker = __importStar(require("faker")); | ||
const lodash_1 = require("lodash"); | ||
exports.perBuild = exports.sequence = exports.bool = exports.oneOf = exports.build = void 0; | ||
const isGenerator = (field) => { | ||
@@ -44,2 +23,8 @@ if (!field) | ||
}; | ||
function mapValues(object, callback) { | ||
return Object.keys(object).reduce((total, key) => { | ||
total[key] = callback(object[key], key); | ||
return total; | ||
}, {}); | ||
} | ||
const build = (factoryNameOrConfig, configObject) => { | ||
@@ -49,3 +34,3 @@ const config = (typeof factoryNameOrConfig === 'string' ? configObject : factoryNameOrConfig); | ||
const expandConfigFields = (fields, buildTimeConfig = {}) => { | ||
const finalBuiltThing = (0, lodash_1.mapValues)(fields, (fieldValue, fieldKey) => { | ||
const finalBuiltThing = mapValues(fields, (fieldValue, fieldKey) => { | ||
const overrides = buildTimeConfig.overrides || {}; | ||
@@ -70,41 +55,25 @@ const traitsArray = buildTimeTraitsArray(buildTimeConfig); | ||
const expandConfigField = (fieldValue) => { | ||
let calculatedValue; | ||
if (isGenerator(fieldValue)) { | ||
switch (fieldValue.generatorType) { | ||
case 'sequence': { | ||
++sequenceCounter; | ||
calculatedValue = fieldValue.call(fieldValue.userProvidedFunction, sequenceCounter); | ||
break; | ||
return fieldValue.call(++sequenceCounter); | ||
} | ||
case 'faker': { | ||
calculatedValue = fieldValue.call(faker); | ||
break; | ||
} | ||
case 'oneOf': { | ||
calculatedValue = fieldValue.call(fieldValue.options); | ||
break; | ||
} | ||
case 'oneOf': | ||
case 'perBuild': { | ||
calculatedValue = fieldValue.call(fieldValue.func); | ||
break; | ||
return fieldValue.call(); | ||
} | ||
} | ||
} | ||
else if (Array.isArray(fieldValue)) { | ||
calculatedValue = fieldValue.map((v) => expandConfigField(v)); | ||
return calculatedValue; | ||
if (Array.isArray(fieldValue)) { | ||
return fieldValue.map((v) => expandConfigField(v)); | ||
} | ||
else if (fieldValue === null || fieldValue === undefined) { | ||
if (fieldValue === null || fieldValue === undefined) { | ||
// has to be before typeof fieldValue === 'object' | ||
// as typeof null === 'object' | ||
calculatedValue = fieldValue; | ||
return fieldValue; | ||
} | ||
else if (typeof fieldValue === 'object') { | ||
const nestedFieldsObject = fieldValue; | ||
calculatedValue = expandConfigFields(nestedFieldsObject); | ||
if (typeof fieldValue === 'object') { | ||
return expandConfigFields(fieldValue); | ||
} | ||
else { | ||
calculatedValue = fieldValue; | ||
} | ||
return calculatedValue; | ||
return fieldValue; | ||
}; | ||
@@ -131,4 +100,3 @@ return (buildTimeConfig = {}) => { | ||
generatorType: 'oneOf', | ||
options, | ||
call: (options) => { | ||
call: () => { | ||
const randomIndex = Math.floor(Math.random() * options.length); | ||
@@ -142,18 +110,18 @@ return options[randomIndex]; | ||
exports.bool = bool; | ||
const sequence = (userProvidedFunction = (x) => x) => { | ||
exports.sequence = ((userProvidedFunction) => { | ||
return { | ||
generatorType: 'sequence', | ||
userProvidedFunction, | ||
call: (userProvidedFunction, counter) => { | ||
call: (counter) => { | ||
if (typeof userProvidedFunction === 'undefined') { | ||
return counter; | ||
} | ||
return userProvidedFunction(counter); | ||
}, | ||
}; | ||
}; | ||
exports.sequence = sequence; | ||
}); | ||
const perBuild = (func) => { | ||
return { | ||
generatorType: 'perBuild', | ||
func, | ||
call: (f) => { | ||
return f(); | ||
call: () => { | ||
return func(); | ||
}, | ||
@@ -163,11 +131,2 @@ }; | ||
exports.perBuild = perBuild; | ||
const fake = (userDefinedUsage) => { | ||
return { | ||
generatorType: 'faker', | ||
call: (faker) => { | ||
return userDefinedUsage(faker); | ||
}, | ||
}; | ||
}; | ||
exports.fake = fake; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@jackfranklin/test-data-bot", | ||
"version": "1.4.0", | ||
"version": "2.0.0", | ||
"license": "MIT", | ||
"description": "Generate test data for your tests easily.", | ||
"engines": { | ||
"node": ">=10.13" | ||
"node": ">=12" | ||
}, | ||
@@ -36,3 +36,3 @@ "main": "build/index.js", | ||
"@types/jest": "^27.0.3", | ||
"@types/node": "^16.0.0", | ||
"@types/node": "^17.0.9", | ||
"@typescript-eslint/eslint-plugin": "^5.6.0", | ||
@@ -48,10 +48,4 @@ "@typescript-eslint/parser": "^5.6.0", | ||
"ts-jest": "^27.1.1", | ||
"typescript": "^4.5.2" | ||
}, | ||
"dependencies": { | ||
"@types/faker": "5.5.9", | ||
"faker": "5.5.3", | ||
"lodash": "4.17.21", | ||
"@types/lodash": "4.14.178" | ||
"typescript": "^4.6.2" | ||
} | ||
} |
@@ -64,21 +64,2 @@ # @jackfranklin/test-data-bot | ||
### FakerJS with `fake` | ||
test-data-bot provides an API for calling into [Faker.js](https://github.com/marak/Faker.js/) which is a library that lets you generate all sorts of fake data: | ||
```js | ||
const { build, fake } = require('@jackfranklin/test-data-bot'); | ||
const userBuilder = build('User', { | ||
fields: { | ||
name: fake(f => f.name.findName()), | ||
}, | ||
}); | ||
const userOne = userBuilder(); | ||
const userTwo = userBuilder(); | ||
``` | ||
Each user will have a random name as generated by the FakerJS library. You can use any of the Faker API methods that you'd like. | ||
### Incrementing IDs with `sequence` | ||
@@ -194,2 +175,15 @@ | ||
This approach also lets you use any additional libraries, say if you wanted to use a library to generate fake data: | ||
```js | ||
const myFakeLibrary = require('whatever-library-you-want'); | ||
const { build, perBuild } = require('@jackfranklin/test-data-bot'); | ||
const userBuilder = build('User', { | ||
fields: { | ||
name: perBuild(() => myFakeLibrary.randomName()), | ||
}, | ||
}); | ||
``` | ||
### Mapping over all the created objects with `postBuild` | ||
@@ -196,0 +190,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
0
29000
172
460
- Removed@types/faker@5.5.9
- Removed@types/lodash@4.14.178
- Removedfaker@5.5.3
- Removedlodash@4.17.21
- Removed@types/faker@5.5.9(transitive)
- Removed@types/lodash@4.14.178(transitive)
- Removedfaker@5.5.3(transitive)
- Removedlodash@4.17.21(transitive)