🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

typa

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typa

JavaScript type checker utility

0.1.4
Source
npm
Version published
Weekly downloads
2
-95%
Maintainers
1
Weekly downloads
 
Created
Source

Typa

A super-simple JavaScript type checker. Mainly pilfered from this blog post by Webbjocke. No dependencies. 3.6K GCC compiled, 2.0k uncompiled.

Codacy Badge

Install

  npm install typa

Import

  import {is} from 'typa'

  // or

  const is = require('typa')

Typa Function

Checks if value matches the specified type, then returns the first function or value if true or the second function or value if false.

Available Types

  • arr: Array
  • bad: Null, undefined, empty, or an error
  • bool: Boolean
  • date: Date
  • empty: Empty string, array, or object
  • err: Error
  • fn: Function
  • int: Integer
  • json: JSON object
  • nll: Null
  • noru: Null or undefined
  • num: Number
  • obj: Object
  • regex: Regular expression
  • str: String
  • sym: Symbol
  • undef: Undefined
  $str = 'hello'
  $arr = 'hello'

  $fn1 = (() => console.log('hello'))
  $fn2 = (() => console.log('goodbye'))

  is.typa('str', $str, $fn1, $fn2)
    // => 'hello'

  is.typa('arr', $arr, $fn1, $fn2)
    // => 'goodbye'

Individual Checks

Array

  is.arr(['text', 12])
    // => true

Boolean

  is.bool(true)
    // => true

  is.bool(false)
    // => true

Date

  is.date(new Date())
    // => true

Empty

  is.empty('')
    // => true

  is.empty([])
    // => true

  is.empty({})
    // => true

Error

  is.err(new Error('This is an error.'))
    // => true

Function

  is.fn(() => { console.log('Hi!') })
    // => true

Integer

  is.int(12)
    // => true

JSON

  is.json('{"key": "value"}')
    // => true

Null

  is.nll(null)
    // => true

Null or Undefined

  is.noru(null)
    // => true

  is.noru(undefined)
    // => true

Number

  is.num(28.2)
    // => true

Object

  is.obj({ key: 'value' })
    // => true

Regex

  is.regex(new Regex(/\W/))
    // => true

String

  is.str('text')
    // => true

Symbol

  is.sym(Symbol(42))
    // => true

Undefined

  is.undef(undefined)
    // => true

Keywords

javascript

FAQs

Package last updated on 08 Mar 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts