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