@flighthq/path-formats 0.5.1-next.973.788d3d7 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,259 +1,415 @@
1
- import { createPath, forEachPathSegment } from '@flighthq/path/contract';
2
- import type { Path, PathSegment } from '@flighthq/types/contract';
1
+ import { PathCommand, createPath, forEachPathSegment } from '@flighthq/path/contract';
2
+ import type { Path, PathSegment } from '@flighthq/path/contract';
3
3
  import { describe, expect, it } from 'vitest';
4
4
 
5
- import { appendSvgPathData, explainSvgPathData, formatSvgPathData, parseSvgPathData } from './svgPathData';
5
+ import { appendSvgPathData, explainSvgPathData, formatSvgPathData, parseSvgPathData } from './contract.ts';
6
+ import type { SvgPathDataExplanation, SvgPathDataFormatOptions } from './contract.ts';
6
7
 
7
8
  function collectSegments(path: Readonly<Path>): PathSegment[] {
8
- const segments: PathSegment[] = [];
9
- forEachPathSegment(path, (segment) => segments.push(segment));
9
+ let segments: PathSegment[] = [] as never[];
10
+ forEachPathSegment(path, (segment: PathSegment) => segments.push(segment));
10
11
  return segments;
11
12
  }
12
13
 
14
+ function kinds(segments: Readonly<PathSegment[]>): string[] {
15
+ return segments.map((segment) => segment.kind);
16
+ }
17
+
18
+ function values(segment: Readonly<PathSegment>): number[] {
19
+ return (() => {
20
+ const matchValue = segment;
21
+ if (matchValue.kind === 'moveTo' || matchValue.kind === 'lineTo') {
22
+ const x = matchValue.x;
23
+ const y = matchValue.y;
24
+ return [x, y];
25
+ } else if (matchValue.kind === 'quadraticCurveTo') {
26
+ const controlX = matchValue.controlX;
27
+ const controlY = matchValue.controlY;
28
+ const x = matchValue.x;
29
+ const y = matchValue.y;
30
+ return [controlX, controlY, x, y];
31
+ } else if (matchValue.kind === 'cubicCurveTo') {
32
+ const controlX1 = matchValue.controlX1;
33
+ const controlY1 = matchValue.controlY1;
34
+ const controlX2 = matchValue.controlX2;
35
+ const controlY2 = matchValue.controlY2;
36
+ const x = matchValue.x;
37
+ const y = matchValue.y;
38
+ return [controlX1, controlY1, controlX2, controlY2, x, y];
39
+ } else if (matchValue.kind === 'close') {
40
+ return [];
41
+ }
42
+ throw new Error('unknown path segment');
43
+ })();
44
+ }
45
+
46
+ function lastPoint(segments: Readonly<PathSegment[]>): readonly [number, number] {
47
+ const $letElse = segments[segments.length - 1];
48
+ if (!($letElse.kind === 'cubicCurveTo')) {
49
+ throw new Error(`expected a trailing cubic`);
50
+ }
51
+ const x = $letElse.x;
52
+ const y = $letElse.y;
53
+ return [x, y];
54
+ }
55
+
56
+ function precision(value: number | undefined): SvgPathDataFormatOptions {
57
+ return { precision: value };
58
+ }
59
+
60
+ function explained(position: number, reason: string): SvgPathDataExplanation | undefined {
61
+ return { position: position, reason: String(reason) };
62
+ }
63
+
64
+ function parsed(d: string): PathSegment[] {
65
+ return collectSegments(
66
+ (() => {
67
+ const value = parseSvgPathData(d);
68
+ if (value === undefined) throw new Error('well-formed');
69
+ return value;
70
+ })(),
71
+ );
72
+ }
73
+
13
74
  describe('appendSvgPathData', () => {
14
75
  it('appends into an existing path without clearing it', () => {
15
- const path = createPath();
76
+ let path = createPath('nonZero');
16
77
  appendSvgPathData(path, 'M0 0 L10 0');
17
78
  const ok = appendSvgPathData(path, 'M20 20 L30 20');
18
79
  expect(ok).toBe(true);
19
- const segments = collectSegments(path);
20
- expect(segments.map((s) => s.kind)).toEqual(['moveTo', 'lineTo', 'moveTo', 'lineTo']);
80
+ expect(kinds(collectSegments(path))).toEqual(['moveTo', 'lineTo', 'moveTo', 'lineTo']);
21
81
  });
22
-
23
82
  it('returns false on malformed input', () => {
24
- const path = createPath();
25
- expect(appendSvgPathData(path, 'L10 10')).toBe(false);
83
+ let path = createPath('nonZero');
84
+ expect(!appendSvgPathData(path, 'L10 10')).toBe(true);
26
85
  });
27
-
28
86
  it('does not partially mutate the destination on malformed input', () => {
29
- const path = createPath();
87
+ let path = createPath('nonZero');
30
88
  appendSvgPathData(path, 'M1 2 L3 4');
31
- const commands = [...path.commands];
32
- const data = [...path.data];
33
- expect(appendSvgPathData(path, 'M10 20 L30')).toBe(false);
89
+ const commands = path.commands;
90
+ const data = path.data;
91
+ expect(!appendSvgPathData(path, 'M10 20 L30')).toBe(true);
34
92
  expect(path.commands).toEqual(commands);
35
93
  expect(path.data).toEqual(data);
36
94
  });
95
+ it('appends nothing for empty input', () => {
96
+ let path = createPath('nonZero');
97
+ expect(appendSvgPathData(path, ' \t,')).toBe(true);
98
+ expect(path.commands.length === 0 && path.data.length === 0).toBe(true);
99
+ });
37
100
  });
38
101
 
39
102
  describe('explainSvgPathData', () => {
40
103
  it('returns null for valid input and locates malformed input', () => {
41
- expect(explainSvgPathData('M0 0 L1 1')).toBeNull();
42
- expect(explainSvgPathData('L10 10')).toEqual({ position: 0, reason: 'expected-moveto' });
43
- expect(explainSvgPathData('M0 0 X10 10')).toEqual({ position: 5, reason: 'expected-command' });
104
+ expect(explainSvgPathData('M0 0 L1 1')).toEqual(undefined);
105
+ expect(explainSvgPathData('L10 10')).toEqual(explained(0, 'expected-moveto'));
106
+ expect(explainSvgPathData('M0 0 X10 10')).toEqual(explained(5, 'expected-command'));
107
+ });
108
+ it('reports utf16 code unit positions', () => {
109
+ expect(explainSvgPathData('M0 0Z😀')).toEqual(explained(5, 'expected-command'));
110
+ expect(explainSvgPathData('😀')).toEqual(explained(0, 'expected-command'));
111
+ expect(explainSvgPathData('M0 0 é')).toEqual(explained(5, 'expected-coordinate-pair'));
112
+ });
113
+ it('reports each stable reason at the source position', () => {
114
+ expect(explainSvgPathData('M0 0 L10')).toEqual(explained(8, 'expected-coordinate-pair'));
115
+ expect(explainSvgPathData('M')).toEqual(explained(1, 'expected-coordinate-pair'));
116
+ expect(explainSvgPathData('M0 0 H')).toEqual(explained(6, 'expected-coordinate'));
117
+ expect(explainSvgPathData('M0 0 C1 2 3 4 5')).toEqual(explained(15, 'expected-cubic-coordinates'));
118
+ expect(explainSvgPathData('M0 0 S1 2 3')).toEqual(explained(11, 'expected-smooth-cubic-coordinates'));
119
+ expect(explainSvgPathData('M0 0 Q1 2 3')).toEqual(explained(11, 'expected-quadratic-coordinates'));
120
+ expect(explainSvgPathData('M0 0 T1')).toEqual(explained(7, 'expected-coordinate-pair'));
121
+ expect(explainSvgPathData('M0 0 A5 5 0 0 1 10')).toEqual(explained(18, 'expected-arc-parameters'));
122
+ expect(explainSvgPathData(' z')).toEqual(explained(2, 'expected-moveto'));
123
+ });
124
+ it('keeps reading operands after a failed flag as the source does', () => {
125
+ expect(explainSvgPathData('M0 0 A1 1 0 5 6')).toEqual(explained(15, 'expected-arc-parameters'));
126
+ });
127
+ it('stops an operand run at any letter', () => {
128
+ expect(explainSvgPathData('M0 0 1 1 k')).toEqual(explained(9, 'expected-command'));
129
+ expect(explainSvgPathData('M0 0 L1 1 #')).toEqual(explained(10, 'expected-coordinate-pair'));
44
130
  });
45
131
  });
46
132
 
47
133
  describe('formatSvgPathData', () => {
48
- it('emits absolute M/L/C/Q/Z commands per segment kind', () => {
49
- const path = createPath();
134
+ it('emits absolute m l c q z commands per segment kind', () => {
135
+ let path = createPath('nonZero');
50
136
  appendSvgPathData(path, 'M0 0 L10 0 Q15 5 20 0 C22 2 24 2 26 0 Z');
51
- expect(formatSvgPathData(path)).toBe('M0 0L10 0Q15 5 20 0C22 2 24 2 26 0Z');
137
+ expect(formatSvgPathData(path, undefined)).toEqual('M0 0L10 0Q15 5 20 0C22 2 24 2 26 0Z');
52
138
  });
53
-
54
139
  it('rounds coordinates and trims trailing zeros with a precision option', () => {
55
- const path = createPath();
140
+ let path = createPath('nonZero');
56
141
  appendSvgPathData(path, 'M0.123456 0.5 L10.987654 20');
57
- expect(formatSvgPathData(path, { precision: 2 })).toBe('M0.12 0.5L10.99 20');
142
+ expect(formatSvgPathData(path, precision(2.0))).toEqual('M0.12 0.5L10.99 20');
58
143
  });
59
-
60
144
  it('emits full precision by default', () => {
61
- const path = createPath();
145
+ let path = createPath('nonZero');
62
146
  appendSvgPathData(path, 'M0.5 0.25 L1.5 2.75');
63
- expect(formatSvgPathData(path)).toBe('M0.5 0.25L1.5 2.75');
147
+ expect(formatSvgPathData(path, undefined)).toEqual('M0.5 0.25L1.5 2.75');
148
+ expect(formatSvgPathData(path, precision(undefined))).toEqual('M0.5 0.25L1.5 2.75');
64
149
  });
65
-
66
- it('emits no A command — arcs are stored as cubics', () => {
67
- const path = createPath();
150
+ it('emits no a command arcs are stored as cubics', () => {
151
+ let path = createPath('nonZero');
68
152
  appendSvgPathData(path, 'M0 0 A5 5 0 0 1 10 0');
69
- const d = formatSvgPathData(path);
70
- expect(d).not.toContain('A');
153
+ const d = formatSvgPathData(path, undefined);
154
+ expect(!d.includes('A')).toBe(true);
71
155
  expect(d.startsWith('M0 0C')).toBe(true);
72
156
  });
157
+ it('keeps an explicit zero precision distinct from omitted', () => {
158
+ let path = createPath('nonZero');
159
+ appendSvgPathData(path, 'M1.5 -1.5 L2.4 -2.5');
160
+ expect(formatSvgPathData(path, undefined)).toEqual('M1.5 -1.5L2.4 -2.5');
161
+ expect(formatSvgPathData(path, precision(0.0))).toEqual('M2 -1L2 -2');
162
+ });
163
+ it('spells numbers as javascript string does', () => {
164
+ let path = createPath('nonZero');
165
+ appendSvgPathData(path, 'M1e21 1e-7 L123456789012345680000 0.000001 L-0 0.1');
166
+ expect(formatSvgPathData(path, undefined)).toEqual('M1e+21 1e-7L123456789012345680000 0.000001L0 0.1');
167
+ let tiny = createPath('nonZero');
168
+ appendSvgPathData(tiny, 'M1.5e-10 -2.25e30 L5e-324 1.7976931348623157e308');
169
+ expect(formatSvgPathData(tiny, undefined)).toEqual('M1.5e-10 -2.25e+30L5e-324 1.7976931348623157e+308');
170
+ });
171
+ it('normalizes a rounded negative zero to zero', () => {
172
+ let path = createPath('nonZero');
173
+ appendSvgPathData(path, 'M-0.001 -0.004');
174
+ expect(formatSvgPathData(path, precision(2.0))).toEqual('M0 0');
175
+ });
176
+ it('rounds to tens with a negative precision', () => {
177
+ let path = createPath('nonZero');
178
+ appendSvgPathData(path, 'M123 125 L-125 -0.4');
179
+ expect(formatSvgPathData(path, precision(-1.0))).toEqual('M120 130L-120 0');
180
+ });
181
+ it('applies a fractional precision through the power of ten', () => {
182
+ let path = createPath('nonZero');
183
+ appendSvgPathData(path, 'M1 0');
184
+ const d = formatSvgPathData(path, precision(0.5));
185
+ const x: number = ((s) => {
186
+ const v = Number(s);
187
+ return s.trim() !== '' && !Number.isNaN(v) ? v : undefined;
188
+ })(
189
+ d.slice(
190
+ 1,
191
+ ((value: string, pattern: string): number | undefined => {
192
+ const index = value.indexOf(pattern);
193
+ return index === -1 ? undefined : index;
194
+ })(d, ' ')!,
195
+ ),
196
+ )!;
197
+ expect(Math.abs(x - 0.9486832980505138) < 1e-15).toBe(true);
198
+ expect(d.endsWith(' 0')).toBe(true);
199
+ });
200
+ it('yields nan for non finite or overflowing precision', () => {
201
+ let path = createPath('nonZero');
202
+ appendSvgPathData(path, 'M1 0');
203
+ for (const value of [Number.NaN, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY, 400.0]) {
204
+ expect(formatSvgPathData(path, precision(value))).toEqual('MNaN NaN');
205
+ }
206
+ });
207
+ it('spells non finite coordinates as javascript does', () => {
208
+ let path = createPath('nonZero');
209
+ path.commands.push(PathCommand.MOVE_TO & 0xff);
210
+ path.data.push(Number.NaN);
211
+ path.data.push(Number.POSITIVE_INFINITY);
212
+ path.commands.push(PathCommand.LINE_TO & 0xff);
213
+ path.data.push(Number.NEGATIVE_INFINITY);
214
+ path.data.push(-0.0);
215
+ expect(formatSvgPathData(path, undefined)).toEqual('MNaN InfinityL-Infinity 0');
216
+ expect(formatSvgPathData(path, precision(2.0))).toEqual('MNaN InfinityL-Infinity 0');
217
+ });
218
+ it('keeps extreme magnitudes through a precision option', () => {
219
+ let path = createPath('nonZero');
220
+ appendSvgPathData(path, 'M1e300 -1e-300');
221
+ expect(formatSvgPathData(path, precision(2.0))).toEqual('M1e+300 0');
222
+ });
223
+ it('emits an empty string for an empty path', () => {
224
+ expect(formatSvgPathData(createPath('nonZero'), undefined)).toEqual('');
225
+ });
73
226
  });
74
227
 
75
228
  describe('parseSvgPathData', () => {
76
229
  it('parses absolute moveto and lineto', () => {
77
- const path = parseSvgPathData('M0 0 L10 10');
78
- expect(path).not.toBeNull();
79
- expect(collectSegments(path as Path)).toEqual([
80
- { kind: 'moveTo', x: 0, y: 0 },
81
- { kind: 'lineTo', x: 10, y: 10 },
230
+ expect(parsed('M0 0 L10 10')).toEqual([
231
+ { kind: 'moveTo', x: 0.0, y: 0.0 },
232
+ { kind: 'lineTo', x: 10.0, y: 10.0 },
82
233
  ]);
83
234
  });
84
-
85
235
  it('treats repeated moveto operands as implicit lineto', () => {
86
- const path = parseSvgPathData('M0 0 1 1 2 2');
87
- expect(collectSegments(path as Path)).toEqual([
88
- { kind: 'moveTo', x: 0, y: 0 },
89
- { kind: 'lineTo', x: 1, y: 1 },
90
- { kind: 'lineTo', x: 2, y: 2 },
236
+ expect(parsed('M0 0 1 1 2 2')).toEqual([
237
+ { kind: 'moveTo', x: 0.0, y: 0.0 },
238
+ { kind: 'lineTo', x: 1.0, y: 1.0 },
239
+ { kind: 'lineTo', x: 2.0, y: 2.0 },
91
240
  ]);
92
241
  });
93
-
94
242
  it('resolves relative commands against the current point', () => {
95
- const path = parseSvgPathData('m10 10 l5 5 l-5 0');
96
- expect(collectSegments(path as Path)).toEqual([
97
- { kind: 'moveTo', x: 10, y: 10 },
98
- { kind: 'lineTo', x: 15, y: 15 },
99
- { kind: 'lineTo', x: 10, y: 15 },
243
+ expect(parsed('m10 10 l5 5 l-5 0')).toEqual([
244
+ { kind: 'moveTo', x: 10.0, y: 10.0 },
245
+ { kind: 'lineTo', x: 15.0, y: 15.0 },
246
+ { kind: 'lineTo', x: 10.0, y: 15.0 },
100
247
  ]);
101
248
  });
102
-
103
- it('maps horizontal and vertical lineto (absolute and relative)', () => {
104
- const path = parseSvgPathData('M0 0 H10 V20 h5 v-5');
105
- expect(collectSegments(path as Path)).toEqual([
106
- { kind: 'moveTo', x: 0, y: 0 },
107
- { kind: 'lineTo', x: 10, y: 0 },
108
- { kind: 'lineTo', x: 10, y: 20 },
109
- { kind: 'lineTo', x: 15, y: 20 },
110
- { kind: 'lineTo', x: 15, y: 15 },
249
+ it('treats repeated relative moveto operands as relative lineto', () => {
250
+ expect(parsed('m1 1 2 2')).toEqual([
251
+ { kind: 'moveTo', x: 1.0, y: 1.0 },
252
+ { kind: 'lineTo', x: 3.0, y: 3.0 },
253
+ ]);
254
+ });
255
+ it('maps horizontal and vertical lineto absolute and relative', () => {
256
+ expect(parsed('M0 0 H10 V20 h5 v-5')).toEqual([
257
+ { kind: 'moveTo', x: 0.0, y: 0.0 },
258
+ { kind: 'lineTo', x: 10.0, y: 0.0 },
259
+ { kind: 'lineTo', x: 10.0, y: 20.0 },
260
+ { kind: 'lineTo', x: 15.0, y: 20.0 },
261
+ { kind: 'lineTo', x: 15.0, y: 15.0 },
111
262
  ]);
112
263
  });
113
-
114
264
  it('parses cubic curves', () => {
115
- const path = parseSvgPathData('M0 0 C1 2 3 4 5 6');
116
- expect(collectSegments(path as Path)).toEqual([
117
- { kind: 'moveTo', x: 0, y: 0 },
118
- { kind: 'cubicCurveTo', control1X: 1, control1Y: 2, control2X: 3, control2Y: 4, x: 5, y: 6 },
265
+ expect(parsed('M0 0 C1 2 3 4 5 6')).toEqual([
266
+ { kind: 'moveTo', x: 0.0, y: 0.0 },
267
+ {
268
+ kind: 'cubicCurveTo',
269
+ controlX1: 1.0,
270
+ controlY1: 2.0,
271
+ controlX2: 3.0,
272
+ controlY2: 4.0,
273
+ x: 5.0,
274
+ y: 6.0,
275
+ },
119
276
  ]);
120
277
  });
121
-
122
- it('reflects the previous cubic control for the S shorthand', () => {
123
- const path = parseSvgPathData('M0 0 C1 2 3 4 5 6 S3 3 4 4');
124
- const segments = collectSegments(path as Path);
125
- expect(segments[2]).toEqual({
278
+ it('reflects the previous cubic control for the s shorthand', () => {
279
+ expect(parsed('M0 0 C1 2 3 4 5 6 S3 3 4 4')[2]).toEqual({
126
280
  kind: 'cubicCurveTo',
127
- control1X: 7,
128
- control1Y: 8,
129
- control2X: 3,
130
- control2Y: 3,
131
- x: 4,
132
- y: 4,
281
+ controlX1: 7.0,
282
+ controlY1: 8.0,
283
+ controlX2: 3.0,
284
+ controlY2: 3.0,
285
+ x: 4.0,
286
+ y: 4.0,
133
287
  });
134
288
  });
135
-
136
- it('uses the current point as the S control when the previous command was not a cubic', () => {
137
- const path = parseSvgPathData('M0 0 L2 2 S3 3 4 4');
138
- const segments = collectSegments(path as Path);
139
- expect(segments[2]).toEqual({
289
+ it('uses the current point as the s control when the previous command was not a cubic', () => {
290
+ expect(parsed('M0 0 L2 2 S3 3 4 4')[2]).toEqual({
140
291
  kind: 'cubicCurveTo',
141
- control1X: 2,
142
- control1Y: 2,
143
- control2X: 3,
144
- control2Y: 3,
145
- x: 4,
146
- y: 4,
292
+ controlX1: 2.0,
293
+ controlY1: 2.0,
294
+ controlX2: 3.0,
295
+ controlY2: 3.0,
296
+ x: 4.0,
297
+ y: 4.0,
147
298
  });
148
299
  });
149
-
150
300
  it('parses quadratic curves', () => {
151
- const path = parseSvgPathData('M0 0 Q1 1 2 2');
152
- expect(collectSegments(path as Path)).toEqual([
153
- { kind: 'moveTo', x: 0, y: 0 },
154
- { kind: 'curveTo', controlX: 1, controlY: 1, x: 2, y: 2 },
301
+ expect(parsed('M0 0 Q1 1 2 2')).toEqual([
302
+ { kind: 'moveTo', x: 0.0, y: 0.0 },
303
+ { kind: 'quadraticCurveTo', controlX: 1.0, controlY: 1.0, x: 2.0, y: 2.0 },
155
304
  ]);
156
305
  });
157
-
158
- it('reflects the previous quadratic control for the T shorthand', () => {
159
- const path = parseSvgPathData('M0 0 Q1 1 2 2 T4 4');
160
- const segments = collectSegments(path as Path);
161
- expect(segments[2]).toEqual({ kind: 'curveTo', controlX: 3, controlY: 3, x: 4, y: 4 });
306
+ it('preserves the quadratic command numeric value in the parsed path', () => {
307
+ const path = parseSvgPathData('M0 0 Q1 1 2 2')!;
308
+ expect(PathCommand.QUADRATIC_CURVE_TO & 0xff).toEqual(3);
309
+ expect(path.commands).toEqual([PathCommand.MOVE_TO & 0xff, 3]);
310
+ });
311
+ it('reflects the previous quadratic control for the t shorthand', () => {
312
+ expect(parsed('M0 0 Q1 1 2 2 T4 4')[2]).toEqual({
313
+ kind: 'quadraticCurveTo',
314
+ controlX: 3.0,
315
+ controlY: 3.0,
316
+ x: 4.0,
317
+ y: 4.0,
318
+ });
319
+ });
320
+ it('uses the current point as the t control when the previous command was not a quadratic', () => {
321
+ expect(parsed('M0 0 L2 2 T4 4')[2]).toEqual({
322
+ kind: 'quadraticCurveTo',
323
+ controlX: 2.0,
324
+ controlY: 2.0,
325
+ x: 4.0,
326
+ y: 4.0,
327
+ });
162
328
  });
163
-
164
329
  it('appends an arc as cubic segments ending at the arc endpoint', () => {
165
- const path = parseSvgPathData('M0 0 A5 5 0 0 1 10 0');
166
- expect(path).not.toBeNull();
167
- const segments = collectSegments(path as Path);
168
- expect(segments[0].kind).toBe('moveTo');
169
- expect(segments[1].kind).toBe('cubicCurveTo');
170
- const last = segments[segments.length - 1] as Extract<PathSegment, { kind: 'cubicCurveTo' }>;
171
- expect(last.x).toBeCloseTo(10, 6);
172
- expect(last.y).toBeCloseTo(0, 6);
330
+ const segments = parsed('M0 0 A5 5 0 0 1 10 0');
331
+ expect(kinds(segments)[0]).toEqual('moveTo');
332
+ expect(kinds(segments)[1]).toEqual('cubicCurveTo');
333
+ const [x, y] = lastPoint(segments);
334
+ expect(Math.abs(x - 10.0) < 5e-7).toBe(true);
335
+ expect(Math.abs(y) < 5e-7).toBe(true);
173
336
  });
174
-
175
337
  it('starts an arc after close at the closed subpath origin', () => {
176
- const path = parseSvgPathData('M0 0 L10 0 Z A5 5 0 0 1 10 0');
177
- const expected = parseSvgPathData('M0 0 L10 0 Z M0 0 A5 5 0 0 1 10 0');
178
- expect(collectSegments(path as Path)).toEqual(collectSegments(expected as Path));
338
+ expect(parsed('M0 0 L10 0 Z A5 5 0 0 1 10 0')).toEqual(parsed('M0 0 L10 0 Z M0 0 A5 5 0 0 1 10 0'));
179
339
  });
180
-
181
340
  it('closes contours and returns the current point to the subpath start', () => {
182
- const path = parseSvgPathData('M0 0 L10 0 L10 10 Z L5 5');
183
- const segments = collectSegments(path as Path);
184
- expect(segments.map((s) => s.kind)).toEqual(['moveTo', 'lineTo', 'lineTo', 'close', 'lineTo']);
185
- // The lineto after Z starts from the subpath origin (0,0) and goes to (5,5).
186
- expect(segments[4]).toEqual({ kind: 'lineTo', x: 5, y: 5 });
341
+ const segments = parsed('M0 0 L10 0 L10 10 Z L5 5');
342
+ expect(kinds(segments)).toEqual(['moveTo', 'lineTo', 'lineTo', 'close', 'lineTo']);
343
+ expect(segments[4]).toEqual({ kind: 'lineTo', x: 5.0, y: 5.0 });
344
+ expect(parsed('M1 1 L9 9 z l1 0')[3]).toEqual({ kind: 'lineTo', x: 2.0, y: 1.0 });
187
345
  });
188
-
189
346
  it('tokenizes numbers with no space before a negative sign', () => {
190
- const path = parseSvgPathData('M0 0L10-5');
191
- expect(collectSegments(path as Path)).toEqual([
192
- { kind: 'moveTo', x: 0, y: 0 },
193
- { kind: 'lineTo', x: 10, y: -5 },
347
+ expect(parsed('M0 0L10-5')).toEqual([
348
+ { kind: 'moveTo', x: 0.0, y: 0.0 },
349
+ { kind: 'lineTo', x: 10.0, y: -5.0 },
194
350
  ]);
195
351
  });
196
-
197
352
  it('tokenizes scientific notation and comma separators', () => {
198
- const path = parseSvgPathData('M1e1,1e1 L2E1,-1.5e1');
199
- expect(collectSegments(path as Path)).toEqual([
200
- { kind: 'moveTo', x: 10, y: 10 },
201
- { kind: 'lineTo', x: 20, y: -15 },
353
+ expect(parsed('M1e1,1e1 L2E1,-1.5e1')).toEqual([
354
+ { kind: 'moveTo', x: 10.0, y: 10.0 },
355
+ { kind: 'lineTo', x: 20.0, y: -15.0 },
202
356
  ]);
203
357
  });
204
-
205
- it('tokenizes leading dots, packed decimals, explicit plus signs, and trailing dots', () => {
206
- const path = parseSvgPathData('M.5.5 L+2 +3 L10. 20.');
207
- expect(collectSegments(path as Path)).toEqual([
358
+ it('tokenizes leading dots packed decimals explicit plus signs and trailing dots', () => {
359
+ expect(parsed('M.5.5 L+2 +3 L10. 20.')).toEqual([
208
360
  { kind: 'moveTo', x: 0.5, y: 0.5 },
209
- { kind: 'lineTo', x: 2, y: 3 },
210
- { kind: 'lineTo', x: 10, y: 20 },
361
+ { kind: 'lineTo', x: 2.0, y: 3.0 },
362
+ { kind: 'lineTo', x: 10.0, y: 20.0 },
211
363
  ]);
212
364
  });
213
-
365
+ it('tokenizes a trailing dot before an exponent and every separator', () => {
366
+ expect(parsed('M1.e1\t\n\r\u{c}2e+1')).toEqual([{ kind: 'moveTo', x: 10.0, y: 20.0 }]);
367
+ });
214
368
  it('rejects a dangling exponent after backtracking the numeric token', () => {
215
- expect(parseSvgPathData('M1e 0')).toBeNull();
369
+ expect(parseSvgPathData('M1e 0') === undefined).toBe(true);
216
370
  });
217
-
218
371
  it('accepts large finite coordinates at both signs', () => {
219
- const path = parseSvgPathData('M1e308 -1e308');
220
- expect(collectSegments(path as Path)).toEqual([{ kind: 'moveTo', x: 1e308, y: -1e308 }]);
372
+ expect(parsed('M1e308 -1e308')).toEqual([{ kind: 'moveTo', x: 1e308, y: -1e308 }]);
373
+ });
374
+ it('accepts an underflowing token as signed zero', () => {
375
+ const segments = parsed('M1e-400 -1e-400');
376
+ const $letElse = segments[0];
377
+ if (!($letElse.kind === 'moveTo')) {
378
+ throw new Error(`expected moveto`);
379
+ }
380
+ const x = $letElse.x;
381
+ const y = $letElse.y;
382
+ expect(x === 0.0 && y === 0.0).toBe(true);
383
+ expect((x > 0 || Object.is(x, 0)) && (y < 0 || Object.is(y, -0))).toBe(true);
221
384
  });
222
-
223
385
  it('returns null when a numeric token overflows at either sign', () => {
224
- expect(parseSvgPathData('M1e999 0')).toBeNull();
225
- expect(parseSvgPathData('M-1e999 0')).toBeNull();
386
+ expect(parseSvgPathData('M1e999 0') === undefined).toBe(true);
387
+ expect(parseSvgPathData('M-1e999 0') === undefined).toBe(true);
226
388
  });
227
-
228
389
  it('parses packed arc flags with no separators', () => {
229
- const path = parseSvgPathData('M0 0A5 5 0 0110 0');
230
- expect(path).not.toBeNull();
231
- const segments = collectSegments(path as Path);
232
- const last = segments[segments.length - 1] as Extract<PathSegment, { kind: 'cubicCurveTo' }>;
233
- expect(last.x).toBeCloseTo(10, 6);
234
- expect(last.y).toBeCloseTo(0, 6);
390
+ const [x, y] = lastPoint(parsed('M0 0A5 5 0 0110 0'));
391
+ expect(Math.abs(x - 10.0) < 5e-7).toBe(true);
392
+ expect(Math.abs(y) < 5e-7).toBe(true);
235
393
  });
236
-
237
- it('returns an empty path for empty or whitespace-only input', () => {
238
- expect(collectSegments(parseSvgPathData('') as Path)).toEqual([]);
239
- expect(collectSegments(parseSvgPathData(' \n ') as Path)).toEqual([]);
394
+ it('resolves a relative arc endpoint against the current point', () => {
395
+ expect(parsed('M2 3 a5 5 0 0 1 10 0')).toEqual(parsed('M2 3 A5 5 0 0 1 12 3'));
396
+ });
397
+ it('returns an empty path for empty or whitespace only input', () => {
398
+ expect(parsed('').length === 0).toBe(true);
399
+ expect(parsed(' \n ').length === 0).toBe(true);
240
400
  });
241
-
242
401
  it('returns null when the path does not begin with a moveto', () => {
243
- expect(parseSvgPathData('L10 10')).toBeNull();
402
+ expect(parseSvgPathData('L10 10') === undefined).toBe(true);
244
403
  });
245
-
246
404
  it('returns null on an unknown command letter', () => {
247
- expect(parseSvgPathData('M0 0 X10 10')).toBeNull();
405
+ expect(parseSvgPathData('M0 0 X10 10') === undefined).toBe(true);
248
406
  });
249
-
250
407
  it('returns null on a short coordinate run', () => {
251
- expect(parseSvgPathData('M0 0 L10')).toBeNull();
252
- expect(parseSvgPathData('M')).toBeNull();
253
- expect(parseSvgPathData('M0 0 C1 2 3 4 5')).toBeNull();
408
+ expect(parseSvgPathData('M0 0 L10') === undefined).toBe(true);
409
+ expect(parseSvgPathData('M') === undefined).toBe(true);
410
+ expect(parseSvgPathData('M0 0 C1 2 3 4 5') === undefined).toBe(true);
254
411
  });
255
-
256
- it('round-trips representative path data through format and back', () => {
412
+ it('round trips representative path data through format and back', () => {
257
413
  const cases = [
258
414
  'M0 0 L10 0 L10 10 Z',
259
415
  'M0 0 C1 2 3 4 5 6 S7 8 9 10',
@@ -262,21 +418,24 @@ describe('parseSvgPathData', () => {
262
418
  'M0 0 A5 5 0 0 1 10 0 A5 5 0 0 1 20 0',
263
419
  ];
264
420
  for (const d of cases) {
265
- const first = parseSvgPathData(d);
266
- expect(first).not.toBeNull();
267
- const reserialized = formatSvgPathData(first as Path);
268
- const second = parseSvgPathData(reserialized);
269
- expect(second).not.toBeNull();
270
- const a = collectSegments(first as Path);
271
- const b = collectSegments(second as Path);
272
- expect(b.length).toBe(a.length);
273
- for (let i = 0; i < a.length; i++) {
274
- expect(b[i].kind).toBe(a[i].kind);
275
- const av = a[i] as unknown as Record<string, number>;
276
- const bv = b[i] as unknown as Record<string, number>;
277
- for (const key of Object.keys(av)) {
278
- if (key === 'kind') continue;
279
- expect(bv[key]).toBeCloseTo(av[key], 6);
421
+ const first = (() => {
422
+ const value = parseSvgPathData(d);
423
+ if (value === undefined) throw new Error('first parse');
424
+ return value;
425
+ })();
426
+ const reserialized = formatSvgPathData(first, undefined);
427
+ const second = (() => {
428
+ const value = parseSvgPathData(reserialized);
429
+ if (value === undefined) throw new Error('second parse');
430
+ return value;
431
+ })();
432
+ const a = collectSegments(first);
433
+ const b = collectSegments(second);
434
+ expect(b.length).toEqual(a.length);
435
+ for (let i = 0; i < a.length; i += 1) {
436
+ expect(kinds(b.slice(i, i + 1))).toEqual(kinds(a.slice(i, i + 1)));
437
+ for (const [bv, av] of values(b[i]).map((item, index) => [item, values(a[i])[index]] as const)) {
438
+ expect(Math.abs(bv - av) < 5e-7, `${d}: ${bv} vs ${av}`).toBe(true);
280
439
  }
281
440
  }
282
441
  }