@flighthq/path-formats 0.5.1-next.920.da8f530 → 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,222 +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, 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);
85
+ });
86
+ it('does not partially mutate the destination on malformed input', () => {
87
+ let path = createPath('nonZero');
88
+ appendSvgPathData(path, 'M1 2 L3 4');
89
+ const commands = path.commands;
90
+ const data = path.data;
91
+ expect(!appendSvgPathData(path, 'M10 20 L30')).toBe(true);
92
+ expect(path.commands).toEqual(commands);
93
+ expect(path.data).toEqual(data);
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
+ });
100
+ });
101
+
102
+ describe('explainSvgPathData', () => {
103
+ it('returns null for valid input and locates malformed input', () => {
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'));
26
130
  });
27
131
  });
28
132
 
29
133
  describe('formatSvgPathData', () => {
30
- it('emits absolute M/L/C/Q/Z commands per segment kind', () => {
31
- const path = createPath();
134
+ it('emits absolute m l c q z commands per segment kind', () => {
135
+ let path = createPath('nonZero');
32
136
  appendSvgPathData(path, 'M0 0 L10 0 Q15 5 20 0 C22 2 24 2 26 0 Z');
33
- 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');
34
138
  });
35
-
36
139
  it('rounds coordinates and trims trailing zeros with a precision option', () => {
37
- const path = createPath();
140
+ let path = createPath('nonZero');
38
141
  appendSvgPathData(path, 'M0.123456 0.5 L10.987654 20');
39
- 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');
40
143
  });
41
-
42
144
  it('emits full precision by default', () => {
43
- const path = createPath();
145
+ let path = createPath('nonZero');
44
146
  appendSvgPathData(path, 'M0.5 0.25 L1.5 2.75');
45
- 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');
46
149
  });
47
-
48
- it('emits no A command — arcs are stored as cubics', () => {
49
- const path = createPath();
150
+ it('emits no a command arcs are stored as cubics', () => {
151
+ let path = createPath('nonZero');
50
152
  appendSvgPathData(path, 'M0 0 A5 5 0 0 1 10 0');
51
- const d = formatSvgPathData(path);
52
- expect(d).not.toContain('A');
153
+ const d = formatSvgPathData(path, undefined);
154
+ expect(!d.includes('A')).toBe(true);
53
155
  expect(d.startsWith('M0 0C')).toBe(true);
54
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
+ });
55
226
  });
56
227
 
57
228
  describe('parseSvgPathData', () => {
58
229
  it('parses absolute moveto and lineto', () => {
59
- const path = parseSvgPathData('M0 0 L10 10');
60
- expect(path).not.toBeNull();
61
- expect(collectSegments(path as Path)).toEqual([
62
- { kind: 'moveTo', x: 0, y: 0 },
63
- { 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 },
64
233
  ]);
65
234
  });
66
-
67
235
  it('treats repeated moveto operands as implicit lineto', () => {
68
- const path = parseSvgPathData('M0 0 1 1 2 2');
69
- expect(collectSegments(path as Path)).toEqual([
70
- { kind: 'moveTo', x: 0, y: 0 },
71
- { kind: 'lineTo', x: 1, y: 1 },
72
- { 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 },
73
240
  ]);
74
241
  });
75
-
76
242
  it('resolves relative commands against the current point', () => {
77
- const path = parseSvgPathData('m10 10 l5 5 l-5 0');
78
- expect(collectSegments(path as Path)).toEqual([
79
- { kind: 'moveTo', x: 10, y: 10 },
80
- { kind: 'lineTo', x: 15, y: 15 },
81
- { 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 },
82
247
  ]);
83
248
  });
84
-
85
- it('maps horizontal and vertical lineto (absolute and relative)', () => {
86
- const path = parseSvgPathData('M0 0 H10 V20 h5 v-5');
87
- expect(collectSegments(path as Path)).toEqual([
88
- { kind: 'moveTo', x: 0, y: 0 },
89
- { kind: 'lineTo', x: 10, y: 0 },
90
- { kind: 'lineTo', x: 10, y: 20 },
91
- { kind: 'lineTo', x: 15, y: 20 },
92
- { 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 },
93
262
  ]);
94
263
  });
95
-
96
264
  it('parses cubic curves', () => {
97
- const path = parseSvgPathData('M0 0 C1 2 3 4 5 6');
98
- expect(collectSegments(path as Path)).toEqual([
99
- { kind: 'moveTo', x: 0, y: 0 },
100
- { 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
+ },
101
276
  ]);
102
277
  });
103
-
104
- it('reflects the previous cubic control for the S shorthand', () => {
105
- const path = parseSvgPathData('M0 0 C1 2 3 4 5 6 S3 3 4 4');
106
- const segments = collectSegments(path as Path);
107
- 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({
108
280
  kind: 'cubicCurveTo',
109
- control1X: 7,
110
- control1Y: 8,
111
- control2X: 3,
112
- control2Y: 3,
113
- x: 4,
114
- 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,
115
287
  });
116
288
  });
117
-
118
- it('uses the current point as the S control when the previous command was not a cubic', () => {
119
- const path = parseSvgPathData('M0 0 L2 2 S3 3 4 4');
120
- const segments = collectSegments(path as Path);
121
- 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({
122
291
  kind: 'cubicCurveTo',
123
- control1X: 2,
124
- control1Y: 2,
125
- control2X: 3,
126
- control2Y: 3,
127
- x: 4,
128
- 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,
129
298
  });
130
299
  });
131
-
132
300
  it('parses quadratic curves', () => {
133
- const path = parseSvgPathData('M0 0 Q1 1 2 2');
134
- expect(collectSegments(path as Path)).toEqual([
135
- { kind: 'moveTo', x: 0, y: 0 },
136
- { 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 },
137
304
  ]);
138
305
  });
139
-
140
- it('reflects the previous quadratic control for the T shorthand', () => {
141
- const path = parseSvgPathData('M0 0 Q1 1 2 2 T4 4');
142
- const segments = collectSegments(path as Path);
143
- 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
+ });
144
328
  });
145
-
146
329
  it('appends an arc as cubic segments ending at the arc endpoint', () => {
147
- const path = parseSvgPathData('M0 0 A5 5 0 0 1 10 0');
148
- expect(path).not.toBeNull();
149
- const segments = collectSegments(path as Path);
150
- expect(segments[0].kind).toBe('moveTo');
151
- expect(segments[1].kind).toBe('cubicCurveTo');
152
- const last = segments[segments.length - 1] as Extract<PathSegment, { kind: 'cubicCurveTo' }>;
153
- expect(last.x).toBeCloseTo(10, 6);
154
- 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);
336
+ });
337
+ it('starts an arc after close at the closed subpath origin', () => {
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'));
155
339
  });
156
-
157
340
  it('closes contours and returns the current point to the subpath start', () => {
158
- const path = parseSvgPathData('M0 0 L10 0 L10 10 Z L5 5');
159
- const segments = collectSegments(path as Path);
160
- expect(segments.map((s) => s.kind)).toEqual(['moveTo', 'lineTo', 'lineTo', 'close', 'lineTo']);
161
- // The lineto after Z starts from the subpath origin (0,0) and goes to (5,5).
162
- 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 });
163
345
  });
164
-
165
346
  it('tokenizes numbers with no space before a negative sign', () => {
166
- const path = parseSvgPathData('M0 0L10-5');
167
- expect(collectSegments(path as Path)).toEqual([
168
- { kind: 'moveTo', x: 0, y: 0 },
169
- { 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 },
170
350
  ]);
171
351
  });
172
-
173
352
  it('tokenizes scientific notation and comma separators', () => {
174
- const path = parseSvgPathData('M1e1,1e1 L2E1,-1.5e1');
175
- expect(collectSegments(path as Path)).toEqual([
176
- { kind: 'moveTo', x: 10, y: 10 },
177
- { 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 },
178
356
  ]);
179
357
  });
180
-
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([
360
+ { kind: 'moveTo', x: 0.5, y: 0.5 },
361
+ { kind: 'lineTo', x: 2.0, y: 3.0 },
362
+ { kind: 'lineTo', x: 10.0, y: 20.0 },
363
+ ]);
364
+ });
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
+ });
368
+ it('rejects a dangling exponent after backtracking the numeric token', () => {
369
+ expect(parseSvgPathData('M1e 0') === undefined).toBe(true);
370
+ });
181
371
  it('accepts large finite coordinates at both signs', () => {
182
- const path = parseSvgPathData('M1e308 -1e308');
183
- 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);
184
384
  });
185
-
186
385
  it('returns null when a numeric token overflows at either sign', () => {
187
- expect(parseSvgPathData('M1e999 0')).toBeNull();
188
- expect(parseSvgPathData('M-1e999 0')).toBeNull();
386
+ expect(parseSvgPathData('M1e999 0') === undefined).toBe(true);
387
+ expect(parseSvgPathData('M-1e999 0') === undefined).toBe(true);
189
388
  });
190
-
191
389
  it('parses packed arc flags with no separators', () => {
192
- const path = parseSvgPathData('M0 0A5 5 0 0110 0');
193
- expect(path).not.toBeNull();
194
- const segments = collectSegments(path as Path);
195
- const last = segments[segments.length - 1] as Extract<PathSegment, { kind: 'cubicCurveTo' }>;
196
- expect(last.x).toBeCloseTo(10, 6);
197
- 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);
198
393
  });
199
-
200
- it('returns an empty path for empty or whitespace-only input', () => {
201
- expect(collectSegments(parseSvgPathData('') as Path)).toEqual([]);
202
- 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);
203
400
  });
204
-
205
401
  it('returns null when the path does not begin with a moveto', () => {
206
- expect(parseSvgPathData('L10 10')).toBeNull();
402
+ expect(parseSvgPathData('L10 10') === undefined).toBe(true);
207
403
  });
208
-
209
404
  it('returns null on an unknown command letter', () => {
210
- expect(parseSvgPathData('M0 0 X10 10')).toBeNull();
405
+ expect(parseSvgPathData('M0 0 X10 10') === undefined).toBe(true);
211
406
  });
212
-
213
407
  it('returns null on a short coordinate run', () => {
214
- expect(parseSvgPathData('M0 0 L10')).toBeNull();
215
- expect(parseSvgPathData('M')).toBeNull();
216
- 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);
217
411
  });
218
-
219
- it('round-trips representative path data through format and back', () => {
412
+ it('round trips representative path data through format and back', () => {
220
413
  const cases = [
221
414
  'M0 0 L10 0 L10 10 Z',
222
415
  'M0 0 C1 2 3 4 5 6 S7 8 9 10',
@@ -225,21 +418,24 @@ describe('parseSvgPathData', () => {
225
418
  'M0 0 A5 5 0 0 1 10 0 A5 5 0 0 1 20 0',
226
419
  ];
227
420
  for (const d of cases) {
228
- const first = parseSvgPathData(d);
229
- expect(first).not.toBeNull();
230
- const reserialized = formatSvgPathData(first as Path);
231
- const second = parseSvgPathData(reserialized);
232
- expect(second).not.toBeNull();
233
- const a = collectSegments(first as Path);
234
- const b = collectSegments(second as Path);
235
- expect(b.length).toBe(a.length);
236
- for (let i = 0; i < a.length; i++) {
237
- expect(b[i].kind).toBe(a[i].kind);
238
- const av = a[i] as unknown as Record<string, number>;
239
- const bv = b[i] as unknown as Record<string, number>;
240
- for (const key of Object.keys(av)) {
241
- if (key === 'kind') continue;
242
- 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);
243
439
  }
244
440
  }
245
441
  }