Skip to content

Commit e6333c5

Browse files
committed
Rewrite middleware to use utils lib
1 parent 56d90fe commit e6333c5

File tree

7 files changed

+48
-179
lines changed

7 files changed

+48
-179
lines changed

src/lib/hashingFunctions.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/lib/imageFiles.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
1-
import fs from 'fs';
21
import path from 'path';
2+
import { filePaths, fileNames } from 'avatars-utils';
33

4-
const IMAGE_DIR = path.join(__dirname, '..', 'img');
4+
const imageDir = path.join(__dirname, '..', 'img');
55

6-
const imageFiles: (path: string) => string[] = p =>
7-
fs.readdirSync(p).filter(fileName => fileName.match(/\.png$/));
6+
export const imageFilePaths = (type: string): string[] =>
7+
filePaths(path.join(imageDir, type));
88

9-
const imageDir: (type: ImageType) => string = type =>
10-
type ? path.join(IMAGE_DIR, type) : IMAGE_DIR;
9+
export const imageFileNames = (type: string): string[] =>
10+
fileNames(path.join(imageDir, type));
1111

12-
export const allNames: (type: ImageType) => string[] = type =>
13-
imageFiles(imageDir(type)).map(imageFileName =>
14-
imageFileName.replace(/\.png/, ''),
15-
);
16-
17-
export const allPaths: (type: ImageType) => string[] = type => {
18-
const dir = imageDir(type);
19-
return imageFiles(dir).map(file => path.join(dir, file));
20-
};
21-
22-
export const pathTo: (type: ImageType, name: string) => string = (type, name) =>
23-
path.join(imageDir(type), name + '.png');
12+
export const eyeImages = imageFilePaths('eyes');
13+
export const noseImages = imageFilePaths('nose');
14+
export const mouthImages = imageFilePaths('mouth');

src/lib/potato.ts

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,35 @@
1-
import { sumAndDiff } from './hashingFunctions';
2-
import { allPaths } from './imageFiles';
3-
import SlotMachine from './slotMachine';
1+
import { Hash, hashFactory, sumAndDiff } from 'avatars-utils';
2+
import { eyeImages, noseImages, mouthImages } from './imageFiles';
43

5-
const colors = [
4+
export class Potato {
5+
private colorHash: Hash<string>;
6+
private eyeHash: Hash<string>;
7+
private noseHash: Hash<string>;
8+
private mouthHash: Hash<string>;
9+
10+
constructor(
11+
colors: string[],
12+
eyes: string[],
13+
noses: string[],
14+
mouths: string[],
15+
) {
16+
this.colorHash = new Hash(colors);
17+
this.eyeHash = new Hash(eyes);
18+
this.noseHash = new Hash(noses);
19+
this.mouthHash = new Hash(mouths, hashFactory(sumAndDiff));
20+
}
21+
22+
public parts(string): Face {
23+
return {
24+
color: this.colorHash.get(string),
25+
eyes: this.eyeHash.get(string),
26+
nose: this.noseHash.get(string),
27+
mouth: this.mouthHash.get(string),
28+
};
29+
}
30+
}
31+
32+
const defaultColors = [
633
'#81bef1',
734
'#ad8bf2',
835
'#bff288',
@@ -14,22 +41,4 @@ const colors = [
1441
'#f6be5d',
1542
];
1643

17-
class Potato {
18-
constructor(
19-
private colorMachine = new SlotMachine(colors),
20-
private eyesMachine = new SlotMachine(allPaths('eyes')),
21-
private noseMachine = new SlotMachine(allPaths('nose')),
22-
private mouthMachine = new SlotMachine(allPaths('mouth'), sumAndDiff),
23-
) {}
24-
25-
parts(string): Face {
26-
return {
27-
color: this.colorMachine.pull(string),
28-
eyes: this.eyesMachine.pull(string),
29-
nose: this.noseMachine.pull(string),
30-
mouth: this.mouthMachine.pull(string),
31-
};
32-
}
33-
}
34-
35-
export default new Potato();
44+
export default new Potato(defaultColors, eyeImages, noseImages, mouthImages);

src/lib/slotMachine.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/routes/index.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Router from 'express';
22
import uuid from 'uuid';
33

4-
import { allNames, pathTo } from '../lib/imageFiles';
4+
import { imageFileNames, imageFilePaths } from '../lib/imageFiles';
55
import { combine } from '../lib/imager';
66
import potato from '../lib/potato';
77

@@ -17,7 +17,7 @@ const sendImage = ({ stdout, response }) => {
1717

1818
router.get('/list', (req, res) => {
1919
const face = {};
20-
imageTypes.forEach(type => (face[type] = allNames(type)));
20+
imageTypes.forEach(type => (face[type] = imageFileNames(type)));
2121

2222
return res.set('Content-Type', 'application/json').send({ face });
2323
});
@@ -43,16 +43,13 @@ router.get('/face/:eyes/:nose/:mouth/:color/:size?', (req, res, next) => {
4343

4444
imageTypes.forEach(type => {
4545
const requestedName = req.params[type];
46-
const names = allNames(type);
47-
let name = names[0];
46+
const paths = imageFilePaths(type);
47+
faceParts[type] =
48+
paths.find(path => !!path.match(requestedName)) || paths[0];
4849

49-
if (names.includes(requestedName)) {
50-
name = requestedName;
51-
} else if (requestedName === 'x') {
52-
name = '';
50+
if (requestedName === 'x') {
51+
faceParts[type] = '';
5352
}
54-
55-
faceParts[type] = pathTo(type, name);
5653
});
5754

5855
return combine(faceParts, req.params.size, (err, stdout) =>

test/lib/imageFiles.test.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

test/lib/slotMachine.test.ts

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)