Skip to content

Commit 1353882

Browse files
author
Nejc
committed
feat: factory
1 parent 37004ea commit 1353882

File tree

6 files changed

+57
-2
lines changed

6 files changed

+57
-2
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"react/jsx-indent": "off",
5959
"jsx-a11y/label-has-for": "off",
6060
"jsx-a11y/label-has-associated-control": "off",
61-
"@typescript-eslint/no-non-null-assertion": "off"
61+
"@typescript-eslint/no-non-null-assertion": "off",
62+
"no-useless-constructor": "off"
6263
}
6364
}

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
"titleBar.activeForeground": "#ffffff", // change this color!
77
"titleBar.inactiveForeground": "#ffffff" // change this color!
88
},
9-
"editor.fontSize": 13
9+
"editor.fontSize": 13,
10+
"jestrunner.jestCommand": "npm run test:nocov"
1011
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"lint": "eslint ./src/**/*.{js,jsx,ts,tsx}",
3535
"lint:fix": "npm run lint --fix",
3636
"test": "npm run lint && jest",
37+
"test:nocov": "jest --coverage=false",
3738
"test:cov": "jest --coverage --colors && jest-coverage-badges",
3839
"test:build": "jest --coverage --colors --silent && jest-coverage-badges",
3940
"test:watch": "jest --watch",

src/patterns/factory/index.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import AlgorithmFactory, {BubbleSort, QuickSort} from './';
2+
3+
describe('Factory', () => {
4+
it('should create an algorithm', () => {
5+
const factory = new AlgorithmFactory();
6+
7+
const algorithm = factory.createAlgorithm('bubble-sort', {values: [1, 5, 2]});
8+
expect(algorithm instanceof BubbleSort).toBeTruthy();
9+
10+
const algorithm2 = factory.createAlgorithm('quick-sort', {
11+
values: [1, 5, 2],
12+
type: 'test',
13+
});
14+
expect(algorithm2 instanceof QuickSort).toBeTruthy();
15+
16+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
17+
const algorithm3 = factory.createAlgorithm('missing' as any, {values: [1, 5, 2]});
18+
expect(algorithm3).toBeUndefined();
19+
});
20+
});

src/patterns/factory/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
export class BubbleSort {
3+
constructor(public values: number[]) {}
4+
}
5+
6+
export class QuickSort {
7+
constructor(public values: number[], public type: string) {}
8+
}
9+
10+
// Algorithm Factory
11+
class AlgorithmFactory {
12+
createAlgorithm(
13+
type: 'bubble-sort' | 'quick-sort',
14+
props: Record<string, any>,
15+
): BubbleSort | QuickSort | undefined {
16+
switch (type) {
17+
case 'bubble-sort':
18+
return new BubbleSort(props.values);
19+
case 'quick-sort':
20+
return new QuickSort(props.values, props.type);
21+
default:
22+
// noop
23+
return undefined;
24+
}
25+
}
26+
}
27+
28+
export default AlgorithmFactory;

src/patterns/readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
https://www.dofactory.com/javascript/design-patterns/singleton
2+
https://loredanacirstea.github.io/es6-design-patterns/
3+
https://www.velotio.com/engineering-blog/design-patterns-in-es6
4+
https://www.javascriptjanuary.com/blog/writing-maintainable-and-readable-javascript-design-patterns

0 commit comments

Comments
 (0)