Type checking library for JavaScript with a 'sweeter' syntax.
t('foo').isString // => true
There are a hundred other type checking libraries out there. But Typy is built with three core behavioral aspects.
- No surprises. Typy will never throw, no matter what the input is.
- Object check will only look for { } rather than JavaScript's native behavior of considering everything as objects such as arrays, functions, null, etc.
- 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
$ npm install --save typy
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'
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: {}
}
};
// 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
// Examples for nested object schema check
import String from 'typy';
const batmanObject = {
name: 'Batman',
data: {
gender: 'Male',
power: 'Genius, Rich'
}
};
const superHeroSchema = {
name: String,
data: {
gender: String,
power: String
}
};
t(batmanObject, superHeroSchema).isValid // => true
- t(input, optionalObjectPath)
- isDefined
- isUndefined
- isNull
- isNullOrUndefined
- isBoolean
- isTrue
- isFalse
- isTruthy
- isFalsy
- isObject
- isEmptyObject
- isString
- isEmptyString
- isNumber
- isArray
- isEmptyArray
- isFunction
- isValid
- safeObject
- safeString
- safeNumber
- safeBoolean
- safeFunction
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')
// To perform a nested object schema check
// You have to pass the schema object.
import Number from 'typy';
const amazingSchema = {
name: Number,
data: [
{
kills: Number,
build: [
{
weight: Number
}
]
}
]
};
t(obj, amazingSchema)
Returns true if the input is defined.
const obj = {
goodKey: 'hello'
}
t(obj.goodKey).isDefined // => true
t(obj.badKey).isDefined // => false
Returns true if the input is undefined.
const obj = {
goodKey: 'hello'
}
t(obj.goodKey).isUndefined // => false
t(obj.badKey).isUndefined // => true
Returns true if the input is null.
const obj = {
foo: null
}
t(obj.foo).isNull // => true
Returns true if the input is null or undefined.
const obj = {
foo: null
}
t(obj.foo).isNullOrUndefined // => true
t(obj.bar).isNullOrUndefined // => true
Returns true if the input is either true
or false
.
t(true).isBoolean // => true
t(false).isBoolean // => true
Returns true if the input is Boolean true
.
t(true).isTrue // => true
t(false).isTrue // => false
Returns true if the input is Boolean false
.
t(true).isFalse // => false
t(false).isFalse // => true
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
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
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.
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
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
Returns true if the input is an empty string.
t('').isEmptyString // => true
t('typy is so great').isEmptyString // => false
Returns true if the input is a number.
t(22).isNumber // => true
t('i am a string').isNumber // => false
t({}).isNumber // => false
Returns true if the input is an array.
t([]).isArray // => true
t([1, 2, 'typy']).isArray // => true
t({}).isArray // => false
Returns true if the input is an empty array.
t([]).isEmptyArray // => true
t([1, 2, 'typy']).isEmptyArray // => false
Returns true if the input is a function.
const func = () => {};
t(func).isFunction // => true
t({}).isFunction // => false
Returns true if the object and schema provided match.
const { String, Number } = require('../lib');
const batmanObject = {
name: 'Batman',
data: {
gender: 'Male',
age: 30,
power: 'Genius, Rich'
}
};
const superHeroSchema = {
name: String,
data: {
gender: String,
age: Number,
power: String
}
};
const weirdSuperheroSchema = {
name: Number,
data: [
{
kills: Number,
build: [
{
species: String,
weight: Number
}
]
}
]
};
t(batmanObject, superHeroSchema).isValid // => true
t(batmanObject, weirdSuperheroSchema).isValid // => false
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(anotherDeepObj, 'nestedArray[1].superNestedKey.superGoodKey').safeObject; // => 'typy is great :)'
Returns the string value if the input type is string or will return an empty string ''
.
const str = t('typy is safe').safeString; // => 'typy is safe'
const str = t(null).safeString; // => ''
const str = t(undefined).safeString; // => ''
const str = t(22).safeString; // => ''
Returns the number if the input type is Number or will return 0
.
const num = t(22).safeNumber; // => 22
const num = t('22').safeNumber; // => 0
const num = t(undefined).safeNumber; // => 0
const num = t(null).safeNumber; // => 0
Returns the boolean if the input type is Boolean or will return false
.
const bool = t(true).safeBoolean; // => true
const bool = t(false).safeBoolean; // => false
const bool = t('22').safeBoolean; // => false
const bool = t(undefined).safeBoolean; // => false
const bool = t(22).safeBoolean; // => false
Returns the function if the input type is function or will return an empty function () => {}
.
const helloFunc = () => { return 'Hello World!' }
const func = t(helloFunc).safeFunction; // => helloFunc reference
const func = t('I am a string').safeFunction; // => empty function () => {}
const func = t(undefined).safeFunction; // => empty function () => {}
const func = t(null).safeFunction; // => empty function () => {}
MIT © Dinesh Pandiyan