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

Expressions: get lookup not evaluated before passing to function #6941

Closed
rconjaerts opened this issue Jul 10, 2018 · 2 comments
Closed

Expressions: get lookup not evaluated before passing to function #6941

rconjaerts opened this issue Jul 10, 2018 · 2 comments

Comments

@rconjaerts
Copy link

Issue

Currently when an expression is created with a call to a function the passed arguments are not evaluated before calling that function. For example the following expression won't be evaluated as intended:

["<=", new Date(["get", "inservicedate"]).getTime(), new Date().getTime()]

The idea is that I would get the property inservicedate from my feature, create a date from it and get the time in as a number so that I can use it in my expression to filter. The issue is that ["get", "inservicedate"] doesn't get evaluated (thus not getting the property), but just passed as a parameter (array objec) to the Date() function which results in a null value.

Is this a bug or am I interpreting the docs of mapbox incorrectly? I can change this into a feature request with the following proposed solutions.

Proposed solutions

  1. Support for date objects (besides number, string, boolean, color, array) since this a basic property type when working with a companies assets on a map.
  2. Ability to call a function in an "expressions" way. ["fn", function, value, value, value, ...], so that in my case this would result in: ["fn", new Date(), ["get", "inservicedate"]], where my "get" would actually be evaluated before passed to the function that would return a value of the users choosing.
@jfirebaugh
Copy link
Contributor

Hi @rconjaerts, thanks for using Mapbox. As you've discovered, you can't intermix Mapbox GL expressions with Javascript execution in this way. In the course of constructing an expression (which takes the form of a Javascript array), you can evaluate Javascript -- for instance, including the result of new Date().getTime(). But after that, you should think of an expression as its own self-contained program, which will be evaluated separately from Javascript execution. So you can't feed ["get", "inservicedate"] into the constructor of Date, as you're trying to do here. That's invalid because at the point that new Date(...) is evaluated, ["get", "inservicedate"] is just an array, and the Date constructor doesn't accept that kind of argument.

You could potentially implement date parsing via a (likely very complex) expression, but it would be quite challenging. So I've added a request for support for date and time types within the expression language to #6484. Until then, the best way to accomplish what you're going for here is probably to adapt the data, supplying a field that contains a count of milliseconds-since-the-epoch directly, and using that for the comparison.

@stdmn
Copy link

stdmn commented Sep 1, 2018

+1 on this request. Would be immensely helpful to use DateTimes in expressions to do something like Kepler.gl's DateTime slider (http://kepler.gl/#/demo/earthquakes).

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

No branches or pull requests

3 participants