burton999dev / CalcNoteHelp

Help for CalcNote
25 stars 1 forks source link

Accept non-numeric values for JS function parameters #26

Open Immateria opened 2 months ago

Immateria commented 2 months ago

It would make the user functions so much more powerful and useful if you allowed any type as input. I can understand needing the result to be a number, given the context, but not being able to use strings hamstrings the custom functions.

An example is that I would like sum to take an arbitrary number of parameters and sum them, but the version of Rhino doesn't support the rest operator. So, the obvious solution seemed to be pass in a string and split it on the comma, then convert each item into an int, add them, return the sum. Can't do it. The only workaround I have found is silly.

function sums(hexInput) {

   var hexRegex = /^[0-9a-fA-F]+$/;

   if (!hexRegex.test(hexInput))
       return hexInput;

   var hexString = hexInput.toString(16).toUpperCase();

   var parts = hexString.split('AA');

   var res = parts.reduce((sum, part) => sum + parseInt(part, 10), 0);

   return res;
}

So I can use: sums(0x1aa2aa3aa4aa5) and I get 1 - 5 added up.

CalcNote is a phenomenal app, I bought pro within a day or two of first trying it, but this is a major pain point, and would make your app much more powerful and user friendly. I ask you to consider it.