Package Exports
- variable-type
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (variable-type) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
variable-type
A simple javascript(
less then 1 kb) library, runtime type checking for variable and similar objects. 一个非常简单(仅 1 kb)的用于做变量结构校验的 JavaScript 模块。Simplified from prop-types.
1. Install
npm i --save variable-type
Then import it.
import VT from 'variable-type'; // ES6
var VT = require('variable-type'); // ES5 with npm2. Usage
Here is some simple examples. More you can see in test.js file.
Arraytype.
var arr = ['hello', 'world', 25, new Date(1992, 8, 1)];
var types = VT.arrayOf(
VT.oneOfType([
VT.number,
VT.string,
VT.instanceOf(Date)
])
);
VT.check(arr, types); // will get true. Objecttype.
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. Complexexample.
// 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.oneOf([1, '1']),
g: VT.shape({
h: VT.oneOfType([
VT.shape({
i: VT.arrayOf(
VT.oneOfType([
VT.number,
VT.string,
VT.bool,
VT.shape({
j: VT.func
})
])
)
})
])
})
}); // Then will get true.3. 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
- VT.oneOf
- VT.oneOfType
- VT.arrayOf
- VT.shape
- VT.and
- VT.or
You can 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.
4. Test
npm i
npm run testLicense
ISC@hustcc.