From 7ecae9e9f2e1324ef72bf5073463e01deff50cd6 Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Sat, 25 Mar 2017 22:22:38 +0100 Subject: [PATCH] New: util.isset(obj, prop) can be used to test if a message property is considered to be set, see #728 --- src/util/minimal.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/util/minimal.js b/src/util/minimal.js index 9fde30976..b03d80cda 100644 --- a/src/util/minimal.js +++ b/src/util/minimal.js @@ -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.