Destructuring assignments in JavaScript, combined with the ternary operator on object values is quite succinct yet expressive.


const { proxyType, proxyProtocol } = originalProtocol === 'ws' ? { proxyType: 'WebSocket', proxyProtocol: 'wss' } : { proxyType: 'HTTP', proxyProtocol: 'https' }


@aral maybe a bit less expressive, but way shorter:

const [ proxyType, proxyProtocol ] = originalProtocol === 'ws' ? [ 'WebSocket', 'wss' ] : [ 'HTTP', 'https' ]

@aral @severo_bo
Rust replaces ternary with if statements as expressions, e.g.

let (proxy_type, proxy_protocol) =
if original_protocol == "ws" {
("WebSocket", "ws")
} else {
("HTTP", "https")

You can also do:
let thing = if let Some(t) = maybe_thing {
} else {
warn!("maybe_thing wasn't a thing");
And because of the return (or continue in a loop), thing can be used. Since there is no implicit null and you need to check, I use this construct a lot.

Sign in to participate in the conversation
La Quadrature du Net - Mastodon - Media Fédéré

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!