Skip to content

Commit

Permalink
Promisify SVGO
Browse files Browse the repository at this point in the history
  • Loading branch information
GreLI committed Mar 25, 2017
1 parent e4be4bb commit d74f6a0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 50 deletions.
2 changes: 1 addition & 1 deletion examples/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ FS.readFile(filepath, 'utf8', function(err, data) {
throw err;
}

svgo.optimize(data, function(result) {
svgo.optimize(data).then(function(result) {

console.log(result);

Expand Down
57 changes: 26 additions & 31 deletions lib/svgo.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,42 @@ var CONFIG = require('./svgo/config.js'),
JS2SVG = require('./svgo/js2svg.js');

var SVGO = module.exports = function(config) {

this.config = CONFIG(config);

};

SVGO.prototype.optimize = function(svgstr, callback) {
if (this.config.error) return callback(this.config);

var _this = this,
config = this.config,
maxPassCount = config.multipass ? 10 : 1,
counter = 0,
prevResultSize = Number.POSITIVE_INFINITY,
optimizeOnceCallback = function(svgjs) {

if (svgjs.error) {
callback(svgjs);
return;
}

if (++counter < maxPassCount && svgjs.data.length < prevResultSize) {
prevResultSize = svgjs.data.length;
_this._optimizeOnce(svgjs.data, optimizeOnceCallback);
} else {
callback(svgjs);
}

};

_this._optimizeOnce(svgstr, optimizeOnceCallback);
SVGO.prototype.optimize = function(svgstr) {
return new Promise((resolve, reject) => {
if (this.config.error) {
reject(this.config.error);
return;
}

var config = this.config,
maxPassCount = config.multipass ? 10 : 1,
counter = 0,
prevResultSize = Number.POSITIVE_INFINITY,
optimizeOnceCallback = (svgjs) => {
if (svgjs.error) {
reject(svgjs.error);
return;
}

if (++counter < maxPassCount && svgjs.data.length < prevResultSize) {
prevResultSize = svgjs.data.length;
this._optimizeOnce(svgjs.data, optimizeOnceCallback);
} else {
resolve(svgjs);
}
};

this._optimizeOnce(svgstr, optimizeOnceCallback);
});
};

SVGO.prototype._optimizeOnce = function(svgstr, callback) {
var config = this.config;

SVG2JS(svgstr, function(svgjs) {

if (svgjs.error) {
callback(svgjs);
return;
Expand All @@ -63,7 +61,6 @@ SVGO.prototype._optimizeOnce = function(svgstr, callback) {
svgjs = PLUGINS(svgjs, config.plugins);

callback(JS2SVG(svgjs, config.js2svg));

});
};

Expand All @@ -74,7 +71,5 @@ SVGO.prototype._optimizeOnce = function(svgstr, callback) {
* @returns {JSAPI} content item
*/
SVGO.prototype.createContentItem = function(data) {

return new JSAPI(data);

};
18 changes: 4 additions & 14 deletions lib/svgo/coa.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,7 @@ function optimizeFromString(svgstr, config, datauri, input, output) {
outBytes,
svgo = new SVGO(config);

svgo.optimize(svgstr, function(result) {
if (result.error) {
console.error(result.error);
return;
}

svgo.optimize(svgstr).then(function(result) {
if (datauri) {
result.data = encodeSVGDatauri(result.data, datauri);
}
Expand All @@ -314,7 +309,7 @@ function optimizeFromString(svgstr, config, datauri, input, output) {

saveFileAndPrintInfo(config, result.data, output, inBytes, outBytes, time);
}
});
}, error => console.error(error));
}

function saveFileAndPrintInfo(config, data, path, inBytes, outBytes, time) {
Expand Down Expand Up @@ -439,12 +434,7 @@ function optimizeFolder(dir, config, output) {
inBytes = Buffer.byteLength(data, 'utf8'),
outBytes;

svgo.optimize(data, function(result) {
if (result.error) {
console.error(result.error);
return;
}

svgo.optimize(data).then(function(result) {
outBytes = Buffer.byteLength(result.data, 'utf8');
time = Date.now() - startTime;

Expand Down Expand Up @@ -482,7 +472,7 @@ function optimizeFolder(dir, config, output) {
optimizeFile(files[i]);
}
}
});
}, error => console.error(error));
});
}
//move on to the next file
Expand Down
5 changes: 2 additions & 3 deletions test/plugins/_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ describe('plugins tests', function() {
js2svg : { pretty: true }
});

svgo.optimize(orig, function(result) {

//FIXME: results.data has a '\n' at the end while it should not
svgo.optimize(orig).then(function(result) {
//FIXME: results.data has a '\n' at the end while it should not
normalize(result.data).should.be.equal(should);
done();
});
Expand Down
2 changes: 1 addition & 1 deletion test/svgo/_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('indentation', function() {
js2svg : { pretty: true, indent: 2 }
});

svgo.optimize(orig, function(result) {
svgo.optimize(orig).then(function(result) {
normalize(result.data).should.be.equal(should);
done();
});
Expand Down

0 comments on commit d74f6a0

Please sign in to comment.