
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
path-data-parser
Advanced tools
I know there are a bunch of SVG path parsers out there, but here's one that I have been using for a few years now. It's small, tree-shakable and provides four simple functions that I have needed in several graphics projects.
From npm
npm install --save path-data-parser
The code is shipped as ES6 modules.
The module exposes 4 methods
This is the main method that parses the SVG path data. You pass in the path string and it returns an array of Segments
. A segment has a key
which is the commands, e.g. M
or h
or C
; and data
which is an array of numbers used by the command
Segment {
key: string;
data: number[];
}
example:
import { parsePath } from 'path-data-parser';
const segments = parsePath('M10 10 h 80 v 80 h -80 C Z');
This is essentially the opposite of the parsePath
command. It outputs a path string from an array of Segment
objects.
import { parsePath, serialize, absolutize } from 'path-data-parser';
const segments = parsePath('M10 10 h 80 v 80 h -80 Z');
const absoluteSegments = absolutize(segments); // Turns relative commands to absolute
const outputPath = serialize(absoluteSegments);
console.log(outputPath); // M 10 10 H 90 V 90 H 10 Z
Translates relative commands to absolute commands. i.e. all commands that use relative positions (lower-case ones), turns into absolute position commands (upper-case ones).
See the serialize
example above.
Normalize takes a list of absolute segments and outputs a list of segments with only four commands: M, L, C, Z
. So every segment is described as move, line, or a bezier curve (cubic).
This is useful when translating SVG paths to non SVG mediums - Canvas, or some other graphics platform. Most such platforms will support lines and bezier curves. It also simplifies the cases to consider when modifying these segments.
import { parsePath, serialize, absolutize, normalize } from 'path-data-parser';
const segments = parsePath(' M 10 80 Q 52.5 10, 95 80 T 180 80');
const absoluteSegments = absolutize(segments);
const normalizedSegments = normalize(absoluteSegments);
// M 10 80 C 38.33 33.33, 66.67 33.33, 95 80 C 123.33 126.67, 151.67 126.67, 180 80
FAQs
Yet another SVG path parser. This one's tiny
The npm package path-data-parser receives a total of 483,202 weekly downloads. As such, path-data-parser popularity was classified as popular.
We found that path-data-parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.