mirror of
https://github.com/svg/svgo.git
synced 2025-07-29 20:21:14 +03:00
Promisify SVGO
This commit is contained in:
@ -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);
|
||||
|
||||
|
53
lib/svgo.js
53
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);
|
||||
|
||||
};
|
||||
|
@ -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
|
||||
|
@ -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();
|
||||
});
|
||||
|
@ -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();
|
||||
});
|
||||
|
Reference in New Issue
Block a user