Skip to content

Commit

Permalink
Simplify API
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja committed Mar 15, 2024
1 parent 1d36249 commit d90a470
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 97 deletions.
41 changes: 20 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,29 @@ console.log(JSON.stringify(segments, null, 2));
"start": 0,
"end": 4,
"params": ["M", 5, 6],
"chained": true,
"chainStart": 0,
"chainEnd": 8
"chain": {
"start": 0,
"end": 8
},
},
{
"start": 5,
"end": 8,
"params": ["M", 7, 8],
"chained": true,
"chainStart": 0,
"chainEnd": 8
"chain": {
"start": 0,
"end": 8
},
},
{
"start": 8,
"end": 12,
"params": ["l", 3, 4],
"chained": false
},
{
"start": 12,
"end": 13,
"params": ["z"],
"chained": false
}
]
```
Expand All @@ -69,26 +69,25 @@ svg-path-segments --pretty "M5 6 7 8l3 4z"

### Reference

<a name="svgPathParse" href="#svgPathParse">#</a> **svgPathParse**(d: _String_)
<a name="svgPathParse" href="#svgPathParse">#</a> **svgPathParse**(d: _string_)
_Segment[]_

Returns the segments of the SVG path. The result is an array of objects, one
per segment, which contain next properties:

- **`start`** (_Number_): Index of the first character of the segment.
- **`end`** (_Number_): Index of the first character after the segment. Note
- **`start`** (_number_): Index of the first character of the segment.
- **`end`** (_number_): Index of the first character after the segment. Note
that you can use `d.substring(result.start, result.end)` to get the raw string
representation of the segment.
- **`params`** (_Array<Number>_): Parameters of the segment. The first parameter always
- **`params`** (_number[]_): Parameters of the segment. The first parameter always
is the command that draws the segment.
- **`chained`** (_Boolean_): Indicates that the segment is part of a chained
set of segments. If this property is `true`, the object will also contain the
properties `chainStart` and `chainEnd`.
- **`chainStart`** (optional, _Number_): Index of the first character of the
chained set of segments to which the segment belongs.
- **`chainEnd`** (optional, _Number_): Index of the first character after the
chained set of segments to which the segment belongs. Note that you can use
`d.substring(result.chainStart, result.chainEnd)` to get the raw string
- **`chain`** (_object?_): If present, indicates that the segment is part of a
chained set of segments.
- **`start`** (_number_): Index of the first character of the chained set of
segments to which the segment belongs.
- **`end`** (_number_): Index of the first character after the chained set of
segments to which the segment belongs. Note that you can use
`d.substring(result.chain.start, result.chain.end)` to get the raw string
representation of the chained set of segments to which the segment belongs.

### Comparison against other implementations
Expand All @@ -113,7 +112,7 @@ per segment, which contain next properties:
| Unicode support | NO | PARTIAL |

> \* Benchmarks are orientative, if you want to perform your own, see
[scripts/*simple-icons*-benchmark.js][scripts-link].
[scripts/\*-benchmark.js][scripts-link].

### Usage with [fontello/svgpath](https://github.com/fontello/svgpath)

Expand Down
16 changes: 8 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
* start: number,
* end: number,
* params: number[],
* chained: boolean,
* chainStart?: number,
* chainEnd?: number
* chain?: {
* start: number,
* end: number,
* },
* }} Segment
*/

Expand Down Expand Up @@ -190,21 +191,20 @@ const scanSegment = function (d, buffer, needParams, start, end, segments) {

if (subSegments.length >> 1) { // bitwise 'subSegments.length > 1'
for (let s = 0; s < subSegments.length; s++) {
subSegments[s].chained = true;
subSegments[s].chainStart = start;
subSegments[s].chainEnd = _lastNumIndex + 1;
subSegments[s].chain = {
start,
end: _lastNumIndex + 1
};
segments.push(subSegments[s]);
}
} else {
subSegments[0].chained = false;
segments.push(subSegments[0]);
}
} else { // zZ
segments.push({
start,
end: end + 1,
params,
chained: false,
});
}
};
Expand Down
36 changes: 18 additions & 18 deletions tests/chains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ const chainsCases = [
[
'M5 6l3 4 1 2z',
[
{start: 0, end: 4, chained: false, params: ['M', 5, 6]},
{start: 0, end: 4, params: ['M', 5, 6]},
{
start: 4, end: 8, params: ['l', 3, 4],
chained: true, chainStart: 4, chainEnd: 12
chain: {start: 4, end: 12}
},
{
start: 9, end: 12, params: ['l', 1, 2],
chained: true, chainStart: 4, chainEnd: 12,
chain: {start: 4, end: 12}
},
{start: 12, end: 13, chained: false, params: ['z']},
{start: 12, end: 13, params: ['z']},
]
],

Expand All @@ -25,34 +25,34 @@ const chainsCases = [
'H3 4 1 2v3 .2.1z',
[
{
start: 0, end: 2, chained: true, params: ['H', 3],
chainStart: 0, chainEnd: 8,
start: 0, end: 2, params: ['H', 3],
chain: {start: 0, end: 8}
},
{
start: 3, end: 4, chained: true,
chainStart: 0, chainEnd: 8, params: ['H', 4]
start: 3, end: 4, params: ['H', 4],
chain: {start: 0, end: 8}
},
{
start: 5, end: 6, chained: true,
chainStart: 0, chainEnd: 8, params: ['H', 1]
start: 5, end: 6, params: ['H', 1],
chain: {start: 0, end: 8},
},
{
start: 7, end: 8, chained: true,
chainStart: 0, chainEnd: 8, params: ['H', 2]
start: 7, end: 8, params: ['H', 2],
chain: {start: 0, end: 8},
},
{
start: 8, end: 10, params: ['v', 3],
chained: true, chainStart: 8, chainEnd: 15,
chain: {start: 8, end: 15},
},
{
start: 11, end: 13, chained: true,
chainStart: 8, chainEnd: 15, params: ['v', .2]
start: 11, end: 13, params: ['v', .2],
chain: {start: 8, end: 15},
},
{
start: 13, end: 15, chained: true,
chainStart: 8, chainEnd: 15, params: ['v', .1]
start: 13, end: 15, params: ['v', .1],
chain: {start: 8, end: 15},
},
{start: 15, end: 16, chained: false, params: ['z']},
{start: 15, end: 16, params: ['z']},
]
]
];
Expand Down
35 changes: 15 additions & 20 deletions tests/commands.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,69 +7,69 @@ const commandCases = [
[
'M5 3',
[
{start: 0, end: 4, chained: false, params: ['M', 5, 3]},
{start: 0, end: 4, params: ['M', 5, 3]},
]
],
[
'm5 3',
[
{start: 0, end: 4, chained: false, params: ['m', 5, 3]},
{start: 0, end: 4, params: ['m', 5, 3]},
]
],

// Ll
[
'L5 3',
[
{start: 0, end: 4, chained: false, params: ['L', 5, 3]},
{start: 0, end: 4, params: ['L', 5, 3]},
]
],
[
'l5 3',
[
{start: 0, end: 4, chained: false, params: ['l', 5, 3]},
{start: 0, end: 4, params: ['l', 5, 3]},
]
],

// Hh
[
'H5',
[
{start: 0, end: 2, chained: false, params: ['H', 5]},
{start: 0, end: 2, params: ['H', 5]},
]
],
[
'h5',
[
{start: 0, end: 2, chained: false, params: ['h', 5]},
{start: 0, end: 2, params: ['h', 5]},
]
],

// Vv
[
'V5',
[
{start: 0, end: 2, chained: false, params: ['V', 5]},
{start: 0, end: 2, params: ['V', 5]},
]
],
[
'v5',
[
{start: 0, end: 2, chained: false, params: ['v', 5]},
{start: 0, end: 2, params: ['v', 5]},
]
],

// Zz
[
'Z',
[
{start: 0, end: 1, chained: false, params: ['Z']},
{start: 0, end: 1, params: ['Z']},
]
],
[
'z',
[
{start: 0, end: 1, chained: false, params: ['z']},
{start: 0, end: 1, params: ['z']},
]
],

Expand All @@ -79,7 +79,6 @@ const commandCases = [
[
{
start: 0, end: 30, params: ['C', -.805, .062, -1.434, .77, -1.434, 1.61],
chained: false,
}
]
],
Expand All @@ -88,7 +87,6 @@ const commandCases = [
[
{
start: 0, end: 32, params: ['c', -.805, .062, -.429, -.893, -1.034, -1.284],
chained: false,
}
]
],
Expand All @@ -97,14 +95,14 @@ const commandCases = [
[
'T.11-.33',
[
{start: 0, end: 8, chained: false, params: ['T', .11, -.33]}
{start: 0, end: 8, params: ['T', .11, -.33]}
]
],
[
't.11-.33',
[
{
start: 0, end: 8, chained: false, params: ['t', .11, -.33]}
start: 0, end: 8, params: ['t', .11, -.33]}
]
],

Expand All @@ -114,7 +112,6 @@ const commandCases = [
[
{
start: 0, end: 19, params: ['A', .72, .72, 0, 0, 0, 23.28, 0],
chained: false,
}
]
],
Expand All @@ -123,7 +120,6 @@ const commandCases = [
[
{
start: 0, end: 20, params: ['a', .72, .72, 90, 1, 0, 23.28, 0],
chained: false,
}
]
],
Expand All @@ -134,7 +130,7 @@ const commandCases = [
[
{
start: 0, end: 21,
chained: false, params: ['S', .036, 18.858, 0, 17.347]
params: ['S', .036, 18.858, 0, 17.347]
}
]
],
Expand All @@ -143,7 +139,7 @@ const commandCases = [
[
{
start: 0, end: 21,
chained: false, params: ['s', .036, 18.858, 0, 17.347]
params: ['s', .036, 18.858, 0, 17.347]
}
]
],
Expand All @@ -154,7 +150,6 @@ const commandCases = [
[
{
start: 0, end: 13, params: ['Q', .14, 0, .25, .12],
chained: false,
}
]
],
Expand All @@ -163,7 +158,7 @@ const commandCases = [
[
{
start: 0, end: 13,
chained: false, params: ['q', .14, 0, .25, .12]
params: ['q', .14, 0, .25, .12]
}
]
],
Expand Down
2 changes: 1 addition & 1 deletion tests/si-integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('pathParse(d) [Command cases]', () => {
// https://www.w3.org/TR/SVG/paths.html#PathDataMovetoCommands
for (let s = 0; s < svgpathParams.length; s++) {
if (
segments[s].chained &&
segments[s].chain !== undefined &&
['m', 'M'].indexOf(segmentsParams[s][0]) > -1 &&
['l', 'L'].indexOf(svgpathParams[s][0]) > -1
) {
Expand Down

0 comments on commit d90a470

Please sign in to comment.