Skip to content

Commit ac25eba

Browse files
committed
Create basic factory function
1 parent faab014 commit ac25eba

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

source/main.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { test } from 'node:test';
2+
import assert from 'node:assert';
3+
import { createFactory } from './main.ts';
4+
5+
type Person = {
6+
readonly name: string;
7+
};
8+
9+
await test('build() returns the given factory object as is', () => {
10+
const factory = createFactory<Person>(() => {
11+
return {
12+
name: 'John Doe'
13+
};
14+
});
15+
16+
const actual = factory.build();
17+
18+
assert.deepStrictEqual(actual, { name: 'John Doe' });
19+
});

source/main.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1-
export function main(): string {
2-
return '';
1+
type ObjectoryFactory<ObjectShape> = {
2+
readonly build: () => ObjectShape;
3+
};
4+
5+
type GeneratorFunction<ObjectShape> = () => ObjectShape;
6+
7+
export function createFactory<ObjectShape extends Record<string, unknown>>(
8+
generatorFunction: GeneratorFunction<ObjectShape>
9+
): ObjectoryFactory<ObjectShape> {
10+
return {
11+
build: generatorFunction
12+
};
313
}

0 commit comments

Comments
 (0)