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

typedproxy

v1.0.4

Published

A module checks out type of methods parameters and properties of Class by using es6 Proxy

Downloads

6

Readme

TypedProxy

A module checks out type of methods parameters and properties of Class by using es6 Proxy. ##Installation

npm i typedproxy

##Example

const Typed = require('typedproxy');
//MyClass declaration
const TypedMyClass = new Typed(MyClass, { 'nonZeroType' : (value) => { 
        if (value === 0 ) throw new TypeError('nonZeroTypeError');
    }
});
const typedInstance = new TypedMyClass();
typedInstance.nonZeroMethod(1);//ok
typedInstance.nonZeroMethod(0);//throw TypeError

##Usage Suppose you have a es-6 class named TestClass, which contains constructor, some static methods and properties(probably with getter and setter), methods and etc. Probably he is looks like class which i have described below:

class TestClass {
    constructor(strName){
        this.soname = strName;
    }
    static staticMethod(strSomeStaticString) {
        return strSomeStaticString;
    }
    calcSomething(uIntSomeValue){
        return uIntSomeValue;
    }
    get name(){
        if(this._name){
            return this._name;
        } else {
            return 'Name not defined';
        }
    }
    set name(strName){
        this._name = strName;
        return true;
    }
};

If you want check out type of parameters, when invoke methods (or set property value) of this class or instance of this class you may follow next steps:

Include typedproxy module in your application :

const Typed = require('typedproxy');

Decide which types you will use in your application (e.g. str, uInt), and describes object that represents this one:

const types = {
    'str': (value) => {
        const typeOfValue = {}.toString.call(value).slice(8, -1);
        if (typeOfValue !== 'String') {
            throw new TypeError(`str parameter must be String instance, not ${typeOfValue}`);
        }
    },
    'uInt': (value) => {
        const typeOfValue = {}.toString.call(value).slice(8, -1);
        if (typeOfValue !== 'Number') {
            throw new TypeError(`uInt parameter must be Number instance, not ${typeOfValue}`);
        }
    }
};

Create new typed class based on your class and types object that you have described above. :

const TypedTestClass = new Typed(TestClass, types);

The TypedTestClass is typed version of TestClass now and you can invoke static methods (including setter of properties and constructor) of him. If parameters passed into methods are not valid, it will throw an TypeError (based on types object). If number of parameters passed into methods is not correct, it will throw an RangeError (by default):

TypedTestClass.staticMethod('string'); //ok
TypedTestClass.staticMethod(1); //throw TypeError
TypedTestClass.staticMethod('string', 'string'); //throw RangeError

Create new typed instances, which represents TestClass instance with typed feature:

const typedInstance = new TypedTestClass('string');
typedInstance.calcSomething(1); //ok

but

typedInstance.calcSomething('string'); //throw TypeError
typedInstance.calcSomething(1, 2); //throw RangeError

Types

All types that you are planning to use in your application are described by types object. This is simple object wich contains type names and their check out functions. The first characters of the parameters used in your class methods, must match with the names of types. For example :

if you want to use your equalOneType than typed object may be looks like :

typed = {
    'equalOneType' : (value) => {
        if(value !== 1) {
            throw new TypeError(`equalOneType param must be equal 1, not ${value}`); 
        }
    }
};

and method must be looks like :

someMethod(equalOneTypeParam){
    this.count+=equalOneTypeParam;
}