Skip to content

Commit 9cfa863

Browse files
committed
feat(): parsePathCurve
1 parent 06bcefe commit 9cfa863

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

intersect.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ declare function findPathIntersections(path1: Path, path2: Path, justCount?: boo
3535

3636
export default findPathIntersections;
3737

38+
/**
39+
* Parse a path so it is suitable to pass to {@link findPathIntersections}
40+
* Used in order to opt out of internal path caching.
41+
*
42+
* @example
43+
* const p1 = parsePathCurve('M0,0L100,100');
44+
* const p2 = parsePathCurve([ [ 'M', 0, 100 ], [ 'L', 100, 0 ] ]);
45+
* const intersections = findPathIntersections(p1, p2);
46+
*
47+
*/
48+
export declare function parsePathCurve(path: Path): PathComponent[]
49+
3850
/**
3951
* A string in the form of 'M150,150m0,-18a18,18,0,1,1,0,36a18,18,0,1,1,0,-36z'
4052
* or something like:

intersect.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,8 @@ function findBezierIntersections(bez1, bez2, justCount) {
395395
* @return {import("./intersect").Intersection[] | number}
396396
*/
397397
export default function findPathIntersections(path1, path2, justCount) {
398-
path1 = getPathCurve(path1);
399-
path2 = getPathCurve(path2);
398+
path1 = path1.parsed ? path1 : getPathCurve(path1);
399+
path2 = path2.parsed ? path2 : getPathCurve(path2);
400400

401401
var x1, y1, x2, y2, x1m, y1m, x2m, y2m, bez1, bez2,
402402
res = justCount ? 0 : [];
@@ -766,7 +766,7 @@ function curveBBox(x0, y0, x1, y1, x2, y2, x3, y3) {
766766
}
767767

768768
/**
769-
* Handles caches
769+
* An impure version of {@link parsePathCurve} handling caching
770770
*/
771771
function getPathCurve(path) {
772772

@@ -795,6 +795,36 @@ function getPathCurve(path) {
795795
return (pth.curve = pathToCurve(abs));
796796
}
797797

798+
/**
799+
* A pure version of {@link getPathCurve}
800+
* @param {import("./intersect").Path} path
801+
* @returns {import("./intersect").PathComponent[]}
802+
*/
803+
export function parsePathCurve(path) {
804+
805+
const abs = (pathToAbsolute(
806+
!Array.isArray(path) ?
807+
parsePathString(path) :
808+
path)
809+
);
810+
811+
const curve = pathToCurve(abs);
812+
813+
/**
814+
* Flag to skip {@link getPathCurve}
815+
*/
816+
return Object.defineProperty(
817+
curve,
818+
'parsed',
819+
{
820+
value: true,
821+
configurable: false,
822+
enumerable: false,
823+
writable: false
824+
}
825+
);
826+
}
827+
798828
function pathToCurve(absPath) {
799829

800830
var curvedPath = pathClone(absPath),

test/intersect.spec.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import intersect from 'path-intersection';
1+
import intersect, { parsePathCurve } from 'path-intersection';
22
import { expect } from 'chai';
33

44
import domify from 'domify';
@@ -21,6 +21,21 @@ describe('path-intersection', function() {
2121
expect(intersections).to.have.length(1);
2222
});
2323

24+
it('parsePathCurve', function() {
25+
26+
// when
27+
var parsed1 = parsePathCurve(p1);
28+
var parsed2 = parsePathCurve(p2);
29+
30+
// then
31+
expect(parsed1).to.deep.eq([['M', 0, 0], ['C', 0, 0, 100, 100, 100, 100]])
32+
expect(parsed1.parsed).to.eq(true)
33+
34+
expect(parsed2).to.deep.eq([['M', 0, 100], ['C', 0, 100, 100, 0, 100, 0]])
35+
expect(parsed2.parsed).to.eq(true)
36+
37+
});
38+
2439

2540
it('should expose intersection', function() {
2641

0 commit comments

Comments
 (0)