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

[IDEA] $t to effectively add optional chaining #5235

Open
MichaelLeeHobbs opened this issue Jun 8, 2022 · 0 comments
Open

[IDEA] $t to effectively add optional chaining #5235

MichaelLeeHobbs opened this issue Jun 8, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@MichaelLeeHobbs
Copy link

Is your feature request related to a problem? Please describe.
Mirth lacks optional chaining as Rhino lacks it. It is an extremely useful feature that saves a lot of typing

Describe your use case
Currently to safely access sub props you must use the es5 way to access the sub prop to avoid throwing if a parent prop is undefined

var d = (a && a.b && a.b.c) || undefined

// or
var d

try {
  d = a.b.c
} catch (ignore) {
}

Describe the solution you'd like
Add the following script as part of the default environment.

/**
 * $t is an inline try/catch to take the place of optional chaining ie var d = $t(()=> a.b.c) is the same as var d = a?.b?.c
 */
function $t(cb) {
  try {
    return cb()
  } catch (ignore) {
    // do nothing
  }
}

This would enable the following

var d = $t(()=> a.b.c)

// or 
var d = $t(()=> a.b.c) || 'default value'

It also enables other use cases beyond what standard optional chaining allows ie running unsafe code in a compact way.

var result = $t(() => someInstance.unsafeFunction())
if (result) {
  // do something
}

Describe alternatives you've considered
It's quite easy to add a global code template to add $t in but it has been so useful and would be very simple for the Connect team to add I believe it should be a core method in Connect.

@MichaelLeeHobbs MichaelLeeHobbs added the enhancement New feature or request label Jun 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant