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

tpl-inator

v0.0.2

Published

An experimental template generator that can be used with Angular to enable template static type checking.

Downloads

2

Readme

tpl-inator

An experimental template generator that can be used with Angular to enable template static type checking. This had been made possible by the use of Typescript's keyof operator.

By using this template generator, errors in the component's template, e.g. wrong spelling of property and/or function, may be reduced because the properties being accessed on the component's template are now checked during the compilation of code instead of during runtime.

Sample usage in Angular

import { Component } from '@angular/core';
import { Card } from './models/card.model';
import { Template, TemplateElement, TemplateGenerator } from 'tpl-inator';

class AppComponentTemplate extends Template<AppComponent> {
  getName(): string {
    return 'app.component';
  }
  getComponentPrototype(): any {
    return AppComponent.prototype;
  }
  getTemplateBody(): TemplateElement[] {
    return [
      this.tag('h1').attrs({
        
        // Accessing of property AppComponent#selectedCard
        '*ngIf': this.prop('selectedCard').str()
      
        // An incorrectly spelled property name!
        // This line will not compile because AppComponent#selectedCrd does not exist.
        // '*ngIf': this.prop('selectedCrd').str()
        //                    ^~~~~~~~~~~~~
        // Will even be highlighted if using an editor like VS Code
        
      }).els([
        
        // Just another html string
        this.str(`Selected card's rank: `),
        
        // Interpolation of the property access `AppComponent#selectedCard#rank`
        this.prop('selectedCard').prop('rank').itrpl8()
        // For now, this will only check the presence of the component's property
        // on the first call of prop method on the function call chain, i.e.
        // the `prop('rank')` function call will not check if `rank` is really a property
        // of the component's property `selectedCard` from `prop('selectedCard')`.
        
      ]).finish(),
      this.tag('ib-card-container').attrs({
      
        // Accessing of property AppComponent#cards
        '[cards]': this.prop('cards').str(), 

        // calling of function AppComponent#cardClicked
        '(cardClicked)': this.call('cardClicked').arg('$event').str(),
        // For now, the call method will throw a runtime error if the argument
        // is not of type function. Checking the declared arguments like `$event`
        // is not yet possible.

      }).finish()
    ]
  }
}

@Component({
  selector: 'app-root',
  
  // this does the magic :)
  template: TemplateGenerator.generateStringFrom(new AppComponentTemplate()),
  
  // Planning to support separate the generation of the HTML files from
  // the template so you can use this instead. Please look at the next section
  // to know more about this.
  templateUrl: './app.component.tpl.html',
  // 'app.component' is the string i returned on the `getName` function on the
  // template class so i'd just append it with '.tpl.html' and that's the template's
  // file name.
  
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  cards = [
    new Card('A', 'hearts'),
    new Card('2', 'clubs'),
    new Card('6', 'diamonds'),
    new Card('K', 'diamonds', true),
    new Card('10', 'spades')
  ];
  
  selectedCard: Card = null;
  
  cardClicked(card: Card) {
    this.selectedCard = card;
    console.log(JSON.stringify(card));
  }
}

The code above will generate the following HTML template.

<h1 *ngIf="selectedCard">Selected card's rank: {{selectedCard.rank}}</h1>
<ib-card-container [cards]="cards" (cardClicked)="cardClicked($event)"></ib-card-container>

Generating HTML files from the template class

This is not yet possible but I've already thought of this. I've thought that if the file which contains the template class has a lots of code in it, the file's compilation will take long. So it is best to separate the template class from the component class. I might be wrong, so feel free to correct me. The more you know!

Instead of passing the string that will be returned by the TemplateGenerator.generateStringFrom to the template property of the Component decorator, simply pass the name of the template you provided on the override of getName function in the TemplateClass extension and append it with an extension .tpl.html.

I'm thinking of adding a hook like mechanism so that after the typescript files compile, the next step is to generate or update the HTML files which corresponds to each template classes. I'm not an expert in such subject but I'll try my best. Of course, anyone's help is much appreciated. :)