schibsted / jslt

JSON query and transformation language
Apache License 2.0
630 stars 119 forks source link

builtin function `replace` does not support positional patterns #347

Open catull opened 4 months ago

catull commented 4 months ago

In issue https://github.com/schibsted/jslt/issues/340 I had to resort to 2 nested replace calls.

replace (
    replace (text, ",]", ]),
    ",}", "}
)

I wanted to collapse the two nested calls into one. Both patterns start with ,, one ends with }, while the other ends in ].

It would be great if this was supported:

replace (input, ",([}\])", "$1")

This way, ,} would result in }, and ,] would become ']'.

Or think of the eternal battle of date formats, ISO vs US format vs European.

def isoDateToUS (date)
  replace (date, "(\d\d\d\d)-(\d\d)-(\d\d)", "$2/$3/$1"

def isoDateToEuropean (date)
  replace (date, "(\d\d\d\d)-(\d\d)-(\d\d)", "$3/$3/$1"

I am not going to suggest that this be supported in this issue:

def isoToUSformat (date)
  replace (date, "(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})", "${month}/${day}/${year}")
catull commented 4 months ago

This issue can be seen as an extension of #346, I realize it now.