npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typa

v0.3.5

Published

Super-simple, zero-dependency JavaScript type checker utility.

Downloads

74

Readme

Typa: Zero-dep JS type checker 🧐

npm npm License: MIT Twitter: @selfagency_llc

The easy-peasy zero-dependency JavaScript type checker that asks, "What typa input is that?" Minified packaged version is 2 entire KB.

Notice: Breaking changes in v0.3.0. New bundler, plus nll is now nil and noru is now nullish.

Install

yarn add typa || npm install typa

Run tests

yarn test

Basic Usage

import is from 'typa' || const is = require('typa')

const hello = 'Hello!'
const goodbye = ['Goodbye!', 'Adios!', 'Au revoir!']

if (is.str(hello)) console.log(hello)
  // => 'Hello!'

if (is.str(goodbye)) console.log(hello)
  // => no result

API

  • 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 string or object
  • nil → Null
  • nullish → Null or undefined
  • num → Number
  • obj → Object
  • prom → Promise
  • regex → Regular expression
  • str → String
  • sym → Symbol
  • undef → Undefined

Typa Method

Ternary operator that checks if the supplied value matches the specified type, then returns the first callback function or value if true or the second callback function or value if false.

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

const isStr = () => console.log('I am a string')
const aintStr = () => console.log('I am not a string')

is.typa('str', 'Am I a string?', isStr, aintStr)
// => 'I am a string'

is.typa('str', ['Am', 'I', 'a', 'string', '?'], isStr, aintStr)
// => 'I am not a string'

What Method

Returns a string or an array of strings matching the type of the supplied value.

.what($value)

is.what('This is a string')
// => 'string'

is.what(['This', 'is', 'an', 'array'])
// => ['array', 'object']

Individual Type Methods

.arr($value) — Array

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

.bad($value) — Null, undefined, empty, or an error

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

.bool($value) — Boolean

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

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

.date($value) — Date

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

.empty($value) — Empty string, array, or object

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

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

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

.err($value) — Error

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

.fn($value) — Function

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

.int($value) — Integer

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

.json($value, $type ['str'|'obj']) — JSON string (default) or object

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

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

.nil($value) — Null

const isNil = is.nil(null)
// => true

.nullish($value) — Null or Undefined

let isNullish = is.nullish(null)
// => true

isNullish = is.nullish(undefined)
// => true

.num($value) — Number

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

.obj($value) — Object

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

.prom($value) — Promise

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($value) — Regular Expression

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

.str($value) — String

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

.sym($value) — Symbol

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

.undef($value) — Undefined

const isUndef = is.undef(undefined)
// => true

Author

👤 Daniel Sieradski [email protected]

Acknowledgements

Most of the checks comprising this library were pilfered from this blog post by Webbjocke.

Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!