Skip to content

Commit

Permalink
updated build options
Browse files Browse the repository at this point in the history
  • Loading branch information
Na'aman Hirschfeld authored and Na'aman Hirschfeld committed Jul 5, 2021
1 parent e0a615c commit f1b115f
Show file tree
Hide file tree
Showing 7 changed files with 521 additions and 455 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@
[1.1.4]

- fixed issue with iterating Dates

[1.1.5]

- updated build options
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ you wish to begin iteration at a value other than 0, you can pass the this as a
### The .build method

To use the factory to generate an object you should call `.build`. This method is `async` so it has to be awaited.
The `.build` method accepts an optional `options` object with two optional keys:
The `.build` method accepts an optional `options` object. This object can either be a defaults objects or an object with two optional keys:

- `overrides`: either an object literal, a function returning an object literal, or a promise resolving to an object
literal. The values of the object are merged with the defaults using `Object.assign` - hence newer values passed in
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "interface-forge",
"author": "Na'aman Hirschfeld",
"version": "1.1.4",
"version": "1.1.5",
"license": "MIT",
"description": "Gracefully generate testing data using TypeScript",
"keywords": [
Expand All @@ -28,18 +28,18 @@
},
"devDependencies": {
"@types/jest": "^26.0.23",
"@typescript-eslint/eslint-plugin": "^4.26.1",
"@typescript-eslint/parser": "^4.26.1",
"eslint": "^7.28.0",
"@typescript-eslint/eslint-plugin": "^4.28.2",
"@typescript-eslint/parser": "^4.28.2",
"eslint": "^7.30.0",
"eslint-config-prettier": "^8.3.0",
"husky": ">=6",
"jest": "^27.0.4",
"husky": ">=7",
"jest": "^27.0.6",
"lint-staged": ">=11",
"prettier": "^2.3.1",
"prettier": "^2.3.2",
"rimraf": "^3.0.2",
"ts-jest": "^27.0.3",
"ts-node": "^10.0.0",
"typescript": "^4.3.2"
"typescript": "^4.3.5"
},
"lint-staged": {
"*.ts": "eslint --fix",
Expand Down
48 changes: 32 additions & 16 deletions src/type-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class TypeFactory<T> {
: Promise.resolve(this._defaults);
}

private static async parse_schema<T>(
private static async parseSchema<T>(
schema: FactorySchema<T>,
iteration: number,
): Promise<T> {
Expand All @@ -44,7 +44,7 @@ export class TypeFactory<T> {
value !== null &&
value.toString() === '[object Object]'
) {
output[key] = await TypeFactory.parse_schema(value, iteration);
output[key] = await TypeFactory.parseSchema(value, iteration);
} else {
output[key] = value;
}
Expand All @@ -53,7 +53,7 @@ export class TypeFactory<T> {
return output as T;
}

private validate_schema(schema: FactorySchema<T>): void {
private validateSchema(schema: FactorySchema<T>): void {
const missingValues: string[] = [];
Object.entries(schema).forEach(([key, value]) => {
if (value instanceof BuildArgProxy) {
Expand All @@ -69,29 +69,45 @@ export class TypeFactory<T> {
}
}

private async parseOptions(
options: FactoryBuildOptions<T> | undefined,
iteration: number,
): Promise<[overrides: FactoryOptions<Partial<T>> | undefined, factory: FactoryFunction<T> | undefined]> {
const overrides = (
options
? Reflect.has(options, 'overrides')
? Reflect.get(options, 'overrides')
: !Reflect.has(options, 'factory')
? options
: {}
: {}
) as FactoryOptions<T>;
const resolvedOverrides =
typeof overrides === 'function' ? await overrides(iteration) : overrides;
const factory =
options && Reflect.has(options, 'factory')
? (Reflect.get(options, 'factory') as FactoryFunction<T>)
: this.factory;
return [resolvedOverrides, factory];
}

resetCounter(value = 0): void {
this.counter = value;
}

async build(options?: FactoryBuildOptions<T>): Promise<T> {
const iteration = this.counter;
this.counter++;
const [overrides, factory] = await this.parseOptions(options, iteration)
const defaults = await this.defaults
const mergedSchema = Object.assign(
{},
await this.defaults,
await Promise.resolve(
typeof options?.overrides === 'function'
? options.overrides(iteration)
: options?.overrides,
),
);
this.validate_schema(mergedSchema);
const value = await TypeFactory.parse_schema<T>(
mergedSchema,
iteration,
defaults,
overrides,
);
const fn = options?.factory ?? this.factory;
return Promise.resolve(fn ? fn(value, iteration) : value);
this.validateSchema(mergedSchema);
const value = await TypeFactory.parseSchema<T>(mergedSchema, iteration);
return factory ? factory(value, iteration) : value
}

async batch(size: number, options?: FactoryBuildOptions<T>): Promise<T[]> {
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type FactorySchema<T> = {
: T[K] | Ref<T[K]> | TypeFactory<T[K]> | Generator<number, number, number> | BuildArgProxy;
};

export interface FactoryBuildOptions<T> {
export type FactoryBuildOptions<T> = {
overrides?: FactoryOptions<Partial<T>>,
factory?: FactoryFunction<T>,
}
} | FactoryOptions<Partial<T>>
9 changes: 9 additions & 0 deletions tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ describe('InterfaceFactory', () => {
});
});
it('merges options correctly when passed object literal', async () => {
const factory = new TypeFactory<ComplexObject>(defaults);
expect(
await factory.build({ name: 'newObject' }),
).toStrictEqual<ComplexObject>({
...defaults,
name: 'newObject',
});
});
it('merges options correctly when passed object literal in overrides key', async () => {
const factory = new TypeFactory<ComplexObject>(defaults);
expect(
await factory.build({ overrides: { name: 'newObject' } }),
Expand Down
Loading

0 comments on commit f1b115f

Please sign in to comment.