Typy
Type checking library for JavaScript with a 'sweeter' syntax.
t('foo').isString // => true
New in version 3 🔥
Version 3.0.0 introduces BREAKING changes (for node.js CommonJS style imports only).
// Before v3.0.0, `t` function was imported asconst t = ; //From v3.0.0, `t` function should be imported asconst t = ; // Note: This version does not affect previous ES6 style imports._; // this will still work; // this will also workWhy? 
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 - Custom type validation and schema validation.
Install
$ npm install --save typy
Usage
; // ES6 style import// var t = require('typy'); // CommonJS style import (version < 3)// var t = require('typy').default; // CommonJS style import (version >= 3) if isString // => true console else console // More examplesisNumber // => falseisString // => trueisObject // => trueisArray // => trueisObject // => false const sym = Symbol'typyIsAwesome';isSymbol // => true // obj.goodKey.nestedKey = 'helloworld'isDefined // => trueisDefined // => false// Typy won't throw undefined error for badKey.nestedKey // to check if obj.goodKey.nestedKey is a stringisString // => trueisString // => false const deepObj = nestedKey: goodKey: 'hello' superNestedKey: {} ;// safely return the value from a nested key in an objectconst myObj = safeObject; // => 'hello'// Typy won't throw undefined error for badKey.goodKey// instead the return value will be undefinedconst myObj = safeObject; // => undefinedAPI
- t(input, optionalObjectPath)
- isDefined
- isUndefined
- isNull
- isNullOrUndefined
- isBoolean
- isTrue
- isFalse
- isTruthy
- isFalsy
- isObject
- isEmptyObject
- isString
- isEmptyString
- isNumber
- isArray
- isEmptyArray
- isFunction
- isDate
- isSymbol
- safeObject
- safeObjectOrEmpty
- safeString
- safeNumber
- safeBoolean
- safeFunction
- safeArray
- isValid (Schema Validation)
- addCustomTypes (Custom Types)
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 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// 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 TypyisDefined
Returns true if the input is defined.
const obj = goodKey: 'hello' isDefined // => trueisDefined // => falseisUndefined
Returns true if the input is undefined.
const obj = goodKey: 'hello' isUndefined // => falseisUndefined // => trueisNull
Returns true if the input is null.
const obj = foo: null isNull // => trueisNullOrUndefined
Returns true if the input is null or undefined.
const obj = foo: null isNullOrUndefined // => trueisNullOrUndefined // => trueisBoolean
Returns true if the input is either true or false.
isBoolean // => trueisBoolean // => trueisTrue
Returns true if the input is Boolean true.
isTrue // => trueisTrue // => falseisFalse
Returns true if the input is Boolean false.
isFalse // => falseisFalse // => trueisTruthy
Returns true if the input is considered truthy.
In JavaScript anything other than false, 0, '', "", null, undefined and NaN is considered truthy.
isTruthy // => trueisTruthy // => trueisTruthy // => trueisTruthy // => trueisFalsy
Returns true if the input is considered falsy.
In JavaScript any of these values false, 0, '', "", null, undefined and NaN are considered falsy.
isFalsy // => trueisFalsy // => trueisFalsy // => trueisFalsy // => trueisObject
Returns true if the input is an object.
const obj = foo: null isObject // => trueisObject // => trueNote: 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: {} isEmptyObject // => trueisEmptyObject // => trueisEmptyObject // => falseisString
Returns true if the input is a string.
const obj = foo: 'typy is awesome =)'isString // => trueisString // => trueisString // => falseisString // => falseisEmptyString
Returns true if the input is an empty string.
isEmptyString // => trueisEmptyString // => falseisNumber
Returns true if the input is a number.
isNumber // => trueisNumber // => falseisNumber // => falseisArray
Returns true if the input is an array.
isArray // => trueisArray // => trueisArray // => falseisEmptyArray
Returns true if the input is an empty array.
isEmptyArray // => trueisEmptyArray // => falseisFunction
Returns true if the input is a function.
const func = {};isFunction // => trueisFunction // => falseisDate
Returns true if the input is a javascript's date object.
const date = ;isDate // => trueisDate // => falseisSymbol
Returns true if the input is a javascript's Symbol.
const mySym = Symbol123;const anotherSymbol = Symbol'typyIsAwesome'; isSymbol // => true;isSymbol // => true; isSymbol // => falseisSymbol // => falseisSymbol // => falsesafeObject
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 objectconst myObj = safeObject; // => 'hello'// Typy won't throw if the key at any level is not found// instead will return undefinedconst myObj = 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 arrayconst myObj = safeObject; // => 'typy is great :)'safeObjectOrEmpty
Safely returns the value from a nested object path if the path exists or returns an empty object if the.
const deepObj = nestedKey: goodKey: 'hello' superNestedKey: {} ;// Typy can safely return the value from a nested key in an objectconst myObj = safeObjectOrEmpty; // => 'hello'// Typy won't throw if the key at any level is not found// instead will return an empty objectconst myObj = safeObjectOrEmpty; // => {} 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 arrayconst myObj = safeObjectOrEmpty; // => 'typy is great :)'safeString
Returns the string value if the input type is string or will return an empty string ''.
const str = safeString; // => 'typy is safe'const str = safeString; // => ''const str = safeString; // => ''const str = safeString; // => ''safeNumber
Returns the number if the input type is Number or will return 0.
const num = safeNumber; // => 22const num = safeNumber; // => 0const num = safeNumber; // => 0const num = safeNumber; // => 0safeBoolean
Returns the boolean if the input type is Boolean or will return false.
const bool = safeBoolean; // => trueconst bool = safeBoolean; // => falseconst bool = safeBoolean; // => falseconst bool = safeBoolean; // => falseconst bool = safeBoolean; // => falsesafeFunction
Returns the function if the input type is function or will return an empty function () => {}.
const helloFunc = { return 'Hello World!' }const func = safeFunction; // => helloFunc referenceconst func = safeFunction; // => empty function () => {}const func = safeFunction; // => empty function () => {}const func = safeFunction; // => empty function () => {}safeArray
Safely returns the value from a nested object path or an empty array. If the path specified exists but is not an array, returns an array containing the value of the specified path.
const deepObj = nestedKey: goodKey: 'hello' numberKey: 10 superNestedKey: {} ;// Typy can safely return the value from a nested key in an object or an arrayconst myObj = safeArray; // => [ { goodKey: ['hello'], numberKey: 10, superNestedKey: {} } ]const myObj = safeArray; // => ['hello']// Typy can wrap a value or object inside an arrayconst myObj = safeArray; // => [ 10 ]const myObj = safeArray; // => [ {} ]// Typy won't throw if the key at any level is not found// instead will return an empty arrayconst myObj = safeArray; // => []const myObj = safeArray; // => []isValid (Schema Validation)
isValid is used to check and validate the schema of an object. It returns true if the schema of the object matches the schema passed or false if the schema doesn't match.
; const superheroSchema = name: SchemaString age: SchemaNumber appearances: title: SchemaString alias: SchemaString lastSeen: SchemaDate;const batmanObject = name: 'Batman' age: 45 isAlive: true appearances: title: 'The Dark Knight' alias: 'Bruce' lastSeen: 14894561568;const isSchemaValid = isValid; // true const simpleSchema = name: SchemaString arr: SchemaArray;const obj = name: 'Jack' arr: 1 2 3;const isSchemaValid = isValid; // trueThe following Schema types are available in typy.
- Number
- String
- Array
- Boolean
- Null
- Undefined
- Function
- Date
addCustomTypes (Custom Types)
addCustomTypes is used to pass custom validators to Typy. It can be used to validate any ipnut for custom types, like this t(input).isMyCustomType.
You will have to add custom types only once in the project (preferabby in entry file. ex. index.js)
Entry file (Ex. index.js)
; ; Anywhere in the project
; const isThePhoneNumberValid = isPhone; // => trueconst isThePhoneNumberValid = isPhone; // => false const isTheAddressValid = isAddress; // => trueconst isTheAddressValid = isAddress; // => false Contributors
Thanks goes to these amazing people 🎉
Dinesh Pandiyan |
Ozair Patel |
Aneerudh |
Ruphaa Ganesh |
Quentin Jadeau |
dan |
|---|---|---|---|---|---|
Robert Schadek |
Michael Kirkpatrick |
Ana Liza Pandac |
Abdul Rehman |
License
MIT © Dinesh Pandiyan