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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@coremyslo/svg-to-icon

v1.1.0

Published

This module represents a helper class for reading and optimizing SVG files, returning their name based on path and file content, and generating glyphs ## Installation

Downloads

687

Readme

@coremyslo/svg-to-icon npm GitHub license

This module represents a helper class for reading and optimizing SVG files, returning their name based on path and file content, and generating glyphs

Installation

$ yarn add @coremyslo/svg-to-icon

Usage

import { Icon } from "@coremyslo/svg-to-icon";

const pathToFile = path.join(__dirname, "./assets/icons/icon-home.svg")
const icon = new Icon(pathToFile);

console.log(icon.name) // returns "icon-home"

await icon.read();
console.log(icon.content) // returns file's content as a string

icon.optimize();
console.log(icon.content) // returns optimized file content as a string

Usage for icon font generation

The following example is based on svgicons2svgfont

// ...

const unicode = "0xE900";
const glyph = icon.getGlyph({
    metadata: {
        name: icon.name,
        unicode: [String.fromCharCode(parseInt(unicode, 16))],
    },
});

const getFontStream = (glyphs: Readable[]): Promise<Uint8Array> => new Promise(resolve => {
    const chunks: Uint8Array[] = [];
    const fontReadStream = new SVGIcons2SVGFontStream({
        fontName: "iconFont",
        fontHeight: 1024,
        normalize: true,
        round: 1,
        log (): void { },
    }).on("data", (chunk: Uint8Array) => {
        chunks.push(chunk);
    }).on("end", () => {
        resolve(Buffer.concat(chunks));
    });
    fontReadStream.write(glyphs[0]);
    fontReadStream.end();
});
console.log((await getFontStream([glyph])).toString());


/* will log

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" >
<svg xmlns="http://www.w3.org/2000/svg">
<defs>
  <font id="iconFont" horiz-adv-x="1024">
    <font-face font-family="iconFont"
      units-per-em="1024" ascent="1024"
      descent="0" />
    <missing-glyph horiz-adv-x="0" />
    <glyph glyph-name="icon-home"
      unicode="&#xE900;"
      horiz-adv-x="1024" d="M235 192H395V459H629V192H789V608L512 816L235 608zM171 128V640L512 896L853 640V128H565V395H459V128zM512 505z" />
  </font>
</defs>
</svg>

*/

API

new Icon(sourceFilePath, options)

  • sourceFilePath: string

Absolute path to the icon file

  • options: object
    • case: "snake" | "pascal" | "camel" | "kebab" | "header" | "constant" - optional, define case for icon name, see case package for details.
      import { Icon } from "@coremyslo/svg-to-icon";
      
      const pathToFile = path.join(__dirname, "./assets/icons/icon-home.svg")
      const icon = new Icon(pathToFile, { case: "camel" });
      console.log(icon.name) // logs "iconHome"
    • sourceDirPath: string - optional, absolute path to root folder for icon. If not set, only the filename of the icon will be considered as the name of the icon.
      import { Icon } from "@coremyslo/svg-to-icon";
      
      const pathToFile = path.join(__dirname, "./assets/icons/icon-home.svg")
      const icon = new Icon(pathToFile, { sourceDirPath: "/assets/" });
      console.log(icon.name) // logs "icons-icon-home"

read(): Promise<this>

Asynchronously reads the file specified by the sourceFilePath parameter passed to the constructor.

optimize(config: Config): this

Synchronously rewrites icon.content with the optimized version of the SVG, using the svgo package.

  • config: Config - optional, configuration object for the svgo package. If not set, the default configuration will be used.

getGlyph(options): Readable

Returns a node Readable stream object.

  • options: object options passed to node's Readable.from under the hood. This method is used to generate a glyph from the SVG icon.

content: string

  • Contains the file content after read() has been executed.

name: string

  • Contains the name based on the file name and root folder if sourceDirPath was passed to the constructor.