All Projects → hustcc → variable-type

hustcc / variable-type

Licence: MIT license
👏 ~ 1 kb. Schema validation. 一个只有 1 kb 的用于变量结构校验的库。

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to variable-type

python-pyfields
Define fields in python classes. Easily.
Stars: ✭ 39 (+62.5%)
Mutual labels:  type-check
think
Instagram clone using React Native with funcional components, React Hooks, React Navigation 4x and Reactotron debugger
Stars: ✭ 20 (-16.67%)
Mutual labels:  prop-types
alphabetical-sorter
VScode extension - For variable alphabetical re-order
Stars: ✭ 30 (+25%)
Mutual labels:  variable
NamingThings
Content on tips, tricks, advice, practices for naming things in in software/technology
Stars: ✭ 31 (+29.17%)
Mutual labels:  variable
Gluten
2 Axes/Variable/Super Soft/Display
Stars: ✭ 28 (+16.67%)
Mutual labels:  variable
prop-types-definition
Patch for prop-types to get property type definition in runtime
Stars: ✭ 15 (-37.5%)
Mutual labels:  prop-types
AndroidOMRHelper
An android application for validating images of OMR sheets before they are sent for processing.
Stars: ✭ 38 (+58.33%)
Mutual labels:  checking
VF-BlenderAutoSaveRender
Automatically saves a numbered or dated image after every render and can extend the Blender output path with dynamic variables
Stars: ✭ 34 (+41.67%)
Mutual labels:  variable
Tourney
3 axes/variable/no curves
Stars: ✭ 20 (-16.67%)
Mutual labels:  variable
Epilogue
2 axes/Variable/18 styles/Sans
Stars: ✭ 104 (+333.33%)
Mutual labels:  variable
Anybody
3 axes/variable/sans/super compressed to super extended
Stars: ✭ 73 (+204.17%)
Mutual labels:  variable
Trispace
2 axes/variable/mostly monospace
Stars: ✭ 49 (+104.17%)
Mutual labels:  variable
Pyre Check
Performant type-checking for python.
Stars: ✭ 5,716 (+23716.67%)
Mutual labels:  type-check
is-lite
Type check tool
Stars: ✭ 16 (-33.33%)
Mutual labels:  type-check

variable-type

A high-performance javascript(less then 1 kb) library, runtime type checking for variable and similar objects.

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

Inspired by prop-types.

npm Version Build Status Coverage Status npm download npm License

1. Install

npm i --save variable-type

Then import it.

import VT from 'variable-type';

2. API & Types

Before use it to check variable, you should make your Types.

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.typeOf(String)
  • VT.in(Array)
  • VT.arrayOf(Type)
  • VT.shape(TypeObject)
  • VT.and(TypeArray)
  • VT.or(TypeArray)
  • VT.not(Type)
  • VT.apply(Function)

The Type has 2 API:

  • check(value)
  • optional(): convent the type into optional.

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. Usage examples

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

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

VT.and([
   VT.string
   VT.in(['hustcc', 1992]),
]).check('hustcc');

VT.or([
   VT.number,
   VT.string,
]).check('hustcc');
  • Array type.
const arr = ['hello', 'world', 25, new Date(1992, 8, 1)];
 
const types = VT.arrayOf(
  VT.or([
    VT.number,
    VT.string,
    VT.instanceOf(Date)
  ])
);

types.check(arr); // will get true. 
  • Object type.
const obj = {
  name: 'hustcc',
  boy: true,
  birthday: new Date(1992, 8, 1)
};
 
const types = VT.shape({
  name: VT.string,
  boy: VT.bool,
  birthday: VT.instanceOf(Date)
});

types.check(obj); // will get true. 
  • Complex example.
// The only API `check`.
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
            })
          ])
        )
      })
    ])
  })
}).check({
  a: true,
  b: 1,
  c: 'str',
  d: function() {},
  e: new Date(),
  f: '1',
  g: {
    h: {
      i: [
        '1',
        2,
        true,
        {
          j: function() {}
        }
      ]
    }
  }
}); // Then will get true.
  • Optional type
VT.shape({
  name: VT.string,
  birthday: VT.string,
  sex: VT.string.optional()
}).check({
  name: 'hustcc',
  birthday: '1992-08-01'
}); // Then will get true.

4. Test & Perf

# install dependence
$ npm i

# run unit test
$ npm run test

# run performance test
$ npm run perf

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

License

MIT@hustcc.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].