A <path> element can usually be found inside an <svg> element and has an attribute d that represents the definition of the outline of a shape.
A brief summary about this attribute:
M).M 10 10 L 20 20 contains unnecessary spaces and could be expressed more compactly as M10 10L20 20).L in M 10 20 L 20 10 L -10 -20 and use M 10 20 L 20 10 -10 -20 instead).Your job is to build a parser that will convert this string in an array of commands, where each array element is an object with the command letter and an array of parameters.
pathDataParser("") ➞ []
pathDataParser("M 0 0") ➞ [{command: 'M', parameters: [0, 0]}]
pathDataParser("M 1 1.5 L 0 1.5 0 0.5 1 0.5 0.5 0 0 0.5 1 1.5 1 0.5 0 1.5" ➞ [
{command: "M", parameters: [1, 1.5]},
{command: "L", parameters: [0, 1.5, 0, 0.5, 1, 0.5, 0.5, 0, 0, 0.5, 1, 1.5, 1, 0.5, 0, 1.5]}
]
pathDataParser("M 0,1 h 1 v -1 h 1 v 1 h 1 C 2,1 3,3 1.5,3 C 0,3 1,1 0,1 z" ➞ [
{command: "M", parameters: [0, 1]},
{command: "h", parameters: [1]},
{command: "v", parameters: [-1]},
{command: "h", parameters: [1]},
{command: "v", parameters: [1]},
{command: "h", parameters: [1]},
{command: "C", parameters: [2, 1, 3, 3, 1.5, 3]},
{command: "C", parameters: [0, 3, 1, 1, 0, 1]},
{command: "z", parameters: []}
]
z (or Z) command is the only one without parameters, in this case return an empty array (see last command of example #4)parameters array contains numbers, not strings, so you'll have to convert them.4 is the same as 0.40-4 is equal to 0 -41.2.34 is actually 2 different numbers: 1.2 and 0.34