Registry / serialization / path-data-parser

path-data-parser

JSON →
library0.1.0jsnpmunverified

path-data-parser is a lightweight and tree-shakable JavaScript library designed for parsing and manipulating SVG path data strings. It currently stands at version 0.1.0, indicating a stable but early-stage release, likely following an 'as-needed' or infrequent release cadence given its focused utility. Its key differentiators include its small footprint and a direct API offering four core functions: `parsePath` for converting SVG path strings into an array of segment objects, `serialize` for converting segment arrays back into path strings, `absolutize` for transforming relative path commands to absolute ones, and `normalize` for simplifying path segments to only M, L, C, and Z commands. This normalization is particularly useful for rendering SVG paths on non-SVG graphics platforms like Canvas, providing a consistent data structure for further processing.

npm install path-data-parser
INSTALL
IMPORT
SIG · PATH-DATA-PARSER
P
path-data-parser
serializationjavascriptv0.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parsePath
import { parsePath } from 'path-data-parser';
const parsePath = require('path-data-parser').parsePath;
The package is shipped as ES6 modules only, so CommonJS require() will not work directly.
serialize
import { serialize } from 'path-data-parser';
const serialize = require('path-data-parser').serialize;
Use ES module import syntax for all exports.
absolutize
import { absolutize } from 'path-data-parser';
const absolutize = require('path-data-parser').absolutize;
All functions are named exports.
normalize
import { normalize } from 'path-data-parser';
const normalize = require('path-data-parser').normalize;
Ensure your environment supports ES modules (e.g., 'type': 'module' in package.json for Node.js).

Demonstrates parsing, absolutizing, serializing, and normalizing SVG path data. It shows how to convert a path string to segments, modify them (e.g., to absolute coordinates), and convert them back to a string, or simplify them to M, L, C, Z commands for other graphics contexts.

import { parsePath, serialize, absolutize, normalize } from 'path-data-parser'; // Type definition for a segment, inferred from README type Segment = { key: string; data: number[]; }; // Example 1: Basic parsing const pathString1 = 'M10 10 h 80 v 80 h -80 C Z'; const segments1: Segment[] = parsePath(pathString1); console.log('Parsed segments 1:', segments1); // Expected output: [{ key: 'M', data: [10, 10] }, { key: 'h', data: [80] }, ...] // Example 2: Absolutize and serialize const pathString2 = 'M10 10 h 80 v 80 h -80 Z'; const segments2: Segment[] = parsePath(pathString2); const absoluteSegments: Segment[] = absolutize(segments2); const outputPath: string = serialize(absoluteSegments); console.log('Absolute and serialized path:', outputPath); // Expected output: M 10 10 H 90 V 90 H 10 Z // Example 3: Normalize absolute segments const pathString3 = ' M 10 80 Q 52.5 10, 95 80 T 180 80'; const segments3: Segment[] = parsePath(pathString3); const absoluteSegments3: Segment[] = absolutize(segments3); const normalizedSegments: Segment[] = normalize(absoluteSegments3); console.log('Normalized path segments:', normalizedSegments); // Expected output: [{ key: 'M', data: [10, 80] }, { key: 'C', data: [38.33, 33.33, 66.67, 33.33, 95, 80] }, ...] console.log('All operations complete.');
Debug
Known issues
gotchaThe library is shipped exclusively as ES6 modules. Direct CommonJS 'require()' statements will fail in environments not configured to handle ESM.
fix
Use ES module import syntax (`import { ... } from 'path-data-parser'`) and ensure your project or runtime environment (e.g., Node.js with 'type': 'module' in package.json, or a bundler like webpack/Rollup) is configured for ES modules.
affects: >=0.1.0
gotchaAs a 0.1.0 release, while functional, the API might undergo minor breaking changes in future 0.x.x versions before a 1.0.0 stable release.
fix
Review release notes carefully when upgrading between 0.x.x versions, as minor versions might not strictly adhere to semantic versioning until 1.0.0.
affects: 0.x.x
gotchaThe parser expects valid SVG path data strings. Malformed input may lead to unexpected parsing results or errors, as extensive input validation might not be present due to the library's focus on tiny size.
fix
Ensure that input path data strings conform to the SVG Path Data specification (e.g., correct command syntax, valid number sequences). Pre-validate user-provided path data if necessary.
affects: >=0.1.0
Errors
Common errors & fixes
SyntaxError: Cannot use import statement outside a module
Attempting to use ES module import syntax (`import`) in a CommonJS environment without proper configuration.
fix
Ensure your Node.js project's `package.json` contains `"type": "module"` or use a bundler (like Webpack or Rollup) to transpile your code if targeting a CommonJS environment.
TypeError: parsePath is not a function
Incorrectly attempting to access named exports from a CommonJS `require()` call, or running the code in a non-ESM environment when the library is ESM-only.
fix
The library is ES module only. Use `import { parsePath } from 'path-data-parser';` in a properly configured ES module context. Do not use `require()`.
Error: Invalid SVG path segment detected
The input SVG path string contains malformed data, such as an incorrect number of arguments for a command, or unsupported/invalid characters.
fix
Carefully review the SVG path data string to ensure it strictly follows the SVG Path Data specification. Validate inputs from external sources before passing them to the parser.
Upgrade
Version history
0.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
path-data-parser — npm install path-data-parser · libregistry