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

tsplate

v1.6.0

Published

Lightweight typescript templating library

Downloads

2

Readme

TSPlate

A lightweight but powerful typescript template validation library

Installation

npm install tsplate

Why TSPlate?

Unifies the definition of model types with model validation, with easy support for custom validation rules. Ontop of this it is extremely lightweight, with no dependencies and a package size of just a few kb.

It can easily be added to an existing project using the class template interface (T.Class)

Documentation

For a brief of the What, Why & How of TSPlate, take a look at the documentation

Examples

You can run any of these examples by cloning this repo any using npm run example [filename].js. See the examples folder for filenames. For example, npm run example basic-validation.js.

Validating a class using decorators

import T from 'tsplate';

// this function type checks each of the input strings mathches a propertery on the class
// and also type checks that the number of input strings matches the number of arguments in the constructor
@T.constructor('name', 'age');
class Example {
    // this function type checks the template type against the property type
    @T.template(T.String)
    public name: string;

    @T.template(T.Int)
    public age: number;

    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }
}

// TExample has type Template<Example, any>
// The class will transit as an object {name: string, age: number}
const TExample = T.AutoClass(Example);

Validating a class using the class template generator

import T from 'tsplate';

class Example {
    public name: string;
    public age: number;

    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }
}

// TExample has type Template<Example, any>
// second parameter defines the instance keys and templates for constructor parameters
// the true transit type of this template would be {name: string, age: number}
const TExample = T.Class(Example, [['name', T.String], ['age', T.Int]]);


// the constructor properties are type checked against their templates
// e.g., the following will highlight a type error on ['age', T.String]
const TExample2 = T.Class(Example, [['name', T.String], ['age', T.String]]);

Validating JSON

import T from 'tsplate';

// TColour has type Template<'red' | 'blue' | 'green', string>
const TColour = T.Enum('red', 'blue', 'green');

// TPerson has type Template<{name: string, age: number}, {name: string, age: number}> 
const TPerson = T.Object({
    name: T.String,
    age: T.Int
});

// TCar has type Template<{name: string, colour: 'red' | 'blue' | 'green', passengers: {name: string, age: number}[]}, ...> 
// (here ... will be the same as the first type argument)
const TCar = T.Object({
    name: T.String,
    colour: TColour,
    passengers: T.Array(TPerson)
});

const data = JSON.parse(`
{
    "name": "mini",
    "colour": "red",
    "passengers": [
        {"name": "bob", "age": 23},
        {"name": "bill", "age": 43}
    ]
}
`);

if (TCar.valid(data)) {
    const car = TCar.toModel(data);

    // car will now also have the appropriate type
    console.log(car.name); // 'mini'
    console.log(car.colour); // 'red'
    car.passengers.forEach(person => {
        /* name: bob, age: 23
           name: bill, age: 43 */
        console.log(`name: ${person.name}, age: ${person.age}`)
    });
}

Writing a custom template

import T, { Template } from 'tsplate';

class Name {
    public name: string;
    constructor(name: string) {
        this.name = name;
    }
}

const TName: Template<Name, string> = {
    valid: T.String.valid,
    toModel: (o: string) => new Name(o),
    toTransit: (name: Name) => name.name
};

const transit = 'example';
if (TName.valid(transit)) {
    const name: Name = TName.toModel(transit);

    console.log(name.name); // 'example'

    const nameTransit: string = TName.toTransit(name);

    console.log(nameTransit); // 'example'
}

License

MIT