adobe / json-formula

Query language for JSON documents
http://opensource.adobe.com/json-formula/
Apache License 2.0
19 stars 8 forks source link

Add a register() function #54

Closed JohnBrinkman closed 1 year ago

JohnBrinkman commented 1 year ago

When we have long-running json-formula sessions (as with forms), we often encounter complex expressions that are repeated later in the session. For example, given an ISO8601 duration value such as: {"cookTime": "PT1H45M"} The following expression will transform it to English text:

substitute(cookTime, 'PT', '') |
  split(@, 'H') |
  reduce(&accumulated & if (find('M', current),' ' & substitute(current, 'M', ' Minutes'), current & if (current != '1', ' hours', ' hour')), @)

We propose to add the ability to register such an expression as a function to allow the author to keep only one copy of the logic. e.g.

register('duration', &substitute(@, 'PT', '') |
  split(@, 'H') |
  reduce(&accumulated & if (find('M', current),' ' & substitute(current, 'M', ' Minutes'), current & if (current != '1', ' hours', ' hour')), @)
)

Then for the remainder of the session, a duration can be displayed using:

duration(cookTime)

All registered functions may accept only one parameter -- referenced in the expression using the current context @. If more parameters are required, they can be combined in a map or array.

vdua commented 1 year ago

Resolved

vdua commented 1 year ago

Resolved