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

码农软件 · 软件分类 · 常用工具包 · 2019-08-13 11:27:14

软件介绍

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.

本文地址:https://codercto.com/soft/d/12266.html

Windows API编程范例入门与提高

Windows API编程范例入门与提高

东方人华 / 清华大学出版社 / 2004-1-1 / 38.00

本书通过大量实用、经典的范例,以Visual Basic为开发平台由浅入深地介绍了Windows API编程的基本方法和大量的实用技巧。本书采用实例带动知识点的形式,使读者快速入门并逐步得到提高。本书每节即是一个实例,操作步骤详尽,所用到的源文件均可在网站下载。读者可以按照操作步骤完成每个实例的制作,并根据自己的喜好进行修改、举一反三。 本书内容翔实,凝结了作者多年的编程经验,既适合......一起来看看 《Windows API编程范例入门与提高》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试