const PathCommands = (function()
{
const
regSplit = /([\+\-.0-9]+)|\s*[\s,]\s*/,isLowerCase = str => /^[a-z]*$/.test(str),codPath =
{ M : [ 'x','y'],L : [ 'x',H : [ 'x' ],V : [ 'y' ],Z : [],C : [ 'x1','y1','x2','y2','y' ],S : [ 'x2',Q : [ 'x1',T : [ 'x',A : [ 'rX','rY','rotation','arc','sweep','y' ]
};
return function(pathStr)
{
let
res = [],arr = pathStr.split(regSplit).filter(Boolean),relativ = false
;
for (let i=0;i<arr.length;)
{
let cmd = isNaN(arr[i]) ? arr[i++] : relativ ? 'l' : 'L'
relativ = isLowerCase(cmd)
res.push( codPath[cmd.toUpperCase()].reduce((a,c)=>{a[c]=Number(arr[i++]);return a},{cmd}))
}
return res
}
})()
// test :
const pathData = document.querySelector('svg path').getAttributeNS(null,'d')
let drawCommands = PathCommands(pathData)
drawCommands.forEach((dc,i)=>console.log(i,JSON.stringify(dc)))
svg {
background : whitesmoke;
margin : 1em;
}
path {
fill : none;
stroke : blue;
stroke-width : 4;
stroke-linecap : round;
stroke-linejoin : round;
}
/** test part **/
.as-console-wrapper { max-height:100% !important; top:0; left:40% !important; width:60%; overflow: scroll !important; }
.as-console-row::after { display:none !important; }
<svg width="200" height="100" viewBox="-40 -390 800 400">
<path d="M1.825,-11.000z" />
</svg>