Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
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.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
51
increased by5000%
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

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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc