Skip to content

parsePath

Parses SVG path data (the d attribute) into commands, as written: the letters keep their case (absolute or relative), H/V/S/T/A stay what they are, and a command repeated without its letter (L 1 2 3 4) becomes one command per set of numbers (M repeats as L, m as l).

It follows the SVG grammar: numbers may be glued (1-2, .5.5, 1e-3), separators are optional, and the two arc flags may be written without any separator (a1 1 0 011 1).

Available since v3.1.10

import { parsePath } from '@helpers4/geometry';
// or, from the all-in-one package (same code, one install):
import { parsePath } from 'helpers4/geometry';
parsePath(d: string): PathCommand[]
ParameterTypeDescription
dstringThe path data

PathCommand[] — The commands, in order. Empty for an empty or blank string

Letters keep their case; a repeated command becomes one command per set of numbers.

parsePath('M0 0h10v10z').map((c) => c.type) // => ['M', 'h', 'v', 'z']

One command of SVG path data: its letter (uppercase is absolute, lowercase relative to the current point) and its numbers, as written in the d attribute.

LetterCommandNumbers
M L Tmove, line, smooth quadraticx y
H Vhorizontal, vertical linex or y
Ccubic Bézierx1 y1 x2 y2 x y
S Qsmooth cubic, quadraticx2 y2 x y or x1 y1 x y
Aelliptical arcrx ry rotation large-arc sweep x y
Zclose the subpathnone
interface PathCommand {
  type: string;
  values: number[];
}

View source on GitHub