silverbulletmd / silverbullet

The hackable notebook
https://silverbullet.md
MIT License
2.01k stars 138 forks source link

null coealsce / "truthy or" #894

Open DanCardin opened 1 week ago

DanCardin commented 1 week ago

Usecase:

{{#each {sometag}}}
* [[{{ displayName }}]]
{{/each}}

displayName is frequently undefined, so where it is undefined, i'd like to fall back to name.

As far as I can tell, the easiest way to achieve this today is displayName: displayName : name (if there's something else, let me know, and perhaps this is just an expression-language-page update). This mainly just an unfortunate pattern because it repeats displayName twice each time

What might be nice as an alternative is:

EDIT: yes i meant displayName ? displayName : name above

MrMugame commented 1 week ago

Do you mean displayName ? displayName : name? I have not seen the other syntax

MrMugame commented 1 week ago

It seems to be that only when your first value exists "true" is returned. So this definitely is a bug/weird behaviour. Imo it should just return v instead of true

const op1Val = evalQueryExpression(
  op1,
  obj,
  variables,
  functionMap,
);
if (op1Val instanceof Promise) {
  return op1Val.then((v) =>
    v ||
    evalQueryExpression(val[2], obj, variables, functionMap)
  );
} else if (op1Val) {
  return true;
} else {
  return evalQueryExpression(val[2], obj, variables, functionMap);
}
terr-steak commented 1 week ago

Don't forget about the '??' (nullish coalescing) operator as defined here and discussed here.

I'd have to check on its implementation insofar as SB templating goes later when not on mobile but, just thought it worth a mention in this thread.