Skip to content

Commit

Permalink
allow to do a multiple optimizations with one init (close #25)
Browse files Browse the repository at this point in the history
  • Loading branch information
deepsweet committed Oct 26, 2012
1 parent aa3fa83 commit 50a8c8a
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 53 deletions.
5 changes: 3 additions & 2 deletions lib/coa.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ module.exports = require('coa').Cmd()
.then(function(svg) {
startBytes = Buffer.byteLength(svg, 'utf-8');

return SVGO(svg, { coa: options });
return new SVGO({ coa: options }).optimize(svg);
})
.then(function(svgmin) {
endTime = Date.now();
Expand Down Expand Up @@ -166,7 +166,8 @@ module.exports = require('coa').Cmd()
}
}

});
})
.end();

});

Expand Down
6 changes: 5 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ module.exports = function(options) {
return readConfig(defaultConfigPath)
.then(function(defaultConfig) {

return extend(true, defaultConfig, options);
if (options) {
return extend(true, defaultConfig, options);
}

return defaultConfig;

});

Expand Down
7 changes: 4 additions & 3 deletions lib/js2svg.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var INHERIT = require('inherit');
var INHERIT = require('inherit'),
extend = require('./tools').extend;

/**
* Convert SVG-as-JS object to SVG (XML) string.
Expand Down Expand Up @@ -27,11 +28,11 @@ var Converter = INHERIT(/** @lends Nodes.prototype */{
__constructor: function(config) {

/**
* Converter config.
* Shallow copy of converter config.
*
* @type {Object}
*/
this.config = config;
this.config = extend({}, config);

/**
* Current indent level hack.
Expand Down
6 changes: 5 additions & 1 deletion lib/svg2js.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var UTIL = require('util'),
Q = require('q'),
SAX = require('sax'),
JSAPI = require('./jsAPI');
JSAPI = require('./jsAPI'),
extend = require('./tools').extend;

/**
* Convert SVG (XML) string to SVG-as-JS object.
Expand All @@ -14,6 +15,9 @@ var UTIL = require('util'),
*/
module.exports = function(svgdata, config) {

// shallow copy
config = extend({}, config);

var strict = config.strict;
delete config.strict;

Expand Down
60 changes: 42 additions & 18 deletions lib/svgo.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,59 @@
var CONFIG = require('./config'),
SVG2JS = require('./svg2js'),
PLUGINS = require('./plugins'),
JS2SVG = require('./js2svg');

/**
* SVGO is a Nodejs-based tool for optimizing SVG vector graphics files.
*
* @see http://deepsweet.github.com/svgo/
*
* @module svgo
*
* @param {String} svgdata input data
* @param {Object} [options] options
* @return {String} output data deferred promise
*
* @author Kir Belevich <kir@soulshine.in> (https://github.com/deepsweet)
* @copyright © 2012 Kir Belevich
* @license MIT https://raw.github.com/deepsweet/svgo/master/LICENSE
*/
module.exports = function(svgdata, options) {

return CONFIG(options)
.then(function(config) {
var INHERIT = require('inherit'),
CONFIG = require('./config'),
SVG2JS = require('./svg2js'),
PLUGINS = require('./plugins'),
JS2SVG = require('./js2svg');

/**
* @class SVGO.
*/
module.exports = INHERIT(/** @lends SVGO.prototype */{

/**
* @param {Object} [config] config to extend
*
* @constructs
*
* @private
*/
__constructor: function(config) {

this.config = CONFIG(config);

},

/**
* Main optimize function.
*
* @param {String} svgdata input data
* @return {String} output data deferred promise
*/
optimize: function(svgdata) {

return this.config
.then(function(config) {

return SVG2JS(svgdata, config.svg2js)
.then(function(jsdata) {

return SVG2JS(svgdata, config.svg2js)
.then(function(jsdata) {
return JS2SVG(PLUGINS(jsdata, config.plugins), config.js2svg);

return JS2SVG(PLUGINS(jsdata, config.plugins), config.js2svg);
});

});
});

});
}

};
});
60 changes: 32 additions & 28 deletions test/plugins/_index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
var QFS = require('q-fs'),
var INHERIT = require('inherit'),
QFS = require('q-fs'),
FS = require('fs'),
PATH = require('path'),
CONFIG = require('../../lib/config'),
SVGO = require('../../lib/svgo'),
regFilename = /^(.*)\.(\d+)\.orig\.svg$/;

var MySVGO = INHERIT(require('../../lib/svgo'), {

enableOnlyOne: function(name) {

this.config = this.config.then(function(config) {

for (var type in config.plugins) {
config.plugins[type].forEach(function(plugin) {
plugin.active = plugin.name === name;
});
}

return config;

});

}

}),
mySVGO = new MySVGO({
js2svg: {
pretty: true
}
});

describe('plugins tests', function() {

FS.readdirSync(__dirname).forEach(function(file) {
Expand Down Expand Up @@ -37,12 +61,11 @@ describe('plugins tests', function() {
});

function getResult(name, index) {
return prepareConfig(name)
.then(function(config) {
return readFile(name + '.' + index + '.orig.svg')
.then(function(input) {
return SVGO(input.toString(), config);
});
return readFile(name + '.' + index + '.orig.svg')
.then(function(input) {
mySVGO.enableOnlyOne(name);

return mySVGO.optimize(input.toString());
})
.then(function(min) {
return readFile(name + '.' + index +'.should.svg')
Expand All @@ -52,25 +75,6 @@ function getResult(name, index) {
});
}

function prepareConfig(name) {

return CONFIG()
.then(function(config) {
for (var type in config.plugins) {
config.plugins[type].forEach(function(plugin) {
if (plugin.name !== name) {
plugin.active = false;
}
});
}

config.js2svg.pretty = true;

return config;
});

}

function readFile(path) {
return QFS.read(PATH.resolve(__dirname, path));
}

0 comments on commit 50a8c8a

Please sign in to comment.