Coder Social home page Coder Social logo

hustcc / variable-type Goto Github PK

View Code? Open in Web Editor NEW
23.0 4.0 3.0 105 KB

๐Ÿ‘ ~ 1 kb. Schema validation. ไธ€ไธชๅชๆœ‰ 1 kb ็š„็”จไบŽๅ˜้‡็ป“ๆž„ๆ ก้ชŒ็š„ๅบ“ใ€‚

Home Page: http://git.hust.cc/variable-type

License: MIT License

TypeScript 100.00%
variable-type prop-types variable type-check checking

variable-type's Introduction

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.

variable-type's People

Contributors

hustcc avatar ryantate13 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

variable-type's Issues

Skip type checking on production

I believe it's nice to have type checking when working with weak dynamic types, especially with shapes. As far as I know type checking makes sense during developing process or for those people who will read the code in order to have understanding with what types of variables the code works. Languages with type checking will not compile your code until types are correct. So, in runtime it basically makes no sense to check types of variables.
Modern building tools during a building process can read variables from node's environment and replace their value with booleans, e.g.:

// source
if (process.env.NODE_ENV === 'production') {

}

// build (in case if NODE_ENV = production)
if (true) {

}

After that minifiers (like uglifyjs) can exclude such code which is always under false block, because it's never used. This helps to avoid unnecessary calculations on production. React's prop-types does exactly like that.

@hustcc What do you thing about adding the same behavior to variable-type?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.