Skip to content

Commit 9a22139

Browse files
committed
Lint existing codebase
Mostly converting to arrow functions and fixing some minor stuff. No major changes.
1 parent f71b34e commit 9a22139

File tree

10 files changed

+156
-138
lines changed

10 files changed

+156
-138
lines changed

src/lib/hashingFunctions.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ const _multiplication = (a, b) => {
1010
return a * b;
1111
};
1212

13-
export const sum = (array) => {
13+
export const sum = array => {
1414
return array.reduce(_addition, 0);
1515
};
1616

17-
export const sumAndDiff = (array) => {
18-
return array.reduce(function(prev, curr, index) {
17+
export const sumAndDiff = array => {
18+
return array.reduce((prev, curr, index) => {
1919
if (index % 2 === 0) {
2020
return _addition(prev, curr);
2121
} else {
@@ -24,6 +24,6 @@ export const sumAndDiff = (array) => {
2424
}, 0);
2525
};
2626

27-
export const product = (array) => {
27+
export const product = array => {
2828
return array.reduce(_multiplication, 1);
2929
};

src/lib/imageFiles.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,27 @@ import path from 'path';
44
const DEFAULT_IMAGE_DIR = path.join(__dirname, '..', 'img');
55

66
export const dirFor = (type, imageDir = DEFAULT_IMAGE_DIR) => {
7-
if (type) return path.join(imageDir, type);
8-
else return imageDir;
7+
if (type) {
8+
return path.join(imageDir, type);
9+
}
10+
11+
return imageDir;
912
};
1013

1114
export const allNames = (type, imageDir = DEFAULT_IMAGE_DIR) => {
12-
return fs.readdirSync(dirFor(type, imageDir)).filter(function(imageFileName) {
13-
return imageFileName.match(/\.png/);
14-
}).map(function(imageFileName) {
15-
return imageFileName.replace(/\.png/, '');
16-
});
15+
return fs
16+
.readdirSync(dirFor(type, imageDir))
17+
.filter(imageFileName => {
18+
return imageFileName.match(/\.png/);
19+
})
20+
.map(imageFileName => {
21+
return imageFileName.replace(/\.png/, '');
22+
});
1723
};
1824

1925
export const allPaths = (type, imageDir = DEFAULT_IMAGE_DIR) => {
20-
var dir;
21-
dir = dirFor(type, imageDir);
22-
return allNames(type, imageDir).map(function(name) {
23-
return path.join(dir, name + '.png');
24-
});
26+
const dir = dirFor(type, imageDir);
27+
return allNames(type, imageDir).map(name => path.join(dir, name + '.png'));
2528
};
2629

2730
export const pathFor = (type, fileName, imageDir = DEFAULT_IMAGE_DIR) => {

src/lib/imager.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,49 @@
1-
import gm from 'gm';
1+
import gm from 'gm';
22
const imageMagick = gm.subClass({ imageMagick: true });
33

44
const minSize = 40;
55
const maxSize = 400;
66

7-
const _clamp = (num) => {
7+
const _clamp = num => {
88
return Math.min(Math.max(num, minSize), maxSize);
99
};
1010

11-
const _parseSize = (size) => {
11+
const _parseSize = size => {
1212
const ref = size.split('x');
1313
const width = ref[0];
1414
let height = ref[1];
1515

16-
if (height === null) height = width;
16+
if (height === null) {
17+
height = width;
18+
}
1719

1820
return {
1921
width: _clamp(width),
20-
height: _clamp(height)
22+
height: _clamp(height),
2123
};
2224
};
2325

24-
export const combine = (face, size, callback) => {
25-
if (size) { size = _parseSize(size); }
26-
else {
27-
size = { width: maxSize, height: maxSize };
28-
}
26+
export const combine = (face, _size, callback) => {
27+
const size = _size ? _parseSize(_size) : { width: maxSize, height: maxSize };
2928

3029
imageMagick('')
31-
.quality(0)
32-
.in(face.eyes)
33-
.in(face.nose)
34-
.in(face.mouth)
35-
.background(face.color)
36-
.mosaic()
37-
.resize(size.width, size.height)
38-
.trim()
39-
.gravity('Center')
40-
.extent(size.width, size.height)
41-
.stream('png', callback);
30+
.quality(0)
31+
.in(face.eyes)
32+
.in(face.nose)
33+
.in(face.mouth)
34+
.background(face.color)
35+
.mosaic()
36+
.resize(size.width, size.height)
37+
.trim()
38+
.gravity('Center')
39+
.extent(size.width, size.height)
40+
.stream('png', callback);
4241
};
4342

4443
export const resize = (imagePath, size, callback) => {
4544
size = _parseSize(size);
4645

4746
imageMagick(imagePath)
48-
.resize(size.width, size.height)
49-
.stream('png', callback);
47+
.resize(size.width, size.height)
48+
.stream('png', callback);
5049
};

src/lib/potato.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ const colors = [
1212
'#6ff2c5',
1313
'#f0da5e',
1414
'#eb5972',
15-
'#f6be5d'
15+
'#f6be5d',
1616
];
1717

1818
class Potato {
1919
constructor(
2020
private colorMachine = new SlotMachine(colors),
2121
private eyesMachine = new SlotMachine(allPaths('eyes')),
2222
private noseMachine = new SlotMachine(allPaths('nose')),
23-
private mouthMachine = new SlotMachine(allPaths('mouth'), sumAndDiff)
23+
private mouthMachine = new SlotMachine(allPaths('mouth'), sumAndDiff),
2424
) {}
2525

2626
// Construct Faces Parts
@@ -29,7 +29,7 @@ class Potato {
2929
color: this.colorMachine.pull(string),
3030
eyes: this.eyesMachine.pull(string),
3131
nose: this.noseMachine.pull(string),
32-
mouth: this.mouthMachine.pull(string)
32+
mouth: this.mouthMachine.pull(string),
3333
};
3434
}
3535
}

src/routes/avatars.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,44 @@ const partTypes = ['eyes', 'nose', 'mouth'];
1010

1111
const router = Router();
1212

13-
router.param('id', function(req, res, next, id) {
14-
var faceParts;
15-
faceParts = potato.parts(id);
16-
//@ts-ignore
13+
router.param('id', (req, res, next, id) => {
14+
const faceParts = potato.parts(id);
15+
// @ts-ignore
1716
req.faceParts = faceParts;
1817
return next();
1918
});
2019

21-
router.get('/list', function(req, res) {
22-
let response = { face: {} };
20+
router.get('/list', (req, res) => {
21+
const response = { face: {} };
2322

24-
partTypes.forEach(function(type) {
23+
partTypes.forEach(type => {
2524
response.face[type] = allNames(type);
2625
});
2726

2827
return res.set('Content-Type', 'application/json').send(response);
2928
});
3029

31-
router.get('/:size?/random', function(req, res) {
32-
var faceParts;
33-
faceParts = potato.parts(uuid.v4());
34-
//@ts-ignore
30+
router.get('/:size?/random', (req, res) => {
31+
const faceParts = potato.parts(uuid.v4());
32+
// @ts-ignore
3533
req.faceParts = faceParts;
3634

37-
return combine(faceParts, req.params.size, function(err, stdout) {
35+
return combine(faceParts, req.params.size, (err, stdout) => {
3836
return common.sendImage(err, stdout, req, res);
3937
});
4038
});
4139

42-
router.get('/:size?/:id', function(req, res, next) {
43-
//@ts-ignore
44-
return combine(req.faceParts, req.params.size, function(err, stdout) {
40+
router.get('/:size?/:id', (req, res, next) => {
41+
// @ts-ignore
42+
return combine(req.faceParts, req.params.size, (err, stdout) => {
4543
return common.sendImage(err, stdout, req, res, next);
4644
});
4745
});
4846

49-
router.get('/face/:eyes/:nose/:mouth/:color/:size?', function(req, res, next) {
50-
let faceParts = { color: '#' + req.params.color };
47+
router.get('/face/:eyes/:nose/:mouth/:color/:size?', (req, res, next) => {
48+
const faceParts = { color: '#' + req.params.color };
5149

52-
partTypes.forEach(function(type) {
50+
partTypes.forEach(type => {
5351
const possibleFileNames = allNames(type);
5452
const requestedFileName = req.params[type];
5553

@@ -65,7 +63,7 @@ router.get('/face/:eyes/:nose/:mouth/:color/:size?', function(req, res, next) {
6563
faceParts[type] = pathFor(type, fileName);
6664
});
6765

68-
return combine(faceParts, req.params.size, function(err, stdout) {
66+
return combine(faceParts, req.params.size, (err, stdout) => {
6967
return common.sendImage(err, stdout, req, res, next);
7068
});
7169
});

src/routes/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const common = {
33
res.setHeader('Expires', new Date(Date.now() + 604800000));
44
res.setHeader('Content-Type', 'image/png');
55
stdout.pipe(res);
6-
}
6+
},
77
};
88

99
export default common;

test/integration.test.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,88 +5,88 @@ import webserver from './server';
55

66
const im = subClass({ imageMagick: true });
77

8-
const parseImage = function(res, callback) {
8+
const parseImage = (res, callback) => {
99
res.setEncoding('binary');
1010
res.data = '';
1111

12-
res.on('data', function(chunk) {
12+
res.on('data', chunk => {
1313
res.data += chunk;
1414
});
1515

16-
res.on('end', function() {
16+
res.on('end', () => {
1717
callback(null, new Buffer(res.data, 'binary'));
1818
});
1919
};
2020

21-
describe('routing', function() {
22-
var request;
21+
describe('routing', () => {
22+
let request;
2323

24-
beforeEach(function() {
24+
beforeEach(() => {
2525
request = supertest(webserver);
2626
});
2727

28-
describe('v2 avatar request', function() {
29-
it('responds with an image', function(done) {
28+
describe('v2 avatar request', () => {
29+
it('responds with an image', done => {
3030
request
3131
.get('/avatars/abott')
3232
.expect('Content-Type', /image/)
3333
.end(done);
3434
});
3535

36-
it('can resize an image', function(done) {
36+
it('can resize an image', done => {
3737
request
3838
.get('/avatars/220/abott')
3939
.parse(parseImage)
40-
.end(function(err, res) {
41-
im(res.body).size(function(_, size) {
40+
.end((err, res) => {
41+
im(res.body).size((_, size) => {
4242
expect(size).to.eql({ height: 220, width: 220 });
4343
done();
4444
});
4545
});
4646
});
4747

48-
it('can manually compose an image', function(done) {
48+
it('can manually compose an image', done => {
4949
request
5050
.get('/avatars/face/eyes1/nose4/mouth11/bbb')
5151
.expect(200)
5252
.expect('Content-Type', /image/)
5353
.end(done);
5454
});
5555

56-
it('can manually compose an image with a custom size', function(done) {
56+
it('can manually compose an image with a custom size', done => {
5757
request
5858
.get('/avatars/face/eyes1/nose4/mouth11/bbb/50')
5959
.expect(200)
6060
.expect('Content-Type', /image/)
6161
.parse(parseImage)
62-
.end(function(err, res) {
63-
im(res.body).size(function(_, size) {
62+
.end((err, res) => {
63+
im(res.body).size((_, size) => {
6464
expect(size).to.eql({ height: 50, width: 50 });
6565
done();
6666
});
6767
});
6868
});
6969
});
7070

71-
describe('v2 avatar list requests', function() {
72-
it('responds with json', function(done) {
71+
describe('v2 avatar list requests', () => {
72+
it('responds with json', done => {
7373
request
7474
.get('/avatars/list')
7575
.expect('Content-Type', /json/)
7676
.end(done);
7777
});
7878

79-
it('responds with a list of possible face parts', function(done) {
80-
request.get('/avatars/list').end(function(err, res) {
79+
it('responds with a list of possible face parts', done => {
80+
request.get('/avatars/list').end((err, res) => {
8181
const faceParts = res.body.face;
8282
expect(faceParts).to.have.keys('eyes', 'mouth', 'nose');
8383
done();
8484
});
8585
});
8686
});
8787

88-
describe('v2 avatar random requests', function() {
89-
it('can randomly generate a new avatar', function(done) {
88+
describe('v2 avatar random requests', () => {
89+
it('can randomly generate a new avatar', done => {
9090
const getRandom = () =>
9191
request
9292
.get('/avatars/random')
@@ -96,9 +96,9 @@ describe('routing', function() {
9696

9797
getRandom().end((_, r1) => {
9898
getRandom().end((_, r2) => {
99-
//@ts-ignore
99+
// @ts-ignore
100100
im(r1.body).identify('%#', (_, id1) => {
101-
//@ts-ignore
101+
// @ts-ignore
102102
im(r2.body).identify('%#', (_, id2) => {
103103
expect(id1).not.to.equal(id2);
104104
done();
@@ -108,14 +108,14 @@ describe('routing', function() {
108108
});
109109
});
110110

111-
it('supports a custom size parameter', function(done) {
111+
it('supports a custom size parameter', done => {
112112
request
113113
.get('/avatars/50/random')
114114
.expect(200)
115115
.expect('Content-Type', /image/)
116116
.parse(parseImage)
117-
.end(function(err, res) {
118-
im(res.body).size(function(_, size) {
117+
.end((err, res) => {
118+
im(res.body).size((_, size) => {
119119
expect(size).to.eql({ height: 50, width: 50 });
120120
done();
121121
});

0 commit comments

Comments
 (0)