typechecker ‐ Validate Javascript Types - cobocombo/Scriptit-Core GitHub Wiki

Module that will help validate all JavaScript types and any other custom types during development. Built in types:

  • string
  • number
  • boolean
  • object
  • array
  • function
  • any

Methods

  • check ({ type, value }): Method to check if a value is a specific type. This could be a base type or a previously registered type.
  • checkMultiple ({ types, value }): Method to check if a value is one of multiple types. This could be a base type or a previously registered type.
  • register ({ name, constructor }): Public method to register a new custom type.

Usage

Checking basic JavaScript types:

let str = 'Hello World';
let stringCheck = typechecker.check({ type: 'string', value: str }); // Returns true
let numberCheck = typechecker.check({ type: 'number', value: str }); // Returns false

Checking multiple JavaScript types:

let num = 4;
let check1 = typechecker.checkMultiple({ types: ['number', 'string'], value: num }); // Returns true
let check2 = typechecker.checkMultiple({ types: ['array', 'function'], value: num }); // Returns false

Registering a custom JavaScript type:

class CustomType {};
typechecker.register({ name: 'custom', constructor: CustomType });