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

svg2ts

v0.5.0

Published

## Convert Standard SVG / Parameterized\* SVG to TypeScript code / Angular components.

Downloads

17

Readme

svg2ts

Convert Standard SVG / Parameterized* SVG to TypeScript code / Angular components.

svg2ts svg2ts

Installation

npm install svg2ts -g

Usage

svg2ts Usage:

  --input ./svg           svg source dir
  --output ./svg-ts-out   ts output dir
  --blueprint typescript  blueprint to use 'typescript'[default] 'angular'
  --module svg-to-ts      Module name for angular blueprint
  --config svg2ts.json    Use an external config file

Overview

This tool converts SVGs to a TypeScript representation. The tool accepts 2 kinds of svg files:

  • Standard SVG.
  • Parameterized SVG (with custom template notation).

For example this svg: (rectangle-viewbox-width-height.svg)

<svg xmlns="http://www.w3.org/2000/svg" width="101" height="101" viewBox="0 0 100 100">
    <rect x="0" y="0" width="100" height="100" fill="#f00"/>
</svg>

Will be converted to this Typescript code:

export const RectangleViewboxWidthHeight = {
  width: 101,
  height: 101,
  svgHash: 'ᗢ5n9aqp',
  viewBox: { minx: 0, miny: 0, width: 100, height: 100 },
  name: 'rectangle-viewbox-width-height',
  svg: '<rect class="blue" x="0" y="0" width="100" height="100" fill="#f00"/>'
};

Parameterized SVGs

If you need to parameterize some properties/values of the svg, svg2ts will look for any parameter inside the svg with the following notation:

{{defaultvalue|variableName}}

For this svg:

<svg xmlns="http://www.w3.org/2000/svg" width="101" height="101" viewBox="0 0 100 100">
    <rect fill="{{#f00|fill}}" x="{{0|x}}" y="{{0|y}}" width="{{100|width}}" height="{{100|height}}"/>
</svg>

We will get this ouput:

export interface ParameterizedContext {
  uuid?: number;
  fill?: string;
  x?: number;
  y?: number;
  width?: number;
  height?: number;
}

export const Parameterized = {
  width: 101,
  height: 101,
  svgHash: 'ᗢ5n9aqp',
  viewBox: { minx: 0, miny: 0, width: 100, height: 100 },
  name: 'parameterized',
  svg: '<rect fill="{{fill}}" x="{{x}}" y="{{y}}" width="{{width}}" height="{{height}}"/>',
  contextDefaults: { fill: '#f00', x: 0, y: 0, width: 100, height: 100 }
};

The svg output template will have the final replacement keys while the default values will be exported to the contextDefaults property.

It will also autogenerate an interface of the parameterized values.

Isolation

Svg id's and css styles will be namespaced with a unique key to prevent id's and class names collisions at runtime.

For example, if we process this svg:

<svg xmlns="http://www.w3.org/2000/svg" width="101" height="101" viewBox="0 0 100 100">
    <style>
      .rect { fill: #f00; }
    </style>
    <rect id="rectangleID" x="0" y="0" width="50" height="40"/>
</svg>

Will be converted to

<svg class="ᗢyvbmy7-{{uuid}}" xmlns="http://www.w3.org/2000/svg" width="101" height="101" viewBox="0 0 100 100">
    <style>
      .ᗢyvbmy7-{{uuid}} .rect { fill: #f00; }
    </style>
    <rect id="ᗢyvbmy7-{{uuid}}-rectangleID" fill="#000" x="0" y="0" width="50" height="40"/>
</svg>

Angular

svg2ts can generate an Angular module containing all the needed assets, and some components to help working with it.

To do this use the Angular --blueprint option in the command line:

Blueprint

> svg2ts -i ./source ./dest --blueprint angular

Module

You can provide a custom module name with the --module option or the default one (svg-to-ts) will be used. This module name will determine the selector basename of the resulting components.

> svg2ts -i ./source ./dest --blueprint angular --module my-module-name

Output

The generated output will consist of:

  • An Angular module file
  • An assets directory with all the assets converted to typescript files
  • A specific svg icon "base" component related to this module
  • A components directory containing the parameterized Angular Components

Angular Usage

Using the default --module option value svg-to-ts

Importing

Import the module inside your app module.

@NgModule({
  ...
  imports: [..., SvgToTsModule] // Generated Module Name
  ...
})
export class AppModule {}

Use it in the templates as:

Standard svg icons
<svg-to-ts [icon]="'print'"></svg-to-ts>

Where [icon] is the name property the assets/print.ts file.

export const Print = {
  ...
  name: 'print'
  ...
};
Parametrized svg

Default values:

<svg-to-ts [icon]="'parameterized'" [context]="customParamsObject"></svg-to-ts>

Custom values (parameterized.svg):

<svg xmlns="http://www.w3.org/2000/svg" width="101" height="101" viewBox="0 0 100 100">
    <rect fill="{{#f00|fill}}" x="{{0|x}}" y="{{0|y}}" width="{{100|width}}" height="{{100|height}}"/>
</svg>

Inside the angular component html

<svg-to-ts [icon]="'parameterized'" [context]="customParamsObject"></svg-to-ts>

Inside the angular component

...
public context = {
  fill: '#ff3bd7',
  x: 20,
  y: 16,
  width: 65,
  height: 30;
};
...

Where [icon] is the name property the svg typescript file and [context] the context object used to replace values in the component.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.