typy

05/02/2018



dependencies Status
License: MIT
Gitter

Type checking library for JavaScript with a ‘sweeter’ syntax.

t('foo').isString // => true

Why? start with why

There are a hundred other type checking libraries out there. But Typy is built with three core behavioral aspects.

  1. No surprises. Typy will never throw, no matter what the input is.
  2. Object check will only look for { } rather than JavaScript’s native behavior of considering everything as objects such as arrays, functions, null, etc.
  3. Thought Driven Development. Code should exactly mimic your thoughts on the logic rather than writing extra code just because that’s how JavaScript works.
    t(obj).isDefined // => true

Install

$ npm install --save typy

Usage

import t from 'typy'; 

 

if (t('hello').isString) { 

  console.log('Input is a String!')

} else {

  console.log('Input is not a String!')

}

 

t('22').isNumber 

t('22').isString 

t({}).isObject 

t([]).isArray 

t([]).isObject 

 

t(obj, 'goodKey.nestedKey').isDefined 

t(obj, 'badKey.nestedKey').isDefined 

 

t(obj, 'goodKey.nestedKey').isString 

t(obj, 'badKey.nestedKey').isString 

API

t(input, optionalObjectPath)

Pass in your input to the t() method and Typy will take care of everything

t('str')

t(22)

t({foo: 'fooooo', bar: 'barooo'})

t([2, 'three', 'hey'])

 

const obj = {

  goodKey: {

    nestedKey: 'hello world'

  }

}

t(obj, 'goodKey.nestedKey')

t(obj, 'badKey.nestedKey')

isDefined

Returns true if the input is defined.

const obj = {

  goodKey: 'hello'

}

 

t(obj.goodKey).isDefined 

t(obj.badKey).isDefined 

isUndefined

Returns true if the input is undefined.

const obj = {

  goodKey: 'hello'

}

 

t(obj.goodKey).isUndefined 

t(obj.badKey).isUndefined 

isNull

Returns true if the input is null.

const obj = {

  foo: null

}

 

t(obj.foo).isNull 

isNullOrUndefined

Returns true if the input is null or undefined.

const obj = {

  foo: null

}

 

t(obj.foo).isNullOrUndefined 

t(obj.bar).isNullOrUndefined 

isBoolean

Returns true if the input is either true or false.

t(true).isBoolean 

t(false).isBoolean 

isTrue

Returns true if the input is Boolean true.

t(true).isTrue 

t(false).isTrue 

isFalse

Returns true if the input is Boolean false.

t(true).isFalse 

t(false).isFalse 

isTruthy

Returns true if the input is considered truthy.

In JavaScript anything other than false, 0, '', "", null, undefined and NaN is considered truthy.

t('Typy is amazing =)').isTruthy 

t({}).isTruthy 

t(22).isTruthy 

t([1, 'two']).isTruthy 

isFalsy

Returns true if the input is considered falsy.

In JavaScript any of these values false, 0, '', "", null, undefined and NaN are considered falsy.

t(0).isFalsy 

t(null).isFalsy 

t(undefined).isFalsy 

t(false).isFalsy 

isObject

Returns true if the input is an object.

const obj = {

  foo: null

}

 

t(obj).isObject 

t({}).isObject 

Note: Only { } objects will return this as true as opposed to javascript definition of Object which includes Arrays, Functions, anything and everything related to prototype. This is an intentional behavior as we don’t want arrays to return true for isObject.

isEmptyObject

Returns true if the input is an empty object, aka object without any keys.

const obj = {

  foo: 'hello there',

  bar: {}

}

 

t(obj.bar).isEmptyObject 

t({}).isEmptyObject 

t(obj).isEmptyObject 

isString

Returns true if the input is a string.

const obj = {

  foo: 'typy is awesome =)',

}

t(obj.foo).isString 

t('').isString 

t(22).isString 

t(null).isString 

isEmptyString

Returns true if the input is an empty string.

t('').isEmptyString 

t('typy is so great').isEmptyString 

isNumber

Returns true if the input is a number.

t(22).isNumber 

t('i am a string').isNumber 

t({}).isNumber 

isArray

Returns true if the input is an array.

t([]).isArray 

t([1, 2, 'typy']).isArray 

t({}).isArray 

isEmptyArray

Returns true if the input is an empty array.

t([]).isEmptyArray 

t([1, 2, 'typy']).isEmptyArray 

isFunction

Returns true if the input is a function.

const func = () => {};

t(func).isFunction 

t({}).isFunction 

License

MIT © Dineshkumar Pandiyan

قالب وردپرس