Skip to content

Commit 9fc925d

Browse files
authored
Merge pull request #96 from adorableio/use-utils
Use avatars-utils
2 parents 576d6e2 + 7fc99c1 commit 9fc925d

File tree

10 files changed

+81
-211
lines changed

10 files changed

+81
-211
lines changed

package-lock.json

Lines changed: 9 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"dist"
2323
],
2424
"dependencies": {
25+
"avatars-utils": "^0.0.3",
2526
"express": "^4.16.4",
2627
"gm": "^1.16.0",
2728
"uuid": "^3.3.2"
@@ -38,7 +39,7 @@
3839
"@types/serve-favicon": "^2.2.30",
3940
"chai": "^4.2.0",
4041
"concurrently": "^4.1.0",
41-
"mocha": "^6.1.3",
42+
"mocha": "^6.1.4",
4243
"onchange": "^5.2.0",
4344
"prettier": "^1.17.0",
4445
"serve-favicon": "^2.5.0",

src/lib/FaceFactory.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Hash, hashFactory, sumAndDiff } from 'avatars-utils';
2+
import { eyeImages, noseImages, mouthImages } from './imageFiles';
3+
4+
export class FaceFactory {
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 create(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 = [
33+
'#81bef1',
34+
'#ad8bf2',
35+
'#bff288',
36+
'#de7878',
37+
'#a5aac5',
38+
'#6ff2c5',
39+
'#f0da5e',
40+
'#eb5972',
41+
'#f6be5d',
42+
];
43+
44+
export default new FaceFactory(
45+
defaultColors,
46+
eyeImages,
47+
noseImages,
48+
mouthImages,
49+
);

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: 0 additions & 35 deletions
This file was deleted.

src/lib/slotMachine.ts

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

src/routes/index.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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';
6-
import potato from '../lib/potato';
6+
import FaceFactory from '../lib/FaceFactory';
77

88
const imageTypes: ImageType[] = ['eyes', 'nose', 'mouth'];
99

@@ -17,23 +17,23 @@ 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
});
2424

2525
router.get('/:size?/random', (req, res) => {
26-
const faceParts = potato.parts(uuid.v4());
26+
const face = FaceFactory.create(uuid.v4());
2727

28-
return combine(faceParts, req.params.size, (err, stdout) =>
28+
return combine(face, req.params.size, (err, stdout) =>
2929
sendImage({ stdout, response: res }),
3030
);
3131
});
3232

3333
router.get('/:size?/:id', (req, res, next) => {
34-
const faceParts = potato.parts(req.params.id);
34+
const face = FaceFactory.create(req.params.id);
3535

36-
return combine(faceParts, req.params.size, (err, stdout) =>
36+
return combine(face, req.params.size, (err, stdout) =>
3737
sendImage({ stdout, response: res }),
3838
);
3939
});
@@ -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)