-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Add support for slicing strings and arrays in expressions #9443
Comments
Thanks for the detailed writeup @lbutler ! I think this sounds like a good idea, and a PR is defnitely welcome! This would fit in well with the Also for your use-case, if you have control over the data, I'd recommend preprocessing it and splitting up the data into it up into multiple properties. Its always better to store measurements in one unit system and convert to the desired one on the client based on user preference. |
Agree that including an I'll start with a PR for Unfortunately for me, I'm working with user-supplied data from water utilities and most mix metric and imperial measurements for pipe diameters and store them in a single property as a string. Also, my use-case is probably outside how most people would use expressions. I import |
Hey @lbutler thanks for this writeup. I just want to echo @arindam1993's thoughts above. We have a whole class of expressions that work on strings and arrays, so I think this is a great addition. The
You may be interested in following along with #7670 which is about making expressions work outside of rendering |
Thanks @arindam1993 & @ryanhamley! I've pushed a PR for both
I've got it subscripted, will be awesome when it's available! I've been using the the gist that was linked for typescript for the time being. |
Motivation
Slicing of both strings and arrays is listed on the expression API master ticket #6484.
I currently have data sets where I need to extract part of a string before the underlying data can be used. A simple example for me would be trying to style an attribute like "100mm". Ideally, I'd like to strip the unit from the string and be left with only the integer. In more complex cases, I also need to convert the integer based on the unit that is found.
As this is a significant limiter for my own use case, if the below is OK, I'd like to try to implement the new expression and create a pull request.
Design Alternatives
At the moment I believe with expressions, there are no options to modify or change strings. Any modifications would need to be done to the base data before it is handed to the expression.
When search/regex is added it could be used as an alternative. However, that feature seems like it may be more complex to add at this time.
Design
Javascript contains multiple methods to extract part of a string or an array; as the master ticket specifically says slice and the methods are the same for strings and arrays I believe this is what should be implemented.
If we were just looking at strings you could go with a more verbose
Left
,Right
&Mid
expressions similar to Excel, which may be more approachable for newer programmers. However, you can achieve all the same from the singleSlice
method and it isn't a large learning curve with plenty of examples online.Mock-Up
Slice Expression API:
String examples as per MDN .slice() docs
Array examples as per MDN .slice() docs
Concepts
As slice is in the master ticket, I believe this is what Mapbox wants to go forward with. Though as mentioned in the design section there are multiple methods, some of which are legacy, within Javascript to modify strings and arrays.
Implementation
I assume it would be modelled similar to the existing expression
["length"]
at/src/style-spec/expression/definitions/length.js
which handles arrays and strings.I'm not familiar with the other SDKs that implement Mapbox expressions and if JS slice methods are handled in a similar way on those languages.
The text was updated successfully, but these errors were encountered: