Skip to content

Commit

Permalink
New: util.isset(obj, prop) can be used to test if a message property …
Browse files Browse the repository at this point in the history
…is considered to be set, see #728
  • Loading branch information
dcodeIO committed Mar 25, 2017
1 parent 4bfe0c2 commit 7ecae9e
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/util/minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ util.isObject = function isObject(value) {
return value && typeof value === "object";
};

/**
* Checks if a property on a message is considered present.
* @param {Object} obj Plain object or message instance
* @param {string} prop Property name
* @returns {boolean} `true` if considered present, otherwise `false`
*/
util.isset = function isset(message, prop) {
var value = obj[prop];
if (value != null && obj.hasOwnProperty(prop))
return typeof value !== 'object' || (Array.isArray(value) ? value.length : Object.keys(value).length) > 0;
return false;
};

/*
* Any compatible Buffer instance.
* This is a minimal stand-alone definition of a Buffer instance. The actual type is that exported by node's typings.
Expand Down

2 comments on commit 7ecae9e

@tamird
Copy link

@tamird tamird commented on 7ecae9e Mar 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat! Can you add typings for this?

@dcodeIO
Copy link
Member Author

@dcodeIO dcodeIO commented on 7ecae9e Mar 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should already be there - typings are automatically generated from JSDoc. Object could be problematic, though. Is it?

Please sign in to comment.