mirror of
https://github.com/svg/svgo.git
synced 2025-07-29 20:21:14 +03:00
Simplify number rendering and fix -0 in path
Ref https://github.com/svg/svgo/issues/1422
This commit is contained in:
16
lib/path.js
16
lib/path.js
@ -241,20 +241,12 @@ exports.parsePathData = parsePathData;
|
|||||||
* @param {StringifyNumberOptions} param
|
* @param {StringifyNumberOptions} param
|
||||||
*/
|
*/
|
||||||
const stringifyNumber = ({ number, precision }) => {
|
const stringifyNumber = ({ number, precision }) => {
|
||||||
let result;
|
if (precision != null) {
|
||||||
if (precision == null) {
|
const ratio = 10 ** precision;
|
||||||
result = number.toString();
|
number = Math.round(number * ratio) / ratio;
|
||||||
} else {
|
|
||||||
result = number.toFixed(precision);
|
|
||||||
if (result.includes('.')) {
|
|
||||||
result = result.replace(/\.?0+$/, '');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// remove zero whole from decimal number
|
// remove zero whole from decimal number
|
||||||
if (result !== '0') {
|
return number.toString().replace(/^0\./, '.').replace(/^-0\./, '-.');
|
||||||
result = result.replace(/^0/, '').replace(/^-0/, '-');
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -142,26 +142,24 @@ describe('stringify path data', () => {
|
|||||||
).to.equal('M0-1.2.3 4 5-.6 7 .8');
|
).to.equal('M0-1.2.3 4 5-.6 7 .8');
|
||||||
});
|
});
|
||||||
it('should configure precision', () => {
|
it('should configure precision', () => {
|
||||||
|
const pathData = [
|
||||||
|
{ command: 'M', args: [0, -1.9876] },
|
||||||
|
{ command: 'L', args: [0.3, 3.14159265] },
|
||||||
|
{ command: 'L', args: [-0.3, -3.14159265] },
|
||||||
|
{ command: 'L', args: [100, 200] },
|
||||||
|
];
|
||||||
expect(
|
expect(
|
||||||
stringifyPathData({
|
stringifyPathData({
|
||||||
pathData: [
|
pathData,
|
||||||
{ command: 'M', args: [0, -1.9876] },
|
|
||||||
{ command: 'L', args: [0.3, 3.14159265] },
|
|
||||||
{ command: 'L', args: [100, 200] },
|
|
||||||
],
|
|
||||||
precision: 3,
|
precision: 3,
|
||||||
})
|
})
|
||||||
).to.equal('M0-1.988.3 3.142 100 200');
|
).to.equal('M0-1.988.3 3.142-.3-3.142 100 200');
|
||||||
expect(
|
expect(
|
||||||
stringifyPathData({
|
stringifyPathData({
|
||||||
pathData: [
|
pathData,
|
||||||
{ command: 'M', args: [0, -1.9876] },
|
|
||||||
{ command: 'L', args: [0.3, 3.14159265] },
|
|
||||||
{ command: 'L', args: [100, 200] },
|
|
||||||
],
|
|
||||||
precision: 0,
|
precision: 0,
|
||||||
})
|
})
|
||||||
).to.equal('M0-2 0 3 100 200');
|
).to.equal('M0-2 0 3 0-3 100 200');
|
||||||
});
|
});
|
||||||
it('allows to avoid spaces after arc flags', () => {
|
it('allows to avoid spaces after arc flags', () => {
|
||||||
const pathData = [
|
const pathData = [
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
@@@
|
@@@
|
||||||
|
|
||||||
<svg width="100" height="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
<svg width="100" height="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||||
<path d="m33.027833 1.96545901 5.0135294 5.01352941c.0000001-.00000008-.0319306-2.94431722-.0319306-2.94431722L34 .02523956V0H13v2h20.062374z"/>
|
<path d="m33.027833 1.96545901 5.0135294 5.01352941c1e-7-8e-8-.0319306-2.94431722-.0319306-2.94431722L34 .02523956V0H13v2h20.062374z"/>
|
||||||
</svg>
|
</svg>
|
||||||
|
|
||||||
@@@
|
@@@
|
||||||
|
Before Width: | Height: | Size: 686 B After Width: | Height: | Size: 677 B |
@ -19,7 +19,7 @@ Precision should be applied to all converted shapes
|
|||||||
<path d="M26.614 29.232 34.268 8.176"/>
|
<path d="M26.614 29.232 34.268 8.176"/>
|
||||||
<path d="M26.614 29.232 34.268 8.176z"/>
|
<path d="M26.614 29.232 34.268 8.176z"/>
|
||||||
<path d="M26.614-5.036A34.268 34.268 0 1 0 26.614 63.5 34.268 34.268 0 1 0 26.614-5.036z"/>
|
<path d="M26.614-5.036A34.268 34.268 0 1 0 26.614 63.5 34.268 34.268 0 1 0 26.614-5.036z"/>
|
||||||
<path d="M26.614 21.056A34.268 8.176 0 1 0 26.614 37.408 34.268 8.176 0 1 0 26.614 21.056z"/>
|
<path d="M26.614 21.057A34.268 8.176 0 1 0 26.614 37.408 34.268 8.176 0 1 0 26.614 21.057z"/>
|
||||||
</svg>
|
</svg>
|
||||||
|
|
||||||
@@@
|
@@@
|
||||||
|
Reference in New Issue
Block a user