Skip to content

Commit f382ecd

Browse files
committed
Clean code.
1 parent 310e7e5 commit f382ecd

2 files changed

Lines changed: 68 additions & 201 deletions

File tree

image.js

Lines changed: 23 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const child = require('child_process');
3131
const exec = child.exec;
3232
const spawn = child.spawn;
3333
const Fs = require('fs');
34+
const REGEXP_SVG = /(width=\"\d+\")+|(height=\"\d+\")+/g;
3435
var middlewares = {};
3536

3637
if (!global.framework_utils)
@@ -75,10 +76,7 @@ exports.measureJPG = function(buffer) {
7576
var w = u16(buffer, o + 6);
7677
var h = u16(buffer, o + 4);
7778

78-
return {
79-
width: w,
80-
height: h
81-
};
79+
return { width: w, height: h };
8280
}
8381

8482
return null;
@@ -93,7 +91,7 @@ exports.measurePNG = function(buffer) {
9391

9492
exports.measureSVG = function(buffer) {
9593

96-
var match = buffer.toString('utf8').match(/(width=\"\d+\")+|(height=\"\d+\")+/g);
94+
var match = buffer.toString('utf8').match(REGEXP_SVG);
9795
if (!match)
9896
return;
9997

@@ -106,21 +104,11 @@ exports.measureSVG = function(buffer) {
106104
if (width > 0 && height > 0)
107105
break;
108106

109-
if (width === 0) {
110-
if (value.startsWith('width="')) {
111-
width = +value.match(/\d+/g);
112-
if (isNaN(width))
113-
width = 0;
114-
}
115-
}
107+
if (!width && value.startsWith('width="'))
108+
width = value.parseInt2();
116109

117-
if (height === 0) {
118-
if (value.startsWith('height="')) {
119-
height = +value.match(/\d+/g);
120-
if (isNaN(height))
121-
height = 0;
122-
}
123-
}
110+
if (!height && value.startsWith('height="'))
111+
height = value.parseInt2();
124112
}
125113

126114
return { width: width, height: height };
@@ -168,10 +156,7 @@ Image.prototype.measure = function(callback) {
168156
}
169157

170158
var extension = self.filename.substring(index).toLowerCase();
171-
var stream = require('fs').createReadStream(self.filename, {
172-
start: 0,
173-
end: extension === '.jpg' ? 40000 : 24
174-
});
159+
var stream = require('fs').createReadStream(self.filename, { start: 0, end: extension === '.jpg' ? 40000 : 24 });
175160

176161
stream.on('data', function(buffer) {
177162

@@ -217,9 +202,7 @@ Image.prototype.save = function(filename, callback, writer) {
217202
filename = null;
218203
}
219204

220-
if (!self.builder.length)
221-
self.minify();
222-
205+
!self.builder.length && self.minify();
223206
filename = filename || self.filename || '';
224207

225208
var command = self.cmd(!self.filename ? '-' : self.filename, filename);
@@ -233,6 +216,7 @@ Image.prototype.save = function(filename, callback, writer) {
233216
cmd = null;
234217

235218
self.clear();
219+
236220
if (!callback)
237221
return;
238222

@@ -262,9 +246,7 @@ Image.prototype.save = function(filename, callback, writer) {
262246
}
263247

264248
CLEANUP(cmd.stdin);
265-
266-
if (writer)
267-
writer(cmd.stdin);
249+
writer && writer(cmd.stdin);
268250

269251
return self;
270252
};
@@ -345,8 +327,7 @@ Image.prototype.stream = function(type, writer) {
345327
self.currentStream.pipe(cmd.stdin);
346328
}
347329

348-
if (writer)
349-
writer(cmd.stdin);
330+
writer && writer(cmd.stdin);
350331

351332
var middleware = middlewares[type];
352333
if (!middleware)
@@ -381,22 +362,13 @@ Image.prototype.cmd = function(filenameFrom, filenameTo) {
381362
return (self.isIM ? 'convert' : 'gm -convert') + ' "' + filenameFrom + '"' + ' ' + cmd + ' "' + filenameTo + '"';
382363
};
383364

384-
/*
385-
Internal function
386-
@filenameFrom {String}
387-
@filenameTo {String}
388-
return {String}
389-
*/
390365
Image.prototype.arg = function(first, last) {
391366

392367
var self = this;
393368
var arr = [];
394369

395-
if (!self.isIM)
396-
arr.push('-convert');
397-
398-
if (first)
399-
arr.push(first);
370+
!self.isIM && arr.push('-convert');
371+
first && arr.push(first);
400372

401373
self.builder.sort(function(a, b) {
402374
if (a.priority > b.priority)
@@ -418,9 +390,7 @@ Image.prototype.arg = function(first, last) {
418390
}
419391
}
420392

421-
if (last)
422-
arr.push(last);
423-
393+
last && arr.push(last);
424394
return arr;
425395
};
426396

@@ -432,7 +402,7 @@ Image.prototype.arg = function(first, last) {
432402
Image.prototype.identify = function(cb) {
433403
var self = this;
434404

435-
exec((self.isIM ? 'identify' : 'gm identify') + ' "' + self.fileName + '"', function(error, stdout, stderr) {
405+
exec((self.isIM ? 'identify' : 'gm identify') + ' "' + self.filename + '"', function(error, stdout, stderr) {
436406

437407
if (error) {
438408
cb(error, null);
@@ -441,12 +411,7 @@ Image.prototype.identify = function(cb) {
441411

442412
var arr = stdout.split(' ');
443413
var size = arr[2].split('x');
444-
var obj = {
445-
type: arr[1],
446-
width: framework_utils.parseInt(size[0]),
447-
height: framework_utils.parseInt(size[1])
448-
};
449-
414+
var obj = { type: arr[1], width: framework_utils.parseInt(size[0]), height: framework_utils.parseInt(size[1]) };
450415
cb(null, obj);
451416
});
452417

@@ -460,38 +425,20 @@ Image.prototype.$$identify = function() {
460425
};
461426
};
462427

463-
/*
464-
Append filter to filter list
465-
@key {String}
466-
@value {String}
467-
@priority {Number}
468-
return {Image}
469-
*/
470428
Image.prototype.push = function(key, value, priority) {
471429
var self = this;
472-
self.builder.push({
473-
cmd: key + (value ? ' "' + value + '"' : ''),
474-
priority: priority
475-
});
430+
self.builder.push({ cmd: key + (value ? ' "' + value + '"' : ''), priority: priority });
476431
return self;
477432
};
478433

479434
Image.prototype.output = function(type) {
480435
var self = this;
481-
482436
if (type[0] === '.')
483437
type = type.substring(1);
484-
485438
self.outputType = type;
486439
return self;
487440
};
488441

489-
/*
490-
@w {Number}
491-
@h {Number}
492-
@options {String}
493-
http://www.graphicsmagick.org/GraphicsMagick.html#details-resize
494-
*/
495442
Image.prototype.resize = function(w, h, options) {
496443
options = options || '';
497444

@@ -549,10 +496,6 @@ Image.prototype.trim = function() {
549496
return this.push('-trim +repage', 1);
550497
};
551498

552-
/*
553-
@w {Number}
554-
@h {Number}
555-
*/
556499
Image.prototype.extent = function(w, h) {
557500

558501
var self = this;
@@ -649,9 +592,9 @@ Image.prototype.quality = function(percentage) {
649592
*/
650593
Image.prototype.align = function(type) {
651594

652-
var output = '';
595+
var output;
653596

654-
switch (type.toLowerCase().replace('-', '')) {
597+
switch (type) {
655598
case 'left top':
656599
case 'top left':
657600
output = 'NorthWest';
@@ -698,20 +641,14 @@ Image.prototype.align = function(type) {
698641
break;
699642
}
700643

701-
return this.push('-gravity', output, 3);
644+
ouptut && this.push('-gravity', output, 3);
645+
return this;
702646
};
703647

704-
/*
705-
@type {String}
706-
*/
707648
Image.prototype.gravity = function(type) {
708649
return this.align(type);
709650
};
710651

711-
/*
712-
@radius {Number}
713-
http://www.graphicsmagick.org/GraphicsMagick.html#details-blur
714-
*/
715652
Image.prototype.blur = function(radius) {
716653
return this.push('-blur', radius, 10);
717654
};
@@ -720,20 +657,14 @@ Image.prototype.normalize = function() {
720657
return this.push('-normalize', null, 10);
721658
};
722659

723-
/*
724-
@deg {Number}
725-
http://www.graphicsmagick.org/GraphicsMagick.html#details-rotate
726-
*/
727660
Image.prototype.rotate = function(deg) {
728661
return this.push('-rotate', deg || 0, 8);
729662
};
730663

731-
// http://www.graphicsmagick.org/GraphicsMagick.html#details-flip
732664
Image.prototype.flip = function() {
733665
return this.push('-flip', null, 10);
734666
};
735667

736-
// http://www.graphicsmagick.org/GraphicsMagick.html#details-flop
737668
Image.prototype.flop = function() {
738669
return this.push('-flop', null, 10);
739670
};
@@ -771,22 +702,13 @@ Image.prototype.make = function(fn) {
771702
return this;
772703
};
773704

774-
/*
775-
@cmd {String}
776-
@priority {Number}
777-
*/
778705
Image.prototype.command = function(key, value, priority) {
779706
return this.push(key, value, priority || 10);
780707
};
781708

782709
exports.Image = Image;
783710
exports.Picture = Image;
784711

785-
/*
786-
Init image class
787-
@filename {String}
788-
@imageMagick {Boolean} :: default false
789-
*/
790712
exports.init = function(filename, imageMagick, width, height) {
791713
return new Image(filename, imageMagick, width, height);
792714
};
@@ -803,4 +725,4 @@ exports.middleware = function(type, fn) {
803725

804726
exports.restart = function() {
805727
middlewares = {};
806-
};
728+
};

0 commit comments

Comments
 (0)