-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new plugin: decrease accuracy of floating-point numbers (close svg#8)
- Loading branch information
deepsweet
committed
Nov 29, 2012
1 parent
02f04d9
commit 67daff2
Showing
6 changed files
with
80 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}); | ||
|
||
} | ||
|
||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.