Skip to content

Commit

Permalink
new plugin: decrease accuracy of floating-point numbers (close svg#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
deepsweet committed Nov 29, 2012
1 parent 02f04d9 commit 67daff2
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .svgo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ plugins:
active: true
type: perItem

- name: roundNumericValues
active: true
type: perItem
params:
floatPrecision: 3
leadingZero: true

- name: removeUnknownsAndDefaults
active: true
type: perItem
Expand Down
33 changes: 26 additions & 7 deletions lib/svgo/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,7 @@ exports.cleanupOutData = function(data, params) {
// 0.5 → .5
// -0.5 → -.5
if (params.leadingZero) {
if (item > 0 && item < 1) {
item = ('' + item).slice(1);
}

if (item < 0 && item > -1) {
item = '-' + ('' + item).slice(2);
}
item = removeLeadingZero(item);
}

str += delimiter + item;
Expand All @@ -91,3 +85,28 @@ exports.cleanupOutData = function(data, params) {
return str;

};

/**
* Remove floating-point numbers leading zero.
*
* @example
* 0.5 → .5
*
* @example
* -0.5 → -.5
*
* @param {Float} num input number
*
* @return {String} output number as string
*/
var removeLeadingZero = exports.removeLeadingZero = function(num) {

if (num > 0 && num < 1) {
num = ('' + num).slice(1);
} else if (num < 0 && num > -1) {
num = '-' + ('' + num).slice(2);
}

return num;

};
2 changes: 1 addition & 1 deletion plugins/removeDefaultPx.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var regValPx = /^(-?(?:[0-9]+|[0-9]*\.[0-9]+))px$/;
var regValPx = /^([\-+]?\d*\.?\d+)px$/;

/**
* Remove default "px" unit from attributes values.
Expand Down
40 changes: 40 additions & 0 deletions plugins/roundNumericValues.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

var regNumericValues = /^([\-+]?\d*\.?\d+)(px|pt|pc|mm|cm|m|in|ft|em|ex|%)?$/,
removeLeadingZero = require('../lib/svgo/tools').removeLeadingZero;

/**
* Round numeric values to the fixed precision.
*
* @param {Object} item current iteration item
* @param {Object} params plugin params
* @return {Boolean} if false, item will be filtered out
*
* @author Kir Belevich
*/
exports.roundNumericValues = function(item, params) {

if (item.isElem()) {

var match;

item.eachAttr(function(attr) {
match = attr.value.match(regNumericValues);

// if attribute value matches regNumericValues
if (match) {
// then round it to the fixed precision
var num = +(+match[1]).toFixed(params.floatPrecision) + (match[2] || '');

// and remove leading zero
if (params.leadingZero) {
num = removeLeadingZero(num);
}

attr.value = num;
}
});

}

};
3 changes: 3 additions & 0 deletions test/plugins/roundNumericValues.01.orig.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions test/plugins/roundNumericValues.01.should.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 67daff2

Please sign in to comment.