diff --git a/examples/test.js b/examples/test.js index 3d8ecf02..6dca6ab3 100644 --- a/examples/test.js +++ b/examples/test.js @@ -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); diff --git a/lib/svgo.js b/lib/svgo.js index c593023f..4c216790 100644 --- a/lib/svgo.js +++ b/lib/svgo.js @@ -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); +SVGO.prototype.optimize = function(svgstr) { + return new Promise((resolve, reject) => { + if (this.config.error) { + reject(this.config.error); + return; + } - var _this = this, - config = this.config, - maxPassCount = config.multipass ? 10 : 1, - counter = 0, - prevResultSize = Number.POSITIVE_INFINITY, - optimizeOnceCallback = function(svgjs) { + 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 (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); + 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; @@ -63,7 +61,6 @@ SVGO.prototype._optimizeOnce = function(svgstr, callback) { svgjs = PLUGINS(svgjs, config.plugins); callback(JS2SVG(svgjs, config.js2svg)); - }); }; @@ -74,7 +71,5 @@ SVGO.prototype._optimizeOnce = function(svgstr, callback) { * @returns {JSAPI} content item */ SVGO.prototype.createContentItem = function(data) { - return new JSAPI(data); - }; diff --git a/lib/svgo/coa.js b/lib/svgo/coa.js index f58742e8..12177f5a 100644 --- a/lib/svgo/coa.js +++ b/lib/svgo/coa.js @@ -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); } @@ -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) { @@ -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; @@ -482,7 +472,7 @@ function optimizeFolder(dir, config, output) { optimizeFile(files[i]); } } - }); + }, error => console.error(error)); }); } //move on to the next file diff --git a/test/plugins/_index.js b/test/plugins/_index.js index 508832f4..7ce3d52a 100644 --- a/test/plugins/_index.js +++ b/test/plugins/_index.js @@ -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(); }); diff --git a/test/svgo/_index.js b/test/svgo/_index.js index 1d218a21..053f3998 100644 --- a/test/svgo/_index.js +++ b/test/svgo/_index.js @@ -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(); });