Skip to content

Commit

Permalink
Cli-option to set float numbers precision. Resolves svg#99
Browse files Browse the repository at this point in the history
  • Loading branch information
GreLI committed Mar 30, 2015
1 parent cf37722 commit 60d348b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Options:
-s STRING, --string=STRING : Input SVG data string
-f FOLDER, --folder=FOLDER : Input folder, optimize and rewrite all *.svg files
-o OUTPUT, --output=OUTPUT : Output file or folder (by default the same as the input), "-" for STDOUT
-p PRECISION, --precision=PRECISION : Set number of digits in the fractional part, overrides plugins params
--config=CONFIG : Config file to extend or replace default
--disable=DISABLE : Disable plugin by name
--enable=ENABLE : Enable plugin by name
Expand Down
1 change: 1 addition & 0 deletions README.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ $ [sudo] npm install -g svgo
-s STRING, --string=STRING : Входная строка SVG
-f FOLDER, --folder=FOLDER : Входная папка, оптимизирует и перезаписывает все файлы *.svg
-o OUTPUT, --output=OUTPUT : Выходной файл или папка (совпадает с входным по умолчанию), "-" для STDOUT
-p PRECISION, --precision=PRECISION : Число цифр после запятой, переопределяет параметры плагинов
--config=CONFIG : Файл конфигурации для расширения и замены настроек
--disable=DISABLE : Выключение плагина по имени
--enable=ENABLE : Включение плагина по имени
Expand Down
28 changes: 20 additions & 8 deletions lib/svgo/coa.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = require('coa').Cmd()
.name('input').title('Input file, "-" for STDIN')
.short('i').long('input')
.val(function(val) {
return val || this.reject('Option --input must have a value.');
return val || this.reject("Option '--input' must have a value.");
})
.end()
.opt()
Expand All @@ -45,44 +45,51 @@ module.exports = require('coa').Cmd()
.name('folder').title('Input folder, optimize and rewrite all *.svg files')
.short('f').long('folder')
.val(function(val) {
return val || this.reject('Option --folder must have a value.');
return val || this.reject("Option '--folder' must have a value.");
})
.end()
.opt()
.name('output').title('Output file (by default the same as the input), "-" for STDOUT')
.name('output').title('Output file or folder (by default the same as the input), "-" for STDOUT')
.short('o').long('output')
.val(function(val) {
return val || this.reject('Option --output must have a value.');
return val || this.reject("Option '--output' must have a value.");
})
.end()
.opt()
.name('precision').title('Set number of digits in the fractional part, overrides plugins params')
.short('p').long('precision')
.val(function(val) {
return !isNaN(val) ? val : this.reject("Option '--precision' must be an integer number");
})
.end()
.opt()
.name('config').title('Config file to extend or replace default')
.long('config')
.val(function(val) {
return val || this.reject('Option --config must have a value.');
return val || this.reject("Option '--config' must have a value.");
})
.end()
.opt()
.name('disable').title('Disable plugin by name')
.long('disable')
.arr()
.val(function(val) {
return val || this.reject('Option --disable must have a value.');
return val || this.reject("Option '--disable' must have a value.");
})
.end()
.opt()
.name('enable').title('Enable plugin by name')
.long('enable')
.arr()
.val(function(val) {
return val || this.reject('Option --enable must have a value.');
return val || this.reject("Option '--enable' must have a value.");
})
.end()
.opt()
.name('datauri').title('Output as Data URI string (base64, URI encoded or unencoded)')
.long('datauri')
.val(function(val) {
return val || this.reject('Option --datauri must have one of the following values: base64, enc or unenc');
return val || this.reject("Option '--datauri' must have one of the following values: 'base64', 'enc' or 'unenc'");
})
.end()
.opt()
Expand Down Expand Up @@ -148,6 +155,11 @@ module.exports = require('coa').Cmd()

}

// --precision
if (opts.precision) {
config.floatPrecision = Math.max(0, parseInt(opts.precision));
}

// --disable
if (opts.disable) {
config = changePluginsState(opts.disable, false, config);
Expand Down
8 changes: 8 additions & 0 deletions lib/svgo/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ module.exports = function(config) {
if (config) {
defaults = extendConfig(defaults, config);
defaults.multipass = config.multipass;

if ('floatPrecision' in config) {
defaults.plugins.forEach(function(plugin) {
if (plugin.params && ('floatPrecision' in plugin.params)) {
plugin.params.floatPrecision = config.floatPrecision;
}
});
}
}

defaults.plugins = optimizePluginsArray(defaults.plugins);
Expand Down

0 comments on commit 60d348b

Please sign in to comment.