Use Variables Without `.value`

Whenever you use a variable, you have to add .value to access its actual value. Believe it or not, this small distinction has already caused me quite a few bugs. Plus, it makes my lines of code significantly longer.

While I understand the reasoning behind it—it allows extracting properties like name and pluginType—I rarely find this use case particularly useful. In other words, why not return the contents of .value by default? Instead of writing variable.value, you could simply use variable directly: {{ variable }}.

After looking into it, I found that JavaScript actually supports this behavior, so I don’t see why it wouldn’t be possible to implement:

const myVar = {
  name: 'myVar',
  value: 'Mark',
  pluginType: 'state',
  toString() {
    return typeof this.value === 'string' ? this.value : String(this.value);
  },
  valueOf() {
    return typeof this.value === 'number' ? this.value : NaN; // or return this.value if you want non-numeric values to pass through
  }
};

I’d love to know if this is feasible and, more importantly, I'm curious why it wasn’t implemented in the first place. :nerd_face:

:wave: Thanks!

1 Like

Can I add some other requests? :smiley:

It'd be great if they had helper properties like an "isEmpty" too - so I don't have to check the value of the variable and compare it to null or empty or false etc

Things like "only run if" or "hidden" or "disabled" properties that could be written with a {{ variable.isEmpty }} would be neater to write and easier to read

2 Likes

Yes, absolutely. I also think the Queries could benefit from such a property, which checks whether there are any results.

Your suggestion to use isEmpty for variables can also be flipped to a positive, such as variable1.filled or variable1.hasValue. The best choice likely depends on the context—for example, in a disabled setting, your suggestion makes more sense.

Perhaps include both isEmpty and isNotEmpty, or alternatively, isFilled and isNotFilled.

And before someone suggests using the Lodash method _.isEmpty(variable.value)—just know there's a major caveat: this function returns TRUE when evaluating _.isEmpty(3545) (or any other number).

Hope this helps! :wave:

1 Like

That's the thing, as you say there are already ways to do all of these but some convenience/helper functions would make things a little easier to write and read

1 Like

Yeah, I guess one could also add custom functions in the Advanced Settings, for instance.

window.has = function(variable) {
    return !_.isEmpty(_.toString(variable.value || variable));
}

Not sure if this is the best way to do this, typed it off the top of my head. :wink:

But there could be ways around it, is what I am suggesting. But it would rely on your own implementation and that is never as good as native behavior.