1
0
mirror of https://github.com/svg/svgo.git synced 2025-07-29 20:21:14 +03:00

lib/svgo: global API refactoring (close #37); temporarily remove PhantomJS and --test (close #38)

This commit is contained in:
deepsweet
2012-11-09 18:54:00 +02:00
parent 172b7ea296
commit 1b296adc7e
11 changed files with 223 additions and 232 deletions

View File

@ -11,10 +11,14 @@
*/
var INHERIT = require('inherit'),
Q = require('q'),
FS = require('fs'),
PATH = require('path'),
CONFIG = require('./svgo/config'),
SVG2JS = require('./svgo/svg2js'),
PLUGINS = require('./svgo/plugins'),
JS2SVG = require('./svgo/js2svg');
JS2SVG = require('./svgo/js2svg'),
decodeSVGDatauri = require('./svgo/tools').decodeSVGDatauri;
/**
* @class SVGO.
@ -34,27 +38,61 @@ module.exports = INHERIT(/** @lends SVGO.prototype */{
},
/**
* Main optimize function.
*
* @param {String} svgdata input data
*
* @return {String} output data deferred promise
*/
optimize: function(svgdata) {
fromString: function(str) {
str = decodeSVGDatauri(str);
return this.config
.then(function(config) {
return SVG2JS(svgdata, config.svg2js)
return SVG2JS(str, config.svg2js)
.then(function(jsdata) {
return JS2SVG(PLUGINS(jsdata, config.plugins), config.js2svg);
var out = JS2SVG(PLUGINS(jsdata, config.plugins), config.js2svg);
out.info.startBytes = Buffer.byteLength(str, 'utf-8');
out.info.endBytes = Buffer.byteLength(out.data, 'utf-8');
return out;
});
});
},
fromStream: function(stream) {
var deferred = Q.defer(),
inputData = [],
self = this;
stream.pause();
stream
.on('data', function(chunk) {
inputData.push(chunk);
})
.once('end', function() {
deferred.resolve(inputData.join());
})
.resume();
return deferred.promise
.then(function(str) {
return self.fromString(str);
});
},
fromFile: function(path) {
path = PATH.resolve(__dirname, path);
return this.fromStream(FS.createReadStream(path, { encoding: 'utf8' }));
}
});