Javascript function to replicate a Monk (LISP variant) fucntion

Status
Not open for further replies.
Monk is LISP variant used by the eGate HL7 engine

https://docs.oracle.com/cd/E18867_01/SR ... ce_SRE.pdf

It has a function called list-lookup

list-lookup

Syntax
(list-lookup source-path destination_path lookup-list format trim-chars)

Description
Matches data in the source-path against the key elements of a list and copies the associated value into the structured event.

Code:
(list-lookup ~input%ADT.PID.PID-8-sex.CE.ID_code ~output%ADT.PID.PID-8-sex '(("1" "M")("2" "F")("M" "M")("F" "F")("Male" "M")("Female" "F") (else "U")) "" "")

Essentially it's a dictionary of string replacements and a default value if non match, what's an elegant way of replicating this functionality in javascript, currently I'm doing a big list of string.replace() commands.

Thanks
 
For the simple example shown, you could do this:
Code:
var map = {
  "1": "M",
  "2": "F",
  "M": "M",
  "F": "F",
  "Male": "M",
  "Female": "F",
};

var out = map[in] || "U";
If one of your values could be falsy (e.g. "", or undefined), you'd need something like:
Code:
var out = map.hasOwnProperty(in) ? map[in] : "U";

If your keys need to be something other than strings, it will get more complicated. If you need to extract the paths, there appear to be HL7 parsers for javascript, although I have no idea about their quality.
 
Status
Not open for further replies.