高性能变量结构校验库 variable-type

MIT
JavaScript
跨平台
2017-08-23
小薇

variable-type

一个非常简单的(仅 1 kb)高性能的用于做变量结构校验的 JavaScript 模块。

1. 安装

npm i --save variable-type

Then import it.

import VT from 'variable-type'; // ES6
var VT = require('variable-type'); // ES5 with npm

2. API & Types

The unique API is check(variable, type). And the library contains Types below:

  • VT.bool

  • VT.func

  • VT.number

  • VT.string

  • VT.object

  • VT.array

  • VT.any

  • VT.null

  • VT.undefined

  • VT.instanceOf(Class)

  • VT.in(Array)

  • VT.arrayOf(Type)

  • VT.shape(TypeObject)

  • VT.and(TypeArray)

  • VT.or(TypeArray)

  • VT.not(Type)

  • VT.apply(Function)

You can see all the usage in the test cases file.

If more Types are needed, welcome to send a pull request, or put an issue to me.

3. 使用示例

Here is some examples. More you can see in test.js file.

  • Simple usage

VT.check(1992, VT.number);
VT.check('hustcc', VT.string);
VT.check(Math.min, VT.func);
VT.check(true, VT.bool);
VT.check({}, VT.object);
VT.check([1, 2, 3], VT.array);
VT.check(null, VT.null);
VT.check(undefined, VT.undefined);
VT.check(new Date(), VT.instanceOf(Date));
VT.check('hustcc', VT.in(['hustcc', 'hust', 'cc']));
  • And / Or / Not

VT.check('hustcc', VT.not(VT.in(['hustcc', 'cc'])));
VT.check('hustcc', VT.and([
   VT.string
   VT.in(['hustcc', 1992]),
]));
VT.check('hustcc', VT.or([
   VT.number,
   VT.string,
]));
  • Array type.

var arr = ['hello', 'world', 25, new Date(1992, 8, 1)];
 
var types = VT.arrayOf(
  VT.or([
    VT.number,
    VT.string,
    VT.instanceOf(Date)
  ])
);

VT.check(arr, types); // will get true.
  • Object type.

var obj = {
  name: 'hustcc',
  boy: true,
  birthday: new Date(1992, 8, 1)
};
 
var types = VT.shape({
  name: VT.string,
  boy: VT.bool,
  birthday: VT.instanceOf(Date)
});

VT.check(obj, types); // will get true.
  • Complex example.

// The only API `check`.
VT.check({
  a: true,
  b: 1,
  c: 'str',
  d: function() {},
  e: new Date(),
  f: '1',
  g: {
    h: {
      i: [
        '1',
        2,
        true,
        {
          j: function() {}
        }
      ]
    }
  }
}, VT.shape({
  a: VT.bool,
  b: VT.number,
  c: VT.string,
  d: VT.func,
  e: VT.instanceOf(Date),
  f: VT.in([1, '1']),
  g: VT.shape({
    h: VT.or([
      VT.shape({
        i: VT.arrayOf(
          VT.or([
            VT.number,
            VT.string,
            VT.bool,
            VT.shape({
              j: VT.func
            })
          ])
        )
      })
    ])
  })
}); // Then will get true.

4. Test & Perf

npm i

npm run test

npm run perf

[OPS] variable-type / prop-types = 2.495

协议

ISC@hustcc.

加载中

评论(0)

暂无评论

暂无资讯

暂无问答

Open Declaration java.lang.reflect.TypeVariable...

TypeVariable is the common superinterface for type variables of kinds. A type variable is created the first time it is needed by a reflective method, as specified in this packag...

2013/10/21 17:34
26
0
【C++注意事项】5 Top-level const , The auto and decltype Type Specifier

top-level const As we’ve seen, a pointer is an object that can point to a different object. As a result, we can talk independently about whether a pointer is const and whether...

2015/09/08 21:49
3
0
2: Getting Help(Working with Jupyter console )

Jupyter console has a robust built-in help system. You can get help in several ways: You can type ? after starting the console. This will display help about Jupyter. You can exi...

2016/09/15 14:00
1
0
oracle集合与记录

PL/SQL中有标量类型和组合类型,组合类型分为集合与记录。

2017/08/18 14:15
21
0
一本介绍C指针的书--指针是什么1.3

To review: · A variable is declared by giving it a type and a name (e.g. int k;) · A pointer variable is declared by giving it a type and a name (e.g. int *ptr) where the aste...

2012/08/04 14:29
29
0
Scala - Case Classes and Pattern Matching

Scala does not offers you a language construct called "Enum", however, there is indeed a Enum class but its usage is quite different from what you know from C++/C#/java. One of ...

2013/05/23 07:49
251
0
pmpasswd

Encrypting Passwords Command Line Programs: infacmd infasetup pmcmd pmrep You can encrypt passwords to create an environment variable to use with infacmd, infasetup, pmcmd, and ...

2014/07/21 11:21
74
0
3: Environment Variables(Working with programs)

So far, we've been creating shell variables. These variables can be accessed only within the bash shell. Another type of variable is an environment variable. These can be access...

2016/09/14 00:42
0
0
关于NullPointerException

1、When you declare a reference variable (i.e. an object) you are really creating a pointer to an object. Consider the following code where you declare a variable of primitive ...

2016/08/10 23:02
3
0

没有更多内容

加载失败,请刷新页面

返回顶部
顶部