Code Monkey home page Code Monkey logo

check's Introduction

Check.js

Utility for checking if a nested object is valid.

Experimenting with this for now. It is much in its infancy.

While working for a large company with rich data objects I found myself constantly writing humungous if statements to ensure the nested attribute I was accessing was valid. I'm working on this as a shorthand way to grab a nested object or attribute. Inspired by coffeescript's existential operator.

So far the functionality allows for

  • Getting an object if valid or a fallback value if not valid

  • Finding whether or not an object is valid

  • Doing all of this without writing long if statements checking each value along the way

Usage

Given an object...

	var myObj = {
		Building: {
			apartment: {
				rooms: [
					'living',
					'kitchen',
					'bedroom'
				]
			}
		}
	};

Checkjs allows you to safely use this object like this...

	var result = Check.use(myObj, 'Building', 'apartment', 'rooms', '1');
	//result = 'kitchen'

This is equivalent to

	var result;
	if(myObj && myObj.Building && myObj.Building.apartment && myObj.Building.apartment.rooms && myObj.Building.apartment.rooms[1]){
		result = myObj.Building.apartment.rooms[1]
	} else {
		result = false;
	}

Docs

So far there are three main functions: use, check, and useWithFallback

use

Check.use(object, propertiesToUse...)

Given and object and 'n' number of strings representing the properties or index on that object you want to access, this function will return that attribute/value if it exists and doesn't throw an exception. If that attribute or value is undefined or throws and exception ( possibly do to parent property being undefined) then the function will return the default fallback value (false).

###Example:

Let's say we have an object like the one above in the usage section...

var myObj = {
	Building: {
		apartment: {
			rooms: [
				'living',
				'kitchen',
				'bedroom'
			]
		}
	}
};

and we want to safely access the 'bedroom' String (myObj.Building.apartments.rooms[2]). If this string or anypart of this object is at some point undefined or invalide (for example if you have an object coming from a webservice) a defaultFallback value will be returned. You can change check's default fallback value wich defaults to boolean false.

	var result = Check.use(myObj, 'Building', 'apartment', 'rooms', '1');
	//result = 'kitchen'

useWithFallback

Check.useWithFallback(object, propertiesToUse..., fallback)

Eccentially the same as use, but the last value will be used as the fallback.

Given and object and 'n' number of strings representing the properties or index on that object you want to access, and a fallback value. this function will return that attribute/value if it exists and doesn't throw an exception. If that propery or value is undefined then the function will return the fallback value provided.

###Example:

Let's say we have an object like the one above in the usage section...

var myObj = {
	Building: {
		apartment: {
			rooms: [
				'living',
				'kitchen',
				'bedroom'
			]
		}
	}
};

and we want to safely access the 'bedroom' String (myObj.Building.apartments.rooms[2]). If this string or anypart of this object is at some point undefined or invalide (for example if you have an object coming from a webservice) a defaultFallback value will be returned. You can change check's default fallback value wich defaults to boolean false.

	var result = Check.useWithFallback(myObj, 'Building', 'apartment', 'rooms', '1', 'Object Not found');
	//result = 'kitchen'

	// or
	var result = Check.useWithFallback(myObj, 'Building', 'apartment', 'rooms', '8', 'Object Not Found');
	//result = 'Object Not Found'

check

Check.check(object, propertiesToUse...)

Essentially the same as Use but this will return a boolean true or false for whether or not the attribute or value you are trying to access is valid. The fallback value for this will always be false.

###Example:

	var result = Check.check(myObj, 'Building', 'apartment', 'rooms', '2');
	//result = true

setDefualtFallback

Check.setDefaultFallback (object||string||boolean)

set the default fallback value for the Check object

##Example:

	var myObj = {
		Building: {
			apartment: {
				rooms: [
					'living',
					'kitchen',
					'bedroom'
				]
			}
		}
	};
	Check.setDefaultFallback({});
	var result = Check.use(myObj, 'Building', 'address');
	// result = {}


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.