Skip to content

Commit 7ecae9e

Browse files
committed
New: util.isset(obj, prop) can be used to test if a message property is considered to be set, see #728
1 parent 4bfe0c2 commit 7ecae9e

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/util/minimal.js

+13
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ util.isObject = function isObject(value) {
7070
return value && typeof value === "object";
7171
};
7272

73+
/**
74+
* Checks if a property on a message is considered present.
75+
* @param {Object} obj Plain object or message instance
76+
* @param {string} prop Property name
77+
* @returns {boolean} `true` if considered present, otherwise `false`
78+
*/
79+
util.isset = function isset(message, prop) {
80+
var value = obj[prop];
81+
if (value != null && obj.hasOwnProperty(prop))
82+
return typeof value !== 'object' || (Array.isArray(value) ? value.length : Object.keys(value).length) > 0;
83+
return false;
84+
};
85+
7386
/*
7487
* Any compatible Buffer instance.
7588
* This is a minimal stand-alone definition of a Buffer instance. The actual type is that exported by node's typings.

0 commit comments

Comments
 (0)