JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 21377
  • Score
    100M100P100Q136366F
  • License MIT

Minimal JavaScript type checking library

Package Exports

  • typy

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (typy) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Typy

Build Status dependencies Status npm version 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'; // ES6 style import
// var t = require('typy'); // ES5 style import

if (t('hello').isString) { // => true
  console.log('Input is a String!')
} else {
  console.log('Input is not a String!')
}

// More examples
t('22').isNumber // => false
t('22').isString // => true
t({}).isObject // => true
t([]).isArray // => true
t([]).isObject // => false

// obj.goodKey.nestedKey = 'helloworld'
// to check if obj.goodKey.nestedKey is defined
// but you don't know if obj.goodKey exists
t(obj, 'goodKey.nestedKey').isDefined // => true
t(obj, 'badKey.nestedKey').isDefined // => false
// Typy won't throw undefined error for badKey.nestedKey

// to check if obj.goodKey.nestedKey is a string
t(obj, 'goodKey.nestedKey').isString // => true
t(obj, 'badKey.nestedKey').isString // => false

const deepObj = {
  nestedKey: {
    goodKey: 'hello',
    superNestedKey: {}
  }
};
// Typy can safely return the value from a nested key in an object
const myObj = t(deepObj, 'nestedKey.goodKey').safeObject; // => 'hello'
// Typy won't throw undefined error for badKey.goodKey
// instead the return value will be undefined
const myObj = t(deepObj, 'badKey.goodKey').safeObject; // => undefined

API

t(input, optionalObjectPath)

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

// you can pass any type of input
// Number, String, Object, null, undefined, Array, anything
t('str')
t(22)
t({foo: 'fooooo', bar: 'barooo'})
t([2, 'three', 'hey'])

const obj = {
  goodKey: {
    nestedKey: 'hello world'
  }
}
// To pass nested path of an object
// Ex. obj.goodKey.nestedKey
// You have to pass the path as string in the second param
t(obj, 'goodKey.nestedKey')
t(obj, 'badKey.nestedKey')
// this is because if you pass t(obj.badKey.nestedKey),
// you will get undefined exception
// because that is how javascript is designed
// to overcome that we need to pass the sub key as a string to Typy

isDefined

Returns true if the input is defined.

const obj = {
  goodKey: 'hello'
}

t(obj.goodKey).isDefined // => true
t(obj.badKey).isDefined // => false

isUndefined

Returns true if the input is undefined.

const obj = {
  goodKey: 'hello'
}

t(obj.goodKey).isUndefined // => false
t(obj.badKey).isUndefined // => true

isNull

Returns true if the input is null.

const obj = {
  foo: null
}

t(obj.foo).isNull // => true

isNullOrUndefined

Returns true if the input is null or undefined.

const obj = {
  foo: null
}

t(obj.foo).isNullOrUndefined // => true
t(obj.bar).isNullOrUndefined // => true

isBoolean

Returns true if the input is either true or false.

t(true).isBoolean // => true
t(false).isBoolean // => true

isTrue

Returns true if the input is Boolean true.

t(true).isTrue // => true
t(false).isTrue // => false

isFalse

Returns true if the input is Boolean false.

t(true).isFalse // => false
t(false).isFalse // => true

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 // => true
t({}).isTruthy // => true
t(22).isTruthy // => true
t([1, 'two']).isTruthy // => true

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 // => true
t(null).isFalsy // => true
t(undefined).isFalsy // => true
t(false).isFalsy // => true

isObject

Returns true if the input is an object.

const obj = {
  foo: null
}

t(obj).isObject // => true
t({}).isObject // => true

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 // => true
t({}).isEmptyObject // => true
t(obj).isEmptyObject // => false

isString

Returns true if the input is a string.

const obj = {
  foo: 'typy is awesome =)',
}
t(obj.foo).isString // => true
t('').isString // => true
t(22).isString // => false
t(null).isString // => false

isEmptyString

Returns true if the input is an empty string.

t('').isEmptyString // => true
t('typy is so great').isEmptyString // => false

isNumber

Returns true if the input is a number.

t(22).isNumber // => true
t('i am a string').isNumber // => false
t({}).isNumber // => false

isArray

Returns true if the input is an array.

t([]).isArray // => true
t([1, 2, 'typy']).isArray // => true
t({}).isArray // => false

isEmptyArray

Returns true if the input is an empty array.

t([]).isEmptyArray // => true
t([1, 2, 'typy']).isEmptyArray // => false

isFunction

Returns true if the input is a function.

const func = () => {};
t(func).isFunction // => true
t({}).isFunction // => false

safeObject

Safely returns the value from a nested object path without throwing any error.

const deepObj = {
  nestedKey: {
    goodKey: 'hello',
    superNestedKey: {}
  }
};
// Typy can safely return the value from a nested key in an object
const myObj = t(deepObj, 'nestedKey.goodKey').safeObject; // => 'hello'
// Typy won't throw if the key at any level is not found
// instead will return undefined
const myObj = t(deepObj, 'badKey.goodKey').safeObject; // => undefined

const anotherDeepObj = {
  nestedArray: [{
    goodKey: 'hello one',
    superNestedKey: {}
  }, {
    goodKey: 'hello two',
    superNestedKey: {
      superGoodKey: 'typy is great :)'
    }
  }]
};
// Typy can safely return the value even from a nested key in a nested array
const myObj = t(deepObj, 'nestedArray[1].superNestedKey.superGoodKey').safeObject; // => 'typy is great :)'

safeString

Returns the string value if the input type is string or will return an empty string ''.

const myObj = t('typy is safe').safeString; // => 'typy is safe'
const myObj = t(null).safeString; // => ''
const myObj = t(undefined).safeString; // => ''
const myObj = t(22).safeString; // => ''

License

MIT © Dineshkumar Pandiyan