diff --git a/index.d.ts b/index.d.ts index be394f08f..49fbff132 100644 --- a/index.d.ts +++ b/index.d.ts @@ -2147,6 +2147,14 @@ export namespace util { */ function safeProp(prop: string): string; + /** + * Returns the value of a property found directly in a given object. + * @param object Source object + * @param prop Property name + * @returns Value or `undefined` if not set + */ + function getProp(object: object, prop: string): any; + /** * Converts the first character of a string to upper case. * @param str String to convert diff --git a/src/namespace.js b/src/namespace.js index de9f4cdb0..262dc5697 100644 --- a/src/namespace.js +++ b/src/namespace.js @@ -191,7 +191,7 @@ Namespace.prototype.addJSON = function addJSON(nestedJson) { * @returns {ReflectionObject|null} The reflection object or `null` if it doesn't exist */ Namespace.prototype.get = function get(name) { - return this.nested && this.nested[name] + return util.getProp(this.nested, name) || null; }; diff --git a/src/service.js b/src/service.js index bc2c3080c..0d2b9428b 100644 --- a/src/service.js +++ b/src/service.js @@ -98,7 +98,7 @@ function clearCache(service) { * @override */ Service.prototype.get = function get(name) { - return this.methods[name] + return util.getProp(this.methods, name) || Namespace.prototype.get.call(this, name); }; diff --git a/src/type.js b/src/type.js index 2e7bda49b..7835dab57 100644 --- a/src/type.js +++ b/src/type.js @@ -312,9 +312,9 @@ Type.prototype.resolveAll = function resolveAll() { * @override */ Type.prototype.get = function get(name) { - return this.fields[name] - || this.oneofs && this.oneofs[name] - || this.nested && this.nested[name] + return util.getProp(this.fields, name) + || util.getProp(this.oneofs, name) + || util.getProp(this.nested, name) || null; }; diff --git a/src/util.js b/src/util.js index c39d33a6a..2ef38f878 100644 --- a/src/util.js +++ b/src/util.js @@ -78,6 +78,16 @@ util.safeProp = function safeProp(prop) { return "." + prop; }; +/** + * Returns the value of a property found directly in a given object. + * @param {Object} object Source object + * @param {string} prop Property name + * @returns {*} Value or `undefined` if not set + */ +util.getProp = function get(object, prop) { + return object && Object.prototype.hasOwnProperty.call(object, prop) ? object[prop] : undefined; +}; + /** * Converts the first character of a string to upper case. * @param {string} str String to convert