@trz/type
TypeScript icon, indicating that this package has built-in type declarations

0.5.0 • Public • Published

@trz/type

A tool for real type validate.

Install

npm install @trz/type

# Or

yarn add @trz/type

Usage

Import

// ES6
import types, {of, is} from '@trz/type';

// nodejs or broswer
const types  = require('@trz/type');

Interface

function of(source: any): string;

  • Enter an element of any type and return the real type for the element in JavaScript.

  • Example:

    import types from '@trz/type';
    
    types.of(123456789)              //>>> number
    
    types.of('this is a string')     //>>> string
    
    types.of([])                     //>>> array
    
    types.of({})                     //>>> object

function is(source: any, assert: string): boolean

  • Compare whether the source type is the specified type.

  • Example:

    import types from '@trz/type';
    
    types.is([], 'array')              //>>> true
    
    types.is([], 'object')             //>>> false
    
    types.is('123456789', 'string')    //>>> true
    
    types.is(null, 'string')           //>>> false

function some(source: any, asserts: string[]): boolean

  • Verify that the specified element contains the specified type.

  • Example:

    import types from '@trz/type';
    
    types.some([1, 2], ['array', 'object'])      //>>> true
      
    types.some([1, 2], ['object', 'string'])     //>>> false
      
    types.some('1239', ['string'])               //>>> true
      
    types.some(null, ['string'])                 //>>> false

function isInteger(source: any): boolean

  • Verify that the source is an integer type

  • Example:

    import { isInteger } from '@trz/type';
    
    isInteger(12345678)          //>>> true
    
    isInteger(12345.00)          //>>> true
    
    isInteger(0.123456)          //>>> false
    
    isInteger(1.234567)          //>>> false
    
    isInteger('12345678')        //>>> true
    
    isInteger('12345.00')        //>>> true
    
    isInteger('0.123456')        //>>> false
    
    isInteger('1.234567')        //>>> false
    
    isInteger('string')          //>>> false
    
    isInteger([])                //>>> false
    
    (...)

Package Sidebar

Install

npm i @trz/type

Weekly Downloads

1

Version

0.5.0

License

MIT

Unpacked Size

14.5 kB

Total Files

8

Last publish

Collaborators

  • chenzhenyuan