Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
2 changes: 1 addition & 1 deletion src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
6 changes: 3 additions & 3 deletions src/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
10 changes: 10 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down