safe-prop-getter
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

safe-prop-getter

A tool to safely access the properties

Intro

Sometime the object or json has nested object properties. When trying to access a property from undefined, like:

const resFromServer = {
  fruit: {
    name: 'banana',
    color: 'yellow'
  }
};
const colorOfBanana = resFromServer.fruit.color; // 'yellow'
const colorOfTomato = resFromServer.vegetable.color; //Uncaught TypeError: Cannot read property 'color' of undefined
// The following code will never be executed
 
// some code to process tomato
 
// some code to process banana

Instead, we manually check every object if it's undefined

if (resFromServer && resFromServer.vegetable) {
  const colorOfTomato = resFromServer.vegetable.color;
  // some code to process tomato
} else {
  // handle no vegetable
}
 
if (resFromServer && resFromServer.fruit) {
  const colorOfBanana = resFromServer.fruit.color;
  // some code to process banana
} else {
  // handle no fruit
}

but if there are many nested properties, it will be very tedious to check them all

// what we want to do is just getting id from request object
if (
  request &&
  request.body &&
  request.body.event &&
  request.body.event.data &&
  request.body.event.data.new &&
  equest.body.event.data.new.id
) {
} else {
  // handle no id here
  response.status = 400;
  response.error = 'invalid request';
}

with safe-prop-getter, you can simplify your property check like this

const sp = new SafeProp();
// even if request is an empty object
// sp.set(request).get('body.event.data.new.id').val is undefined, and no error will be thrown
if (sp.set(request).get('body.event.data.new.id').val) {
}

install

npm install safe-prop-getter

usage

basic

Javascript

const { SafeProp } = require('safe-prop-getter');

Typescript

import { SafeProp } from 'safe-prop-getter';
 
const testObj = {
  response: {
    body: {
      message: [
        {
          title: 'system',
          content: 'user info update successful',
        }
      ]
    }
  }
}
 
const sp = new SafeProp();
const date = testObj.request.header.date; //Uncaught TypeError: Cannot read property 'request' of undefined
const safeDate = sp.set(testObj).get('request.header.date').val; // undefined
const firstMessageTitle = sp.set(testObj).get('response.body.message[0].title').val; // 'system'
// you can use number as property
const firstMessageBody = sp.set(testObj).get('response.body.message.0.content').val; // 'user info update successful'
 
 
// you can run function as well
const testFuncObj = {
  fruit: {
    methods: [
      {
        log: (fruit) => {
              console.log(fruit);
              },
      }, {
        getAnother: () => {
          return 'banana';
        }
      }
    ]
  }
}
sp.set(testFuncObj).get('fruit.methods[0].log').val('apple'); // console.log('apple');
sp.set(testFuncObj).get('fruit.methods[1].getAnother').val(); // 'banana'
}
 

parameters

  1. mode: 'log' | 'strict' | 'default'(default value)
  • log mode: errors will be logged with console.error, like safeProp Error: Cannot read property request of undefined!
  • strict mode: errors will be thrown with message, like safeProp Error: Cannot read property request of undefined!
  • default mode: no errors will be logged or thrown, just return undefined (or null, if you configure returnEmptyType);
const testObj = {
  food: {
    fruit: {
      color: 'red',
      name: 'apple'
    }
  }
};
const spDefaultMode = new SafeProp(); // or const spDefaultMode = new safeProp('any string not equal to log or strict');
spDefaultMode.set(testObj).get('food.fruit.color').val; // 'red'
spDefaultMode.set(testObj).get('food.vegetable.color').val; // undefined
 
const spLogMode = new SafeProp('log'); // equal to const sp = new SafeProp(); sp.mode = 'log';
spLogMode.set(testObj).get('food.fruit.color').val; // 'red'
spLogMode.set(testObj).get('food.vegetable.color'); // console.error('safeProp Error: Cannot read property color of undefined!');
 
const spStrictMode = new SafeProp('strict'); // equal to const sp = new SafeProp(); sp.mode = 'log';
spStrictMode.set(testObj).get('food.fruit.color').val; // 'red'
spStrictMode.set(testObj).get('food.vegetable.color').val; // throw new Error('safeProp Error: Cannot read property color of undefined!');

2.returnEmptyType: 'undefined'(default value) | 'null' | 'generic'

2.1 returnEmptyType: 'undefined'(default value)

const testObj = {
  a: undefined,
  b: null
};
 
const spReturnUndefined = new SafeProp();
const a = spReturnUndefined.set(testObj).get('a').val; // undefined
const valueOfA = spReturnUndefined.set(testObj).get('a.value').val; // undefined
const b = spReturnUndefined.set(testObj).get('b').val; // null
const valueOfB = spReturnUndefined.set(testObj).get('b.value').val; // undefined

2.2 returnEmptyType: 'null'

const testObj = {
  a: undefined,
  b: null
};
 
const spReturnNull = new SafeProp('default', 'null'); // equal to: const spReturnNull = new SafeProp(); spReturnNull.returnEmptyType = 'null';
const a = spReturnNull.set(testObj).get('a').val; // undefined
const valueOfA = spReturnNull.set(testObj).get('a.value').val; // null
const b = spReturnNull.set(testObj).get('b').val; // null
const valueOfB = spReturnNull.set(testObj).get('b.value').val; // null

2.3 returnEmptyType: 'generic'

const testObj = {
  a: undefined,
  b: null
};
 
const spReturnGeneric = new SafeProp('default', 'generic'); // equal to: const spReturnGeneric = new SafeProp(); spReturnGeneric.returnEmptyType = 'generic';
const a = spReturnGeneric.set(testObj).get('a').val; // undefined
const valueOfA = spReturnGeneric.set(testObj).get('a.value').val; // undefined
const b = spReturnGeneric.set(testObj).get('b').val; // null
const valueOfB = spReturnGeneric.set(testObj).get('b.value').val; // null

Package Sidebar

Install

npm i safe-prop-getter

Weekly Downloads

2

Version

1.0.3

License

MIT

Unpacked Size

15.6 kB

Total Files

5

Last publish

Collaborators

  • felixwu