@fdaciuk/is

Typechecker in Vanilla JS

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@fdaciuk/is
2701.1.06 years ago6 years agoMinified + gzip package size for @fdaciuk/is in KB

Readme

is
Typechecker in Vanilla JS

!Build Statustravis-imagetravis-url !Coveralls Coverage Statuscoverage-imagecoverage-url !Code Climate Coveragecodeclimate-coverage-imagecodeclimate-coverage-url !Code Climatecodeclimate-imagecodeclimate-url !Licenselicense-imagelicense-url !CONTRIBUTINGcontributing-imagecontributing-url

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