openhab / openhab-js

openHAB JavaScript Library for JavaScript Scripting Automation
https://www.openhab.org/addons/automation/jsscripting/
Eclipse Public License 2.0
38 stars 31 forks source link

Utils: Fix property names getters of dumpObject() #105

Closed florian-h05 closed 2 years ago

florian-h05 commented 2 years ago

Utils: Fix property names getters of dumpObject()

Description

Fixes #104 and adds a getter for the object's own enumerable property names.

Signed-off-by: Florian Hotze florianh_dev@icloud.com

Testing

I have used the following UI-script for testing:

var myObj = {
  foo: 'Hello',
  bar: 'world'
}

utils.dumpObject(myObj);

This logs:

2022-04-01 16:05:42.791 [INFO ] [org.openhab.automation.script.utils ] - Dumping object...
2022-04-01 16:05:42.792 [INFO ] [org.openhab.automation.script.utils ] -   typeof obj = object
2022-04-01 16:05:42.794 [INFO ] [org.openhab.automation.script.utils ] -   Java.isJavaObject(obj) = false
2022-04-01 16:05:42.796 [INFO ] [org.openhab.automation.script.utils ] -   Java.isType(obj) = false
2022-04-01 16:05:42.798 [INFO ] [org.openhab.automation.script.utils ] -   getOwnPropertyNames(obj) = foo,bar
2022-04-01 16:05:42.802 [INFO ] [org.openhab.automation.script.utils ] -   getAllPropertyNames(obj) = foo,bar,__proto__,constructor,hasOwnProperty,isPrototypeOf,propertyIsEnumerable,toLocaleString,toString,valueOf,__defineGetter__,__defineSetter__,__lookupGetter__,__lookupSetter__
digitaldan commented 2 years ago

Thanks, out of curiosity whats the benefit of listing getOwnPropertyNames and getAllPropertyNames since the later seems to be a super set ?

florian-h05 commented 2 years ago

Thanks, out of curiosity whats the benefit of listing getOwnPropertyNames and getAllPropertyNames since the later seems to be a super set ?

Yes, it's a superset of course, but I thing it is anyway practical too also have only the own property names, because usually I only want to see the own poperty names. E.g. a function in my rule dynamically adds properties to an object and I want to have a quick look on what has been added.