Skip to content

uop is a tiny package that helps strict undefined checking on objects with nested properties

License

Notifications You must be signed in to change notification settings

jorchg/uop

Repository files navigation

UOP

Build StatusCoverage Status

UOP stands for Undefined Object Properties and is a tiny library which works on Node and browser which simply checks for undefined properties or nested properties on objects. We all have seen even in highly-skilled developers groups checking for undefined properties like:

if(obj&&obj.nested&&obj.nested.datetime&&obj.nested.datetime.time&&obj.nested.datetime.time.hours&&obj.nested.datetime.time.hours.stringFormat)returntrue;

This is awful and we all should avoid it. It is frequently common also the other way, not checking all of the nested properties on an object which produce TypeError and crash your application. With UOP we can do:

if(uop(obj,'nested.datetime.time.hours.stringFormat'))returntrue

And it will just return the value of the property if all nested properties are not undefined on the object or null if any of them are. If so, you should be responsible for finding out which of the properties is causing a problem, because actually UOP does not provide this functionality.

Getting started

  • Using the library
npm i --save @jorchgg/uop
constuop=require('@jorchgg/uop');constassert=require('assert');constobjWithNestedProperties={'nested': {'datetime': {'time': {'hours': {'stringFormat': 'Sun, 02 Jun 2019 17:40:59 GMT',},},},},};assert.strictEqual(uop(objWithNestedProperties,'nested.datetime.time.hours.stringFormat'),'Sun, 02 Jun 2019 17:40:59 GMT');assert.strictEqual(uop(objWithNestedProperties,'nested.inexistentProp.time.hours.stringFormat'),null);// It also supports arraysconstobjWithNestedArray={'nested': {'hours': [{'first': 9,},{'second': 10},],},};assert.strictEqual(uop(objWithNestedArray,'nested.hours[0].first'),9);assert.strictEqual(uop(objWithNestedArray,'nested.hours[1].second'),10);assert.strictEqual(uop(objWithNestedArray,'nested.hours[2].first'),null);
  • Running the tests npm test builds the library, then tests it.

License

MIT.

About

uop is a tiny package that helps strict undefined checking on objects with nested properties

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •