npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@svg-fns/info

v1.0.0

Published

Lightweight, tree-shakeable utilities to extract structured information (dimensions, aspect ratio, bounding boxes, colors) from SVG elements. Works in both browser and Node.js.

Readme

@svg-fns/info

test codecov Version Downloads npm bundle size NPM License

Lightweight, tree-shakeable utilities to extract structured information from SVGs. Works seamlessly in both browser and Node.js (with DOM shims like JSDOM).

Part of the svg-fns ecosystem — modular SVG utilities inspired by date-fns.


✨ Features

  • Get dimensions (width, height)
  • Compute aspect ratio (width ÷ height)
  • Calculate bounding boxes (browser-native or Resvg fallback)
  • Extract unique fill and stroke colors
  • ⚡ Zero dependencies · Fully tree-shakeable · Runs anywhere

📦 Installation

npm install @svg-fns/info

or

pnpm add @svg-fns/info

or

yarn add @svg-fns/info

🚀 Usage

import { getSvgDimensions, getSvgAspectRatio, getSvgBBox, getSvgColors } from "@svg-fns/info";

const svg = document.querySelector("svg")!;

// Dimensions
console.log(getSvgDimensions(svg));
// → { width: 800, height: 600 }

// Aspect ratio
console.log(getSvgAspectRatio(svg));
// → 1.333...

// Bounding box
console.log(getSvgBBox(svg));
// Browser → DOMRect
// Node.js → Promise<DOMRect> (via Resvg or fallback)

// Colors
console.log(getSvgColors(svg));
// → { fills: ["#000", "#f00"], strokes: ["#333"], colors: ["#000", "#f00", "#333"] }

📝 Note: These APIs require an SVGSVGElement. To parse an SVG string, use @svg-fns/io:

import { parseSvg } from "@svg-fns/io";
import { getSvgDimensions } from "@svg-fns/info";

const svgElement = parseSvg("<svg width='100' height='50'/>");
console.log(getSvgDimensions(svgElement));
// → { width: 100, height: 50 }

⚡ Performance Notes

  • Browser: Uses native DOM APIs (getBBox(), getComputedStyle) → fast and accurate.

  • Node.js:

    • With @resvg/resvg-js → accurate bounding boxes (Rust-based).
    • Without Resvg → fallback approximation by traversing child elements.
  • Aspect ratio: Returns Infinity if height = 0 (instead of NaN).


📚 API Reference

getSvgDimensions(svg: SVGSVGElement)

  • Returns { width, height }.
  • Uses width/height attrs first, then falls back to viewBox.

getSvgAspectRatio(svg: SVGSVGElement)

  • Returns width ÷ height.
  • Returns Infinity if height = 0.

getSvgBBox(svg: SVGSVGElement)

  • Browser: Returns DOMRect (sync).
  • Node.js: Returns Promise<DOMRect> (async import of Resvg).
  • Fallback → approximate bbox traversal.

getSvgColors(svg: SVGSVGElement)

  • Returns { fills: string[], strokes: string[], colors: string[] }.
  • Browser → uses computed styles (includes CSS inheritance).
  • Node.js → scans element attributes only.

🔗 Related Packages


🛠️ Contributing

We welcome contributions! 🎉

  • Open an issue for bugs/feature requests.
  • PRs should include tests + docs.
  • See our contributing guide.

📜 License

Licensed under the MPL-2.0 open-source license.