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

ts-common-decorators

v0.9.12

Published

Some decorators

Downloads

4

Readme

npm version CI

Common decorators

Just some decorators that don't need any dependencies, yet are very handy.

Observe

Just a simple non recursive observer, very handy for simple use cases

import {Observe} from 'ts-common-decorators'
import Axios from 'axios';
class Model {
    public id: number;
    
    @Observe(Model.prototype.toBackend)
    public someField: string = '';
    
    
    toBackend() {
        Axios.post('/model/put/'+this.id,this);
    }
}

const model = new Model();
model.someField = 'some data';

Cached

Why call should we call this twice?

import {Cached} from 'ts-common-decorators';
import Axios from 'axios';

class Foo {
    @Cached()
    bar(id: number|string): Promise<any> {
        return Axios({
            method: 'get',
            url: '/model/'+id,
        })
    }
}

const foo = new Foo;

const model1 = foo.bar(1); // Logs first time called
const model1Again = foo.bar(1); // Doesnt fetch model1 again, its cached!

console.log(model1 === model1Again); // true, refers to the same object

QueuedPromise

Javascript is async, but not everything is javascript. From time to time, you want a promise that is queued. Without making a massive callback hell

import {QueuedPromise} from 'ts-common-decorators';
class Person {
    name: string;
    constructor(name: string) {
        this.name = name
    }

    @QueuedPromise()
    async assignTask(task: string) : Promise<any> {
        console.log('['+this.name+'] start: '+task); // did task
        // wait 300 ms to simulate some "work"
        await new Promise(resolve => setTimeout(() => resolve(),300));
        console.log('['+this.name+'] done: '+task); // did task
    }

}

const alice = new Person('alice');
const bob = new Person('bob');
[alice,bob].forEach((self,i,everyone) => {
    self.assignTask('write code');
    self.assignTask('test code');
    self.assignTask('fix bug');
    self.assignTask('commit code');
    self.assignTask('push code').then(() => everyone[1-i].assignTask('review code'));
});

Promise once

Don't you hate it to see something is promises twice at the same time? If functions are requesting a model, its sad it retrieves it twice from the database. So, @PromiseOnce ensures you it only fetches it once

Take a look at the example. While only 2 requests will take place, /model/1 and /model/7. While model 1 is called twice

import {PromiseOnce} from 'ts-common-decorators';
import Axios from 'axios';

class Foo {
    @PromiseOnce
    bar(id? :number) {
        return Axios({
            method: 'get',
            url: '/model/'+id,
        })
    }
}

const foo = new Foo();

const fetch1 = foo.bar(1), 
    fetch2 = foo.bar(1),

// Fetching another model
    fetch3 = foo.bar(7);

Promise.all([fetch1,fetch2,fetch3]).then(data => {
    // Both fetch 1 and 2 are a request for model's instance 1.
    console.log('fetch 1 and 2 refer to the same object?', data[0] === data[1]); // true

    // fetch 3 is a request for model's instance 7.
    console.log('fetch 1 and 3 refer to the same object?',data[0] === data[2]); // false
});

Static

Sometimes you see these purely static classes. Its better to make them safe so they cant be created

import {Static} from 'ts-common-decorators';

@Static
class Foo {
    static bar() {
    
    }
}

// Error, can't call new on a static class
const foo = new Foo;

Metadata

Give a method some metadata

import {MetaData} from 'ts-common-decorators';

class Foo {
    @MetaData({something: 'special'})
    bar() {
    
    }
}
const foo = new Foo();

console.log(MetaData.get(foo.bar).something); // special

Logger

Getting tired of figuring out if a method gets called while changing it?

import {Logger} from 'ts-common-decorators'


class Foo {
    @Logger(() => console.log('called Foo.bar'))
    bar() {
    
    }
}
const foo = new Foo();
foo.bar(); // Logs it on the console. Handy debug function

More

More to come in the future