SVG Path D Attribute Parser
d-path-parser is a JavaScript library designed to parse the 'd' attribute of SVG `<path>` elements. Its current and only stable version is 1.0.0, released in 2016. The package is lightweight (under 1KB minified and gzipped) and engineered for performance, distinguishing itself from alternatives like `svg-path-parser` by being significantly smaller and faster (6-10x faster depending on input length) while maintaining 100% test coverage. It provides a structured breakdown of SVG path commands, facilitating easier debugging, reading, and manipulation of complex path data. The library supports CommonJS, AMD, and exposes a global function if no module loader is detected. Due to its last update being in 2016, the project is considered abandoned.
Common errors
-
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: ...d-path-parser/index.js require() of ES modules is not supported.
cause Attempting to `require()` an ES Module or using `import` for a CommonJS module in an incompatible environment or build configuration.fixThis specific error likely occurs if a build process attempts to treat `d-path-parser` (a CJS module) as an ESM module when being `require`d by another CJS module, or if a newer Node.js environment tries to `require` it in an ESM context. Ensure your `package.json` `type` field is correctly set, or use dynamic `import()` for CJS modules in ESM contexts: `const parse = await import('d-path-parser').then(mod => mod.default);` -
TypeError: parse is not a function
cause The imported `parse` symbol is not recognized as a function, often due to incorrect import syntax or a broken package installation.fixVerify that `const parse = require('d-path-parser');` is used in a CommonJS context. If using a bundler, ensure it correctly handles CommonJS modules. Reinstall the package (`npm install d-path-parser`) to rule out corruption.
Warnings
- gotcha The parser does not enforce SVG path strictness. It will not throw an error if the input string is not a complete SVG `d` attribute or if the first command is not a `moveTo`. This allows parsing of partial path segments.
- breaking The project is abandoned, with the last release (v1.0.0) and code update occurring in 2016. This means there will be no further updates, bug fixes, or compatibility improvements for newer JavaScript versions or browser environments.
- gotcha The package is a CommonJS module and does not natively support ES Modules (ESM) `import` syntax. Attempting to use `import parse from 'd-path-parser'` directly in a pure ESM Node.js environment or a modern browser build without proper transpilation may lead to errors.
Install
-
npm install d-path-parser -
yarn add d-path-parser -
pnpm add d-path-parser
Imports
- parse
import parse from 'd-path-parser';
const parse = require('d-path-parser');
Quickstart
const parse = require('d-path-parser');
const path1 = "M0,0 l10,10 A14.142 14.142 0 1 1 10,-10 Z";
const commands1 = parse(path1);
console.log('Parsed Path 1:', JSON.stringify(commands1, null, 2));
const path2 = "m10 10c10-10 20-10 30 0";
const commands2 = parse(path2);
console.log('\nParsed Path 2:', JSON.stringify(commands2, null, 2));
/* Example of output for path1:
[
{
"code": "M",
"relative": false,
"end": {
"x": 0,
"y": 0
}
},
{
"code": "l",
"relative": true,
"end": {
"x": 10,
"y": 10
}
},
{
"code": "A",
"relative": false,
"radii": {
"x": 14.142,
"y": 14.142
},
"rotation": 0,
"large": true,
"clockwise": true,
"end": {
"x": 10,
"y": -10
}
},
{
"code": "Z"
}
]*/