@fdaciuk/is

Typechecker in Vanilla JS

Stats

stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
270Dec 18, 2016Dec 16, 2016Minified + gzip package size for @fdaciuk/is in KB

Readme

is

Typechecker in Vanilla JS

Build Status Coveralls Coverage Status Code Climate Coverage Code Climate License CONTRIBUTING

Why?

Because type checking in JS is hard. See some examples:

console.log(typeof 1) // 'number'
console.log(typeof 'javascript') // 'string'
console.log(typeof {}) // 'object'
console.log(typeof []) // 'object' wat?
console.log(typeof null) // 'object' wat? wat?
console.log(typeof new String('js')) // 'object' :|

That's why @fdaciuk/is is useful. See some examples using this library in ES2015:

import is from '@fdaciuk/is'

console.log(is('number', 10)) // true
console.log(is('object', {})) // true
console.log(is('array', [])) // true
console.log(is('object', [])) // false
console.log(is('string', new String('js'))) // true (yes, 'string', not 'object')

Installation

Yarn

yarn add @fdaciuk/is

NPM

npm i --save @fdaciuk/is

Usage

AMD

define(['is'], function (is) {
  console.log(is('arguments', arguments)) // true
})

CommonJS

var is = require('@fdaciuk/is').default
console.log(is('array', [])) // true

ES6 / ES2015 Module

import is from '@fdaciuk/is'

const isString = is('string')
console.log(isString('daciuk')) // true

Method of window object

console.log(window.is('number', 10)) // true

Signature

is(typeToTest, value)
// or
is(typeToTest)(value)

Documentation

is

A function that checks if a value is of a type:

import is from '@fdaciuk/is'
// or using commonjs:
// var is = require('@fdaciuk/is').default

const value = []

if (is('array', value)) {
  // do something
}

typeOf

A function that returns a real type of a value:

import { typeOf } from '@fdaciuk/is'
// or using commonjs:
// var typeOf = require('@fdaciuk/is').typeOf

console.log(typeOf([])) // 'array'
console.log(typeOf(123)) // 'number'
console.log(typeOf(new String('hey'))) // 'string', not 'object'

Contributing

Check CONTRIBUTING.md

License

MIT © Fernando Daciuk

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.