
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
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.
// Returns a Boolean of the expected value match.
type(value).true // true | false
// Returns a Boolean of the expected value match.
type(value).false // true | false
// Value is neither null nor undefined.
type(value).exist // OR !type(value).exist
// Value is null, undefined or an emppty string ''.
type(value)empty // OR !type(value).empty
// Value is null, undefined or equal to 0.
type(value).zero // OR !type(value).zero
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.
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')
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')
Reveals the type as a string.
type(10000).is // "number"
Reveals the raw object-type without sugar coating as a string.
type(null).raw // "[object Null]"
type(NaN).raw // "[object Number]"
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.
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.
FAQs
A dynamic type checker for JavaScript
We found that typecase demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.