Using JS Split Function on Command Value

Hi Guys - I frequently use the .substr with command values and it works well. Now I need to separate values by a comma deliminated list passed into SambaPOS via a message.

My command value that comes in is as such: typeCreate,field1,field2,field3,field4

In my Rule for my action to be fired, I’ve added these to try to deliminate the above fields:

[='[:Command]'.split(",")[1]]
[='[:Command]'.split(",")[2]]

My goal is for the first of the above to pass “field1” and the second, “field2”

This however does not work. I based this logic off a method that does work which is: [=’[:Command]’.substr(15)]

Does anybody see the syntax error or know why this fails? Thanks.

I think most likely it failed because of square bracket [1], lot of chances parsing error with square bracket. Why not just use call function for substr?

Just make generic something like this (not test)

function getArrayValue(array,index) {
    array=array.split(",");
    return array[index]
}
1 Like

@sukasem -

That may be slightly beyond my understanding of the rules/action system in SambaPOS which do appreciate and continue learning. It’s funny I can write plenty of node.js but not handle a few simple functions in SambaPOS.

Basically I actually tried to pass the whole thing to a call function first and then do the split there but it failed, I had this as an example:

{CALL:processor.NewInput('[:Command]')}

and then

function NewInput(dataIn) {
var returnMe = dataIn.split(",")[1];
return returnMe;
}

This however produced nothing in the return.

This may work in node.
Remember Samba has older JS comaptibility.

1 Like

Yeah, it’s probably my [lack of] programming skills and not SambaPOS though. I think the string split function should work.

Split work but you need a separate index from it.

IIRC function like trim still not support. Lots of function seem to be basic but still not support.

Maybe that why lot of ppl say should learn vanilla JS before learning Node.js.

1 Like

SambaPOS uses a JScript Engine which is Microsoft’s old version of Javascript which is ECMAscript 3 (ES3).

The trailing [index] after your split() is probably not supported in ES3. So you need to do it in 2 steps…

function NewInput(dataIn,idx) {
  var returnMe = dataIn.split(",");
  return returnMe[idx];
}

EDIT: sorry, just noticed @sukasem posted almost exactly the same thing :stuck_out_tongue:

2 Likes

Thank you (both). I never could get this to work, I ended up just using GQL to create the entity, was just trying to avoid more GQL but hey, why not :wink: