Jump to content
Sign in to follow this  
Daniel Eiszele

TVariant Extension Function

Recommended Posts

I recently had the need to enumerate the values of properties on a class instance, which can be done in Javascript using the "Object.values" function. In SMS the TVariant class abstracts/maps most of the Javascript "Object" functions, but it is missing this function (unless it's squirrelled away somewhere I couldn't find it).  To fix it I modeled the following function after TVariant.GetKeys.

class function TVariant.Values(const Data : Variant): TStrArray;
    if (Object.values) {
      @result = Object.values(@Data);
    } else {
      @result = [];

It probably requires shims to ensure it works in all browsers but according to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values it works in all the major ones.

@jarto Can we please get this function added to the RTL for completeness or let me know if there is a more SMS like way to achieve the same ends.

Share this post

Link to post
Share on other sites

@Daniel Eiszele Thank you, I'll add it.

There is also TVariant.ForEachProperty, which can be used for traversing all the properties. It's handy as you get both the name and the value in the same callback. However, it had a bug in it and always returned the value as undefined.

  TVariant.ForEachProperty(MyVariant, function (Name: string; Data: variant): TEnumState
      WriteLn('Name of the property is: '+Name);

I'm going to include the new TVariant.Values and the bug fix to TVariant.ForEachProperty in the next update.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this