strict-method

0.0.8 • Public • Published

strictmethod

strict method

example

 var m = require("strict-method");

 var user = {
    _name:"leo",
    _age:22,
    change:m(String,{min:5,default:"liang"},Number,function(name,age){
        this._name = name;
        this._age = age;
    })
 }

 user.change("brighthas",28); // not error.
 console.log(user._name);  // brighthas
 console.log(user._age);   //  28


 user.change("bri",28);
 console.log(user._name);  // brighthas
 console.log(user._age);   //  28

 user.change(null,25)
 console.log(user._name);  // liang
 console.log(user._age);   //  25

npm install

npm install strict-method

component install

component install brighthas/strict-method

core strict type

    Date
      var me = m(Date,function(){})
      me(new Date())   // pass

    Object
      var me = m(Object,function(){},true) // 'true' meaning is no throw error, return ParamError object .
      me(null)      // pass
      me("leo")     // pass
      var err = me(undefined) // return error
      console.log(err instanceof ParamError)  // true

    String
      var me = m(String,function(){})
      me("")   // pass
      me(null) // throw error
      me(13)   // throw error

    Boolean
      var me = m(Boolean,function(){})
      me(false) // pass
      me(true)  // pass
      me(1)     // throw error
      me(0)     // throw error

    RegExp
      var me = m(RegExp,function(){})
      me(new RegExp(...))   // pass
      me(/abc/)             // pass
      me("/abc/")           // throw error

    Array
      var me = m(Array,function(){})
      me([])   // pass
      me(new Array())   // throw error

    Number
      var me = m(String,function(){})
      me(13)   // pass
      me(23.6) // pass
      me("13") // throw error

    Function
      var me = m(Function,function(){})
      me(function(){})  // pass

    Arguments
      var obj  = {
         m1:function(){
            this._m2(arguments);  // pass
            this._m2("hi");       // throw error
         },
         _m2:m(m.Arguments,function(){
            ...
         })
      }

      obj.m1('hi');

custom converter and validater

use m.converter(Type,converter) to set Type's converter. use m.validater(Type,validater) to set Type's validater.

    var m = require("strict-method");

    // set converter for Type.
    m.converter(Type,function converter(value){
        if(value.name){
            return new Type(value.name);
        }else{
            throw new Error("convert Type error!");
        }
    })

    // set validater for Type.
    m.validater(Type,function validater(value,params){
         if(params.len && value.name.length === params.len){
         }else{
            throw new Error;
         }
    });

    function Type(name){
         this.name = name;
    }

    function Article(){
        this._name = null;
        this._type = null;
    }

    Article.prototype = {
        setName:m(String,function(name){
            this._name = name;
        }),
        setType:m(Type,{len:5},function(type){
            this._type = type;
        })
    }

    var article = new Article();

    article.setType({name:"ttttt"});

    console.log(article._type instanceof Type)  // true
    console.log(article._type.name) // ttttt

    article.setType(new Type("abc"));
    console.log(article._type.name) // ttttt

default value

   ...

   setName:m(String,{default:"brighthas"},function(name){
            this._name = name;
   })

   ...

   .setName() equal .setName("brighthas")

   ...

dumb

    ...
    setType:m(Type,{len:5},function(type){
        this._type = type;
    })
    ...

    // set dumb=true , then if convert or validate fail , no throw error , return ParamError object .
    // and default dumb = false , then if convert or validate fail , then throw error.

    ...
    setType:m(Type,{len:5},function(type){
        this._type = type;
    },true)
    ...

Number type validater

   Number validater
     m(Number,{min:3,max:12} ... )

String type validater

   String validater
     m(String,{min:3,max:12} ... )
     m(String,{regexp:/abc/} ...)
     m(String,{len:10} ... )
     m(String,{type:"mail"} ... )

     // type : mail | url | ip | ipv4 | ipv6 | int | float

LICENSE

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i strict-method

Weekly Downloads

2

Version

0.0.8

License

MIT

Last publish

Collaborators

  • brighthas