man-type

small async and sync cutomizable simple type check library

Usage no npm install needed!

<script type="module">
  import manType from 'https://cdn.skypack.dev/man-type';
</script>

README

Man Type

Small simple and customizable type check library;


Usage

First install package

npm i man-type
const is = require("man-type");

is(arg?: any, sameType?: string | boolean | null | Array<string | boolean | null>, deep?: boolean);

Less if condition less complexibility

Example:

if( typeof x === "object" ) {
    throw new Error(...)
}

// with man-type
if( is( x, "object") ) {
    throw new Error(...)
}

You can use it instead of using ! operator

if( typeof x !== "object" ) {
    throw new Error(...)
}

// with man-type
if( is( x, "!object") ) {
    throw new Error(...)
}

You can use it instead of using OR operator

if( typeof x !== "object" || typeof x !== "number" ) {
    //...
}

// with man-type
if( is(x, ["!object", "!number"]) ) {
    //.....
}

Check elements of array with passing true

const numArr = [z, y, x, ....];

numArr.every(el => typeof el === "number")

// with man-type
if( is(numArr, "number", true) ) {

}
  • Note: If you do not pass true as deep parameter this will returns false because it's an array

You can check multiple things with out AND operator

if( typeof x === "object" && typeof y === "object" ) {
    //....
}

// with man-type
if( is([x, y], "object", true) ) {
    //....
}

And combination of these

if( typeof x !== "object" || typeof x !== "number" && typeof y !== "object" || typeof y !== "number" ) {
    //...
}

// with man-type :)
if( is([x, y], ["!number", "!object"], true) ) {
    //...
}

Wanna use Promise :)

You can use promise for types instead of normal functions

this is helpful when you wanna check a data with server

  • Sorry, Still In Development...

All types

There is some other types that you can use instead of coding more

  • watch out! these are Case Sensitive

string

number // NaN is not acceptable

function

object // Arrays & Null are returns false

bigint

undefined

null

array // Array.isArray (native)

arraylike // check for object that has length property that has equal amount of object elements

plainobject // This type is included from is-plain-object package

nan // check for NaN (native)

infinite // check for not finite

finite // check for finite (native)

empty // check for empty arrays or plainobjects or string

promise

async-function // async function() {}

stream // Stream detection is included from is-stream package

writable-stream

readable-stream

duplex-stream

transform-stream

buffer // (native)

  • These are two kind of types

    custom types

    types: ["string", "boolean", "bigint"]

Types (normal types) and custom Type are in root function


is.types

> ["string", "boolean", "bigint"]

is.customType

> {

    plainobject: function() {...},
    ....

}

Array likes


const arraylikeObj = { length: 1, fn: () => {} }

is(arrayLikeObj, "arraylike")

> true

const notArrayLikeObj = {length: 0, one: "hello", two:"world"} // lenght should be 2

is(notArrayLikeObj, "arraylike")

> false

Important

undefined or null or booleans should not be in quotes


is(undefined, "null")

> false

is(undefined, null)

> true

Arrays are arrays not object (see below):


is([1,2,3,4], "object")

> false

is([1,2,3,4,5], "array")

> true

null is not object (see below):


is(null, "object")

> false

typeof null === "object"

> true

  • if you wanna change this behavior you can use your own:

is.declareType({
"normal-object"(arg) {
return typeof arg === "object"
}
})

Customize it

I think this is the best part of documention


is.declareType(this: is, types: PlainObject<(this: is, arg?: any) => boolean>): is;


is.declareType({
"greater-than-two": function (arg) /\*_ arg is entry _/ {
// this refers to is function
if(this(arg, "number") && arg > 2) {
return true
}
return false;
}
})

is(3, "greater-than-two")

> true

is(2, "!greater-than-two")

> true

You can also declare a direct type (or put functions in array)


// direct type declaration
is(3, function greaterThanTwo(arg) {
if(arg > 2) return true;
return false;
});

> true

Contact Me

with this feature you can write plugins or types that you wanna use in your code

All issues and pull requests are welcome if you wanna contact me please send an email to:

mohammadmahdi1383@outlook.com