Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/phawk/js-typeof

A better typeOf method for JavaScript, where null !== "object"
https://github.com/phawk/js-typeof

Last synced: 14 days ago
JSON representation

A better typeOf method for JavaScript, where null !== "object"

Awesome Lists containing this project

README

        

# js typeOf

## typeOf(Object)

JavaScripts typeof operator sucks! This aims to provide an extremely simple method to call to check typeof and should be a little more epectable, and reduce boilerplate on your input checking.

```js
// Rudementary example
if (objectToTest && typeof objectToTest === "object" && objectToTest instanceof Array)

// Easier
if (typeOf(objectToTest) === "array")
```

## is(Object, "Object")

Used for shorthand comparisons, returns a boolean.

```js
var obj = {};

is(obj, "object"); // true
is(obj, "string"); // false
```

## Changelog

### v0.3.0

* Switched is("number", 5) to is(5, "number") for better readability.
* Added more resiliant check for null and undefined for odd environments, like phantomJS.

### v0.2.0

* Simpler and more accurate calculation using `Object.toString.
* Now with `is()` method for comparisons
* typeOf(new Date()) now equals "date"
* typeOf(new Error()) now equals "error"

### v0.1.2

* Massively simplify and optimise

### v0.1.1

* Custom constructors return typeof as `object`
* The only two objects that do return a different typeof are `Array` and `RegExp`

### v0.1.0

* Added support for `null`
* Add extra tests