0.1.4 • Published 7 years ago

basetype v0.1.4

Weekly downloads
6
License
MIT
Repository
github
Last release
7 years ago

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