Customized functions

Hello,
Follwing my support email, would it be possible to show how can we customize the functions when handling the data (from collection or rest API) please?

I am struggling a bit with String functions as “replace” doesn’t work as expected and I can’t find a way to concat 2 strings.

Thank you in advance.

Hello @bktag

This would require building a plugin :slight_smile:
You would be actually first one who will do it, here’s how:

  1. Install Creator application

  2. From sidebar, open creator, then create new plugin, name it ‘Custom Replace’ or something similar.
    image

  3. Create your first javascript file (name it replace.js or something similar)
    image

  4. Paste this code

mdFunctions.OnReady(() =>
{
    mdFunctions.ItemAdd({
        id: 'my-replace',
        parameters: {
            pattern: {type: 'INPUT'},
            keyword: {type: 'INPUT'},
        },
        description: 'Replace pattern with keyword.',
        group: 'String',
        function: function(data, value, pattern, keyword)
        {
            if(typeof value !== 'string')
            {
                return value;
            }
    
            // Perform JS here on the 'value' (value - value that function get's, eg: text)
            return 'this is new replaced value';
        }
    })
});
  1. Save and then refresh the website. You will then see this function to select as well named, ‘my-replace’

Let me know if you had any success.
In this way you can build plugins and publish to marketplace :slight_smile:

That looks cool! Will have a try and let you know :slight_smile:

Just to let you know I created a function (addToString). It’s rather simple and answer to my specific need :smiley: (adding URL to an image path).
Might add later a customizable concat function :slight_smile:

@bktag

So everything works fine?

We will be releasing documentation in coming months, so you will have ability to extend/modify entire builder. It’s very powerful :slight_smile:

1 Like

Yes it worked! Thanks!
And indeed, Divhunt looks quite powerful! You guys will have a lot of work to do to document all the possibilities :sweat_smile:

Thanks for sharing this! It is indeed super powerful!

Quick question here … I added this as a “Plugin”:

mdFunctions.OnReady(() =>
  {
      mdFunctions.ItemAdd({
          id: 'getNameOfDayFromIndex',
          parameters: {
              dayOfIndex: {type: 'INPUT', value: ''},
          },
          description: 'Get the ',
          group: 'String',
          function: function(data, value, dayOfIndex)
          {
      
              // Perform JS here on the 'value' (value - value that function get's, eg: text)
              const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
              return `${days[dayOfIndex]} | ${dayOfIndex} | ${value}`
  
          }
      })
  });

I am just trying to understand the parameters.

data is required and indeed has information in it.
value, however, is reported as null, but the function breaks if it is not included as a parameter.

Are these just kind of always required and pass-in parameters for the other data points needed?

Also, what is the best way to import an external module/package. I would like to be able to use Day.js in plugins, but I could not find a way to import the code. Here is the link to their docs: Installation · Day.js

value is the value being passed from variable.

So for example if you’ve showing Collection name

$name->getNameOfDayFromIndex(5)

In this case, $name will be value inside function, if is NULL that means that you are using invalid/non-existing variable.

It’s always good idea to validate value, for example you can say

if(!value || !dayOfIndex)
{
return 'Monday'; // Or 'Invalid day' or something.
}