basetype

0.1.4 • Public • Published

basetype

Provide basic type validation functions and simple type composition.

Build Status Coverage Status

install

npm i basetype --save

type function

Type function is a boolean function, accept one argument and return boolean value.

eg:

let {likeArray} = require('basetype');
 
likeArray(123); // => false
likeArray([1, 2, 3]); // => true

basic types

isUndefined, isNull, isFalsy, likeArray, isString, isObject, isFunction, isNumber, isBool, isNode, isPromise, isReadableStream, isWritableStream

compose types

  • and
let newType = and(isObject, isArray);
newType([1, 2, 3]); // true
newType({}); // false
  • or
let newType = or(isNumber, isBool);
newType(4); // true
newType(true); // true
newType("123"); // false
  • not
let newType = not(isNumber);
newType(1); // false
newType("123"); // true
  • mapType
let type = mapType({
    a: isNumber,
    b: isBool
});
 
type({
    a: 3,
    b: false
}); //true
 
type({
    a: 3,
    b: {}
}); //false
  • listType
let type = listType(isNumber);
type([1, 2, 3]); //true
type([1, {}, 3]); //false
  • any
any([1, 2, 3], isNumber); //true
any([1, {}, 3], isNumber); //false
  • exist
exist([1, 2, 3], isString); //false
exist([1, {}, 3], isNumber); // true

function arguments type

let fun = funType((a, b) => a + b, [
    isNumber,
    isNumber
]);
 
fun(1, 2); // 3
fun('1', 2); // TypeError

Readme

Keywords

Package Sidebar

Install

npm i basetype

Weekly Downloads

4

Version

0.1.4

License

MIT

Last publish

Collaborators

  • ddchen