Giter VIP home page Giter VIP logo

Comments (15)

3cp avatar 3cp commented on June 1, 2024

This is expected behavior.

foo-baz is not a valid JS variable, neither it works in bcx expression.

In JS, dash is not one of the chars that can be used in variable name. When you parse foo-baz through any JS syntax parser, it becomes a math expression foo - baz. The ambiguity is probably why dash is not allowed in JS (and almost all other languages) variable name.

from bcx-expression-evaluator.

thorek avatar thorek commented on June 1, 2024

Sadly accepted :-(

from bcx-expression-evaluator.

thorek avatar thorek commented on June 1, 2024

To be fair: a . (dot) is also not considered a valid char in a variable name in JS. But as part of a JSON key considered by the parser. One could argue the same case for - (dash).

from bcx-expression-evaluator.

3cp avatar 3cp commented on June 1, 2024

foo.bar is not one variable, it's parsed the same way as JS, to access property bar on variable foo.

from bcx-expression-evaluator.

3cp avatar 3cp commented on June 1, 2024

To the expression being evaluated, the context object is something like global name space. Comparing to JS, it's like window object in browser. That's why you can access foo as a variable.

from bcx-expression-evaluator.

3cp avatar 3cp commented on June 1, 2024

BTW, if getting a path from an json is the only thing you want. It's cheaper to just use lodash.
bcx-expression-evaluator might be an overkill for that use case.

_.get(json, 'foo.bar');
_.get(json, 'foo-baz');
// it can go through array too
_.get(json, 'array[0].field.deeperField');

from bcx-expression-evaluator.

thorek avatar thorek commented on June 1, 2024

Yes, I know. But the use case is different. We operate on a given valid JSON in the context and want to do:

const context = {"foo": {"bar-baz": 23, "bar-bay": 42 } };

expect( evaluate('foo.bar-baz', context ) ).toBe(23);
expect( evaluate('foo.bar-bay == 42', context ) ).toBe( true );
expect( evaluate('foo.bar-baz > foo.bar-bay', context ) ).toBe( false );
expect( evaluate('foo.bar-bay - foo.bar-baz', context ) ).toBe(19);

from bcx-expression-evaluator.

3cp avatar 3cp commented on June 1, 2024

If you want, you can modify this method to accept dash as part of identifier.

function isIdentifierPart(code) {

from bcx-expression-evaluator.

3cp avatar 3cp commented on June 1, 2024

After you modify it, it means you can only use minus operator with space around it.

from bcx-expression-evaluator.

thorek avatar thorek commented on June 1, 2024

Thanks!

Using whitespace around identifier is our convention anyhow.

How about making this a configuration option?

from bcx-expression-evaluator.

3cp avatar 3cp commented on June 1, 2024

Sorry, I am not going to support it since it differs from JS.

You need to fork.

from bcx-expression-evaluator.

thorek avatar thorek commented on June 1, 2024

That is of course your decision and I thank for your support and effort with providing this to the community - which I highly respect.

But also with respect I disagree. A dash is a valid character in a JSON object key. So allowing to use this as such wouldn't violate the standard.

from bcx-expression-evaluator.

3cp avatar 3cp commented on June 1, 2024

You did not get the point, this “expression” is trying to mimic JavaScript language expression, not an another JSON path lib like XPATH for xml.

We support a subnet of JavaScript expression, not inventing anything else. foo.foo-baz is not a valid JavaScript expression.

from bcx-expression-evaluator.

3cp avatar 3cp commented on June 1, 2024

A dash is a valid character in a JSON object key.

You have to calm down and understand you cannot make an argument on chars in json key. As json key is arbitrary string, including white space, dot, +, *, /, (, ...

The whole reason you want to support dash is not because json supports it. It is only for your use case.

from bcx-expression-evaluator.

thorek avatar thorek commented on June 1, 2024

I am sorry, when I let the impression I wasn't "calm". I am.

You are absolutely right in regards of arbitrary strings in in JSON keys, as well as it is our special use case that drives the request.

I still believe it would be a great and reasonable opportunity to have the ability to include "non-variable-chars" to the identifier as an option to the evaluate method. But of course I accept your decision on this.

from bcx-expression-evaluator.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.