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

typecase

v1.1.3

Published

A dynamic type checker for JavaScript

Downloads

12

Readme

Typecase

Typecase aims to provide a useful type of a given value. It is intended for standard dynamically typed JavaScript.

Types seem to be a commonly misunderstood subject in the world of JavaScript and can sometimes be difficult to manage even if you do understand it well. Typecase differentiates between different types from the perspective of the JavaScript language and common usage rather than other static typed languages.

.true

// Returns a Boolean of the expected value match.
type(value).true // true | false

.false

// Returns a Boolean of the expected value match.
type(value).false // true | false

.exist

// Value is neither null nor undefined.
type(value).exist // OR !type(value).exist

.empty

// Value is null, undefined or an emppty string ''.
type(value)empty // OR !type(value).empty

.zero

// Value is null, undefined or equal to 0.
type(value).zero // OR !type(value).zero

Types

typecase allows you to see types for what they are. When applicable, the object wrapper is shown by the prefix 'object'. Non standard types (such as elements) will return undefined if the value is falsy. All other types return boolean values.

type (<value>).<type> // true | false
// Common types

// String
type('Hello World!').string
type(String()).string
type(new String()).objectString

// Number
type(1000).number
type(Number(1000)).number
type(new Number()).objectNumber

// Boolean
type(true).boolean
type(Boolean(true)).boolean
type(new Boolean()).objectBoolean

// Array
type([]).array
type(Array()).array
type(new Array()).array

// Function
type(()=>{}).function
type(function(){}).function
type(Function()).function
type(new Function()).function

// Date, Map, WeakMap, Symbol
type(new Date()).objectDate
type(Symbol('foo')).symbol
type(new Map()).objectMap
type(new WeakMap()).objectWeakMap

// RegExp
type(/Hello/).regExp
type(RegExp()).regExp
type(new RegExp()).regExp

// Null, undefined and NaN
type(undefined).undefined
type(null).null
type(NaN).NaN

// Objects 
type({}).object
type(<other objectObjects>).object

// Non-standard type examples
type(document.createElement('div')).objectHTMLDivElement    // true | undefined
type(document.body).objectHTMLBodyElement                   // true | undefined
...And so on.

.some()

Returns true if at least one of the types validate as expected. If not returns false. If the expected types are of an equivalent amount to the values supplied, each type and value will be compared in order. If not they will be compared sporadically.

type(value, value, value, value).some('string','objectDate', 'number', 'array')

type(value, value, value, value, value, value).some('string','objectDate')

.every()

Returns true if all types validate as expected. If not returns false. Values and expectedTypes must be of equal length.

type(value, value, value, value).every('string','objectDate','true','false')

.is

Reveals the type as a string.

type(10000).is // "number"

.raw (TBA)

Reveals the raw object-type without sugar coating as a string.

type(null).raw // "[object Null]"
type(NaN).raw // "[object Number]"

What this library is not

This library does not check if a value is an instance of another, use the instanceof operator:

someDOMElement instanceof Element // true

Alternatively (type check only)

 type(someDOMElement).is.includes('Element') // true

Typecase does not aim to tell you the specific "type" of sub-object it may contain, this is usually not important since you would likely need to check for existing properties, and if not then native properties will be sufficient.

For everything else kind-of type related, the native language should be more than sufficient.

Explanations.

  • Although Array is an object 'array' is returned rather than 'objectArray' because it is not an intended object for Object use unlike i.e. new Boolean(). The same applies to RegExp.

  • Although NaN is an invalid type-of 'number', because this is commonly problematic 'NaN' is returned instead.

  • 'objectObject' is always returned as 'object' for simplicity.

  • null is returned as "null" since typeof null // object is a mistake.

  • 'empty' and 'zero' also check for non-existence.

MIT (c) 2017 Julien Etienne.