mirror of
https://github.com/svg/svgo.git
synced 2025-07-29 20:21:14 +03:00
Implement path stringify (#1387)
Ref https://github.com/svg/svgo/issues/32 Added `stringifyPathData` utility to produce small as possible path by - matching leading moveto and line to commands (m -> l, M -> L) - combining moveto and lineto commands - avoiding space before decimal numbers if possible
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const { expect } = require('chai');
|
||||
const { parsePathData } = require('./path.js');
|
||||
const { parsePathData, stringifyPathData } = require('./path.js');
|
||||
|
||||
describe('parse path data', () => {
|
||||
it('should allow spaces between commands', () => {
|
||||
@ -74,3 +74,88 @@ describe('parse path data', () => {
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('stringify path data', () => {
|
||||
it('should combine sequence of the same commands', () => {
|
||||
expect(
|
||||
stringifyPathData({
|
||||
pathData: [
|
||||
{ command: 'M', args: [0, 0] },
|
||||
{ command: 'h', args: [10] },
|
||||
{ command: 'h', args: [20] },
|
||||
{ command: 'h', args: [30] },
|
||||
{ command: 'H', args: [40] },
|
||||
{ command: 'H', args: [50] },
|
||||
],
|
||||
})
|
||||
).to.equal('M0 0h10 20 30H40 50');
|
||||
});
|
||||
it('should not combine sequence of moveto', () => {
|
||||
expect(
|
||||
stringifyPathData({
|
||||
pathData: [
|
||||
{ command: 'M', args: [0, 0] },
|
||||
{ command: 'M', args: [10, 10] },
|
||||
{ command: 'm', args: [20, 30] },
|
||||
{ command: 'm', args: [40, 50] },
|
||||
],
|
||||
})
|
||||
).to.equal('M0 0M10 10m20 30m40 50');
|
||||
});
|
||||
it('should combine moveto and sequence of lineto', () => {
|
||||
expect(
|
||||
stringifyPathData({
|
||||
pathData: [
|
||||
{ command: 'M', args: [0, 0] },
|
||||
{ command: 'l', args: [10, 10] },
|
||||
{ command: 'M', args: [0, 0] },
|
||||
{ command: 'l', args: [10, 10] },
|
||||
{ command: 'M', args: [0, 0] },
|
||||
{ command: 'L', args: [10, 10] },
|
||||
],
|
||||
})
|
||||
).to.equal('m0 0 10 10M0 0l10 10M0 0 10 10');
|
||||
expect(
|
||||
stringifyPathData({
|
||||
pathData: [
|
||||
{ command: 'm', args: [0, 0] },
|
||||
{ command: 'L', args: [10, 10] },
|
||||
],
|
||||
})
|
||||
).to.equal('M0 0 10 10');
|
||||
});
|
||||
it('should avoid space before first, negative and decimals', () => {
|
||||
expect(
|
||||
stringifyPathData({
|
||||
pathData: [
|
||||
{ command: 'M', args: [0, -1.2] },
|
||||
{ command: 'L', args: [0.3, 4] },
|
||||
{ command: 'L', args: [5, -0.6] },
|
||||
{ command: 'L', args: [7, 0.8] },
|
||||
],
|
||||
})
|
||||
).to.equal('M0-1.2.3 4 5-.6 7 .8');
|
||||
});
|
||||
it('should configure precision', () => {
|
||||
expect(
|
||||
stringifyPathData({
|
||||
pathData: [
|
||||
{ command: 'M', args: [0, -1.9876] },
|
||||
{ command: 'L', args: [0.3, 3.14159265] },
|
||||
{ command: 'L', args: [100, 200] },
|
||||
],
|
||||
precision: 3,
|
||||
})
|
||||
).to.equal('M0-1.988.3 3.142 100 200');
|
||||
expect(
|
||||
stringifyPathData({
|
||||
pathData: [
|
||||
{ command: 'M', args: [0, -1.9876] },
|
||||
{ command: 'L', args: [0.3, 3.14159265] },
|
||||
{ command: 'L', args: [100, 200] },
|
||||
],
|
||||
precision: 0,
|
||||
})
|
||||
).to.equal('M0-2 0 3 100 200');
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user