Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evaluate also getter from classes #177

Open
WebDucer opened this issue Aug 19, 2022 · 1 comment
Open

Evaluate also getter from classes #177

WebDucer opened this issue Aug 19, 2022 · 1 comment
Labels

Comments

@WebDucer
Copy link

WebDucer commented Aug 19, 2022

Could be related to #174

Motivation

Currently the getter from classes, that encapsulate fields, are not "visible" to the library, so it is not possible to get a value from getter property. Becase getter are "seen" from developer as readonly fields, the library should at least also be able to "read" them.

Current behavior

getter are ignored

Desired behavior

The getters should be evaluated exactly the same as pure fields in classes.

Code example

import { JSONPath } from 'jsonpath-plus';

class TestData {
  #value;
  
  constructor(value) {
    this.#value = value;
  }

  get value() {
    return this.#value;
  }

  toJSON() {
    return { jsonValue: this.value };
  }

  someValue = 33;
}

const data = new TestData(11);
console.log(data); // TestData {someValue: 33}
console.log(data.value); // 11
console.log(data['value']); // 11
console.log(JSON.stringify(data)); // {"jsonValue":11}

const val = JSONPath({ path: '$.value', json: data });
const val1 = JSONPath({ path: '$.jsonValue', json: data });
const val2 = JSONPath({ path: '$.#value', json: data });
const val3 = JSONPath({ path: '$.someValue', json: data });

console.log(val); // [] - expected [11]
console.log(val1); // []
console.log(val2); // []
console.log(val3); // [33]

console.log(Object.prototype.hasOwnProperty.call(data, 'value')); // false
console.log('value' in data); // true
@LoicMahieu
Copy link

Related to PR #197

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants