New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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.10
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
16
decreased by-70.37%
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 CircleCI

Install

  npm install typa

Import

  import {is} from 'typa'

  // or

  const is = require('typa')

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: Serialized JSON object
  • nll: Null
  • noru: Null or undefined
  • num: Number
  • obj: Object
  • prom: Promise
  • regex: Regular expression
  • str: String
  • sym: Symbol
  • undef: Undefined

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.

Method:

  is.typa($type, $value, $fn1, $fn2)

Example:

  const myString = 'this is a string'
  const myArray = 'this is also a string, not an array'

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

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

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

Individual Checks

Array Method:

  is.arr($value)

Example:

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

Bad Method:

  is.bad($value)

Example:

  let isBad = is.bad(null)
    // => true

  isBad = is.bad(undefined)
    // => true

  isBad = is.bad({})
    // => true

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

Boolean Method:

  is.bool($value)

Example:

  let isBool = is.bool(true)
    // => true

  isBool = is.bool(false)
    // => true

Date Method:

  is.date($value)

Example:

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

Empty Method:

  is.empty($value)

Example:

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

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

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

Error Method:

  is.err($value)

Example:

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

Function Method:

  is.fn($value)

Example:

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

Integer Method:

  is.int($value)

Example:

  const isInt = is.int(12)
    // => true

JSON Method:

  is.json($value)

Example:

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

Null Method:

  is.null($value)

Example:

  const isNll = is.nll(null)
    // => true

Null or Undefined Method:

  is.noru($value)

Example:

  let isNoru = is.noru(null)
    // => true

  isNoru = is.noru(undefined)
    // => true

Number Method:

  is.num($value)

Example:

  const isNum = is.num(28.2)
    // => true

Object Method:

  is.obj($value)

Example:

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

Promise Method:

  is.prom($value)

Example:

  const myPromise = new Promise((resolve, reject) => {
    try {
      console.log('I make a promise to you')
      resolve()
    } catch(err) {
      reject(err)
    }
  })

  const isProm = is.prom(myPromise)
    // => true

Regex Method:

  is.regex($value)

Example:

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

String Method:

  is.str($value)

Example:

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

Symbol Method:

  is.sym($value)

Example:

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

Undefined Method:

  is.undef($value)

Example:

  const isUndef = 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