0

I was wondering if there was a standard library or external library (eg lodash) function that essentially does following:

function f(options) {
  const x = options.x;
  if (x === null || x === undefined) throw new Error('x is required option');
  ...
}

I have searched various key words in the lodash documentation but have yet to find something like this.

Dylanthepiguy
  • 1,621
  • 18
  • 47

2 Answers2

1

To check if a property exists, you can just write if(x in options)... Libraries exist to make tedious or difficult operations simple, This is such a simple operation that no library is needed and you'd be unlikely to find it added to a library.

While .hasOwnProperty() can work, it will only test for properties attached directly to the object in quesiton, while the in approach tests for inherited properties as well.

But, be careful about throwing errors. It's not generally something that you want to do for performance reasons. It's better to return a code or message.

var options = {
  foo:42,
  bar:"doesn't matter"
};

function propTest(propName, obj){
  // Instead of throwing an error, return a value that indicates success or not
  return (propName in obj) ? true : false;
}

console.log(propTest("foo", options));
console.log(propTest("bar", options));
console.log(propTest("baz", options));
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
0

You can write a function that iterates through an objects keys and looks for undefined ones

function CheckObjectProperties(obj)
{
  for (var key in obj)
  {
    if(!obj[key])
    {
      throw new Error('Object has one or more undefined members');
    }
  }
}

or if you wanna check if an unknown object you can do something along the line of this. This approach is much more dynamic and reusable :D

function CheckObjectForProperties(obj, arraryOfStringProperties)
{
  for(let i = 0; i < arraryOfStringProperties.length; i++)
  {
    let key = arraryOfStringProperties[i];
    if(!obj[key])
    {
      throw new Error('Member not Found in Given Object.');
    }
  }
}


let myObject = {
  x : 1,
  y : 2,
  z : undefined
}

let requiredProperties = ['x', 'y', 'z'];
CheckObjectForProperties(myObject, requiredProperties);
Eddie D
  • 1,120
  • 7
  • 16
  • No loop is needed with `in` to determine if a property exists unless you want to iterate each key for some other reason. – Scott Marcus May 31 '18 at 21:57
  • The second option lets you pass in an array of say, required keys. You loop through each key and check if it exists in the object – Eddie D May 31 '18 at 21:59
  • For that, I'd just use `.indexOf` - still no loop needed. – Scott Marcus May 31 '18 at 22:00
  • Not quite what I'm getting at. Imagine you have an object of so many properties. And whenever you encounter that, you want to ensure it has certain required key value pairs, how can you apply that principal to each instance, and adjust that easily? I expanded the second snippet to provide an example – Eddie D May 31 '18 at 22:04
  • Then, I'd stringify the object and use `.indexOf()`. No loops needed. – Scott Marcus Jun 01 '18 at 00:01