Git Product home page Git Product logo

is's Introduction

Is

A better way to write JavaScript conditional statements and still have pretty code.

This library was inspired by the Maybe Monad pattern and libraries like Jasmine.js and JQuery. It allows you to to write conditional statements without nested if statements or null checking.

###Installation

Just include the minified script in your project. It's really tiny (1kb minified and zipped)

<script src="is.min.js"></script>

Or, if you're using TypeScript (and why aren't you using TyepScript?), include the definition file in your main class.

/// <reference path="is.d.ts" />

###How Does It Work? Typically, a lot of checks go into working with variables, especially when the values are returned by ajax calls or user input. This results in a lot of nested if statements that check for values to be defined, but also to pass various validation checks. For instance, see this code:

var foo="bar";
if(foo){
  if(foo.length>0){
    if(foo == "bar2"){
       throw new Error("bad foo");
    }else if(foo!="bar"){
       throw new Error("bad foo"); 
    }
  }else{
    throw new Error("bad foo");
  }
}else{
    throw new Error("bad foo");
}

A better way to handle this is to write the code in such a way that when any single step fails, the chain is broken and none of the other steps are executed. A single catch function can handle the failure. So using Is, the preceding code can be rewritten as:


var foo="bar";
new Is(foo)
  .isLongerThan(0).
  .equals("bar")
  .not().equals("bar2")
  .then(()=>{
    console.log("success")
  })
  .catch(()=>{
    throw new Error("bad foo");
  });

This allows the code to read almost like spoken words, which hopefully leads to more maintainable code.

There are several methods available for checking the input value. If a validation method is successful, Is returns an instance of Is with the value, otherwise it returns one with nothing in it, which stops the chain.

  var foo="bar";
  new Is(foo)
  .isLongerThan(number) // checks the length of a string for minimum length 
  .isShorterThan(number) // checks the length of a string for maximum length
  .equals("bar") // checks for exact value
  .not() // inverts the next validation
  .equals("baz")
  
  new Is(1000)
  .isNumber() // returns true if the value is a number
  .isLessThan(10000) // checks for maximum value
  .isGreaterThan(0) // checks for minimum value
  
  new Is(100)
    .any("<101",">1000") // you can use a shorthand and check for any of these things to be true
    .is("<101", ">99") // or check that all validations are true
    
  var validEmail = function (val:string) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(val);
  };

  new Is("[email protected]")
    .is(validEmail) //you can use a function as an argument which returns true of false
  
  new Is("[email protected]")
    .is(validEmail)
    .then(()=>{
      consolde.log("valid email") //if the chain succeeds, you can run a callback using "then"
    });
    
  new Is("john.doefakeemail.com")
    .is(validEmail)
    .catch(()=>{
      consolde.log("bad email") //if the chain fails, you can run a callback using "catch"
    });
  
  new Is("john.doefakeemail.com")
    .is(validEmail)
    .catch(()=>{
      //bad email
    }).finally(()=>{
      //using this method, you can run a callback using "finally" no matter what happens
    });
  

The best part of all this is that you no longer need to worry about undefined variable values. For instance, if you have a value like this:

var foo={bar:undefined};

new Is(foo.bar)
  .isLongerThan(3)
  .catch(()=>{
    console.log("no value") // this gets called no matter what the value of foo.bar is
  })

is's People

Contributors

abogartz avatar

Watchers

James Cloos avatar Inno 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.