Giter Site home page Giter Site logo

is's Introduction

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

is's People

Contributors

fdaciuk avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.