Skip to content

lessu/ts-check

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Releases

No releases published

Packages

No packages published