diff --git a/src/jsonpath.js b/src/jsonpath.js index 54e27bf..52fdf3b 100644 --- a/src/jsonpath.js +++ b/src/jsonpath.js @@ -1,4 +1,3 @@ -const {hasOwnProperty: hasOwnProp} = Object.prototype; /** * @typedef {null|boolean|number|string|PlainObject|GenericArray} JSONObject @@ -12,6 +11,17 @@ const {hasOwnProperty: hasOwnProp} = Object.prototype; * @typedef {any} AnyResult */ +/** + * Check if the provided property name exists on the object. + * @param {string} name Name of the property to check + * @returns {boolean} Whether the object has the property or not + */ +function hasOwnProp (name) { + return typeof this === 'object' + ? name in this + : Object.prototype.hasOwnProperty.call(this, name); +} + /** * Copies array and then pushes item into it. * @param {GenericArray} arr Array to copy and into which to push diff --git a/test/test.class.js b/test/test.class.js new file mode 100644 index 0000000..3fee84f --- /dev/null +++ b/test/test.class.js @@ -0,0 +1,43 @@ +import {checkBuiltInVMAndNodeVM} from '../test-helpers/checkVM.js'; + +checkBuiltInVMAndNodeVM(function (vmType, setBuiltInState) { + describe(`JSONPath - Properties (${vmType})`, function () { + before(setBuiltInState); + + /** + * + */ + class Test1 { + /** + * Test2. + */ + constructor () { + this.test2 = "test2"; + } + + /** + * Test3. + * @returns {string} + */ + // eslint-disable-next-line class-methods-use-this + get test3 () { + return "test3"; + } + } + const json = new Test1(); + + it("Checking simple property", () => { + assert.equal( + jsonpath({json, path: "$.test2", wrap: false}), + "test2" + ); + }); + + it("Checking getter property", () => { + assert.equal( + jsonpath({json, path: "$.test3", wrap: false}), + "test3" + ); + }); + }); +});