Skip to content

misakamayako/runtime-type-checker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Runtime Type Checker

a runtime type checker of any object for javascript.

NPM npm

Installation

$ npm i runtime-type-checker

import

import TypeChecker from "runtime-type-checker";//es6
const TypeChecker = require("runtime-type-checker");//
  • unpkg
<!-- latest version -->
<script src="https://unpkg.com/runtime-type-checker/lib/bundle.js"></script>

Usage

// init a checker
const allTypeChecker = {
    //[any key]: TypeChecker[type you want][add isRequire if it Required]
    array: TypeChecker.array.isRequired,
    bool: TypeChecker.bool,
    func: TypeChecker.func,
    number: TypeChecker.number,
    object: TypeChecker.object,
    string: TypeChecker.string,
    symbol: TypeChecker.symbol,
    any: TypeChecker.any,
    arrayOf: TypeChecker.arrayOf(TypeChecker.string),
    instanceOf: TypeChecker.instanceOf(Date),
    objectOf: TypeChecker.objectOf(TypeChecker.string),
    oneOf: TypeChecker.oneOf(['a', 'b']),
    oneOfType: TypeChecker.oneOfType([TypeChecker.string, TypeChecker.number]),
    shape: TypeChecker.shape({key: TypeChecker.string, value: TypeChecker.number}),
    exact: TypeChecker.exact({key: TypeChecker.string, value: TypeChecker.number}),
}
// your data
const exampleData = {
    array: [],
    bool: false,
    func: () => void 0,
    number: 123,
    object: {},
    string: "TypeChecker('string')",
    symbol: Symbol("lucky"),
    any: 123n,
    arrayOf: ["string"],
    instanceOf: new Date(),
    objectOf: {"213": "DD"},
    oneOf: "a",
    oneOfType: "string",
    shape: {key: "123"},
    exact: {key: "123", value: 123},
}

// final, call check fun
//TypeChecker.checkPropTypes(Checker, value, [displayName for usage place], [objctName]);
const result = TypeChecker.checkPropTypes(allTypeChecker, value, 'test', 'test2');
// and result is null because of all prototype has passed the checker
// if some prototype don't, the result will be an array of error message

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published