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

simple-ts-models

v0.1.3

Published

A simple set of Typescript classes for defining models!

Downloads

3

Readme

Simple Typescript models

npm version Build Status codecov npm

Installation

npm install simple-ts-models

DataModel Usage

DataModels can be used to create simple objects that will trigger change events when any of the values change.

import {DataModel} from "simple-ts-models";

// Create a data model from a Javascript object
let m = new DataModel({
    foo: 1,
    bar: 'baz'
});

// Bind to changes once on foo field
m.once('change:foo', (value, previousValue) => {

});
m.foo = 5; // will call function above
m.foo = 3; // will not call function above

// Bind to changes on bar field
m.bind('change:bar', (value, previousValue) => {

});
m.bar = 5; // will call the function above
m.bar = 'foo'; // will also call the function above

// Bind to changes on all fields
m.bind('change', () => {

});
m.foo = 100; // will call the function above
m.bar = 'bar'; // will also call the function above

m.getData(); // Will return {foo: 100, bar: 'bar'}

Model Usage

Models need to be extended and have fields defined in order to set data on them. You can specify field types in the @field decorator. You can easily create your own custom field types for validating your data and implementing your own data manipulation.

import {Model, fields} from "simple-ts-models";

class MyModel extends Model {
    @field(fields.PositiveIntegerField)
    id: number;

    @field(fields.StringField)
    foo: string;

    @field()
    bar: any;
}

let m = new MyModel({
    id: 1 // Can set values from constructor
});

// Can set values from
m.foo = 'string value'; // Will trigger 'change' & 'change:foo' events
m.bar = [1,2,3];  // Will trigger 'change' & 'change:bar' events
m.getData(); // Returns {id: 1, foo: 'string value', bar: [1,2,3]}

Collection Usage

The collection class is an extension of Javascript's built in Array class that can have methods added to it to do operations on a collection of a specific model class.

class ExampleModel extends Model {
    @field()
    id: number;

    @field()
    is_default: boolean;
}

class ExampleCollection extends Collection<ExampleModel> {
    getDefault: () => ExampleModel = (): ExampleModel => {
        for(const i of this) {
            if(i.is_default)
                return i;
        }
    };
}

let m1 = new ExampleModel({id: 1, is_default: false}),
    m2 = new ExampleModel({id: 2, is_default: true}),
    m3 = new ExampleModel({id: 3, is_default: false}),
    c = new ExampleCollection(m1, m2);

// Can use array methods
c.push(m3);
c.length; // Returns 3

// Use method added created in ExampleCollection
c.getDefault() === m2; // true

// Use Collection method getData to get an array of objects containing the data in each model
c.getData(); // Returns [{id: 1, is_default: false},{id: 2, is_default: true},{id: 3, is_default: false}]