@@ -98,4 +98,64 @@ describe('equalize segments', () => {
9898 ] ,
9999 ] ) ;
100100 } ) ;
101+ it ( 'should not recurse infinitely if segments cannot be split' , ( ) => {
102+ const path1 : PathArray = [
103+ [ 'M' , 0 , 0 ] ,
104+ [ 'L' , 1 , 1 ] , // 非常短的线段,不满足 split 条件
105+ ] ;
106+ const path2 : PathArray = [
107+ [ 'M' , 0 , 0 ] ,
108+ [ 'L' , 10 , 10 ] ,
109+ [ 'L' , 20 , 20 ] ,
110+ [ 'L' , 30 , 30 ] ,
111+ ] ;
112+
113+ const result = equalizeSegments ( path1 , path2 ) ;
114+ // 不是一定相等,因为可能无法拆分,重点是不会死循环
115+ expect ( Array . isArray ( result ) ) . toBe ( true ) ;
116+ expect ( result . length ) . toBe ( 2 ) ;
117+ } ) ;
118+ it ( 'should split cubic bezier curves correctly' , ( ) => {
119+ const path1 : PathArray = [
120+ [ 'M' , 0 , 0 ] ,
121+ [ 'C' , 30 , 30 , 60 , 30 , 100 , 0 ] ,
122+ ] ;
123+ const path2 : PathArray = [
124+ [ 'M' , 0 , 0 ] ,
125+ [ 'C' , 20 , 20 , 40 , 20 , 60 , 0 ] ,
126+ [ 'C' , 70 , - 20 , 90 , - 20 , 100 , 0 ] ,
127+ ] ;
128+
129+ const result = equalizeSegments ( path1 , path2 ) ;
130+ expect ( result [ 0 ] . length ) . toBe ( result [ 1 ] . length ) ;
131+ } ) ;
132+ it ( 'should equalizeSegments for complex multi-segment paths' , ( ) => {
133+ const path1 : PathArray = [
134+ [ 'M' , 0 , 0 ] ,
135+ [ 'L' , 50 , 0 ] ,
136+ [ 'C' , 60 , 10 , 70 , 10 , 80 , 0 ] ,
137+ ] ;
138+ const path2 : PathArray = [
139+ [ 'M' , 0 , 0 ] ,
140+ [ 'L' , 20 , 0 ] ,
141+ [ 'L' , 40 , 0 ] ,
142+ [ 'L' , 60 , 0 ] ,
143+ [ 'C' , 65 , 5 , 75 , 5 , 80 , 0 ] ,
144+ ] ;
145+
146+ const result = equalizeSegments ( path1 , path2 ) ;
147+ expect ( result [ 0 ] . length ) . toBe ( result [ 1 ] . length ) ;
148+ } ) ;
149+ it ( 'should terminate recursion at max depth' , ( ) => {
150+ const path1 : PathArray = [
151+ [ 'M' , 0 , 0 ] ,
152+ [ 'L' , 1 , 1 ] ,
153+ ] ;
154+ // @ts -ignore
155+ const path2 : PathArray = Array . from ( { length : 20 } , ( _ , i ) => [ 'L' , i , i + 1 ] ) as PathArray ;
156+ path2 . unshift ( [ 'M' , 0 , 0 ] ) ;
157+
158+ const result = equalizeSegments ( path1 , path2 ) ;
159+ expect ( result . length ) . toBe ( 2 ) ;
160+ } ) ;
101161} ) ;
0 commit comments