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-check

v0.2.4

Published

Type checker for typescript

Downloads

7,384

Readme

TypeChecker for JavaScript/Typescript

Author : lessu

Description

A flexable type checker.

tested under typescript 2.3.4 and 2.4.2, 2.8.1; es5 is now supported

Installation

npm install ts-check --save 

Test

mocha tests/index.js

Features

Flexable,easy-to-extend type checker for complex object.

example

TypeChecker.checkType(sku,"SetSkuItem[]",{
    SetSkuItem : {
        key         : "string",
        stock       : "number",
        unit        : "number",
        price       : "number",
        cost_price  : "number",
        options     : "ItemOptions[]"
    },
    ItemOptions : {
        filed_id : "number",
        filed_options:["number","string"]
    }
});

Update Log

  • v0.2.3

    1.New feature added : now we can get failed log message by access lastError variable.

    2.Fix type error synax under typescript 2.8.1

Usage

import

import * as TypeChecker from "ts-checker";

Basic Type

Basic type include

export enum BasicTypes{
    number      = "number",
    string      = "string",
    object      = "object",
    any         = "any",
    null        = "null",
    undefined   = "undefined"
}

Check basic types :

TypeCheker.checkType(undefined,"undefined");

TypeCheker.checkType(null,"any");

TypeCheker.checkType(123,"number");

//Notice a string number is also a number type in weak Number mode;
TypeCheker.checkType("123","number",{},{weakNumber:true});

Check Array

any length

assert(TypeCheker.checkType([1,2,3,4,5,6],"number[]"));

check length

assert(TypeCheker.checkType([1,2,3],"number[3]"));
assert(!TypeCheker.checkType([1,2,3],"number[2]"));

array array

assert(TypeCheker.checkType([[1],[2],[3]],"number[1][3]"));
assert(TypeCheker.checkType([[1],[2],[3]],"number[][3]"));

Check function

Signature

((value:any)=>boolean);

Usage

TypeCheker.checkType( { a : "1" },(value)=>{
    return typeof value["a"] == "string";
});

Nested Check

TypeCheker.checkType({a:"1"},{
    a : "string"
});

TypeCheker.checkType({
    a:"1",
    b:{c:1},
    c:{
        d:{
            e:1
        }
    }
},{
    a : "string",
    b : function(value:any){
        return typeof value.c == "number";
    },
    c : {
        d : {
            e : "number"
        }
    }
});

Type array

The checked value can be string, number or {a:number};

TypeCheker.checkType({a:1},["string","number",{
    a : "number"
}]);

Customized types DefainedTypes

//definedTypes is a {customType => check} object.
TypeCheker.checkType(value,type,definedTypes);
TypeCheker.checkType(
//to check
{
    biggerThan0 : 2
},
//type
{
    biggerThan0 : ">0"
},
//custom type,define >0 as a custom type , and can be used in typeChecker
{
    ">0" : function( value : any ){
        if(typeof value == "number"){
            return value > 0;
        }else{
            return parseInt(value) > 0
        }
    }
});

//custom type can be nested
TypeCheker.checkType({
    biggerThan0 : [1,2,3,4]
},{
    biggerThan0 : ">0[]"
},{
    ">0" : function(value:any){
        if(typeof value == "number"){
            return value > 0;
        }else{
            return parseInt(value) > 0
        }
    }
});

//or a real life example
TypeChecker.checkType(sku,"SetSkuItem[]",{
    SetSkuItem : {
        key         : "string",
        stock       : "number",
        unit        : "number",
        price       : "number",
        cost_price  : "number",
        options     : "ItemOptions[]"
    },
    ItemOptions : {
        filed_id : "number",
        filed_options:["number","string"]
    }
});

DefinedType With args

If a DefinedType is a function, args are supported;

Notice,Args will be convert to pure string ; every , is recognized as a splitor, So DO NOT call like CustomType(a,"1,2,3"), It will convert to

[
    "a",
    "\"1",
    "2",
    "3\""
]
assert(!
    TypeCheker.checkType({
        biggerThan0 : 6
    },<any>{
        biggerThan0 : "range(0,5)"
    },{
        "range" : function(value:any,args:string[]){
            if(args.length==0){
                return false;
            }else if(args.length == 1){
                return value> parseFloat(args[0]);
            }else{
                return value >= parseFloat(args[0]) && value <= parseFloat(args[1]);
            }
        }
    })
);

Default defined type

Custom type can be add to default defaultDefinedChecker object; so that can be used any where.

TypeCheker.defaultDefinedChecker[">0"] = function(value:any){
    if(typeof value == "number"){
        return value > 0;
    }else{
        return parseInt(value) > 0
    }
};
assert(TypeCheker.checkType({a:1},{a:">0"}));
assert(!TypeCheker.checkType({a:-1},{a:">0"}));

License

MIT