Issue with brackets in JavaScript strings

We have a complex verification code that is been generated by a script (JavaScript) and consumed by a Printer Template.

The problem is when we pass any string that includes the “[” or “]” characters. SambaPOS is doing something with those characters. We tried to debug this problem with Visual Studio but whenever the string includes brackets SambaPOS throws this error:

We understand that the “” must have a special treatment in strings. Is this also the case for brackets? Or, is this a bug?

Pass it from where and how are you passing it? Its hard to understand your problem without seeing it.

1 Like

Without seeing your template or script, I guess you script return bracket to printer template and causing printer template error.

You better post your template and script so, we can see what the problem really is.

@Jesse you are seconds ahead of me lol

2 Likes

I have simplified the problem to be able to clearly explain myself. Please take a look at the following function (stored in a “tests” script):

function getVerificationCode(number, key) {
    var r = '';

    r = "number:" + number + " of type " + typeof number + "    key:" + key + " of type " + typeof key;

    return r;
}

I make three calls to the function from a template:

<L00>Test 1: {CALL:tests.getVerificationCode(5, "abc")}
<L00>Test 2: {CALL:tests.getVerificationCode(5, "a}]c")}
<L00>Test 3: {CALL:tests.getVerificationCode([=TN('{TICKET TOTAL}')], "abc")}

The first call returns:
Test 1: number:5 of type number key:abc of type string

The second call throws this error (dialog box with OK button):
"Template content has invalid bracket syntax.

The third line returns an empty result (possibly “undefined” data). What I expect is to have the Ticket total as a number and pass it as a parameter.
Test 3:

Does this explanation help?

This is mechanism to check bracket system Emre put in to help prevent error.
So, if you have open and close bracket there is no error.

Why would you need bracket in the key?

Can you try without [=TN( )]?

The bracket is part of a string. I think it should be ignored on such validation.

Because it is part of printer template. Unless Emre can give option to disable template checking.

Still don’t understand why would you need bracket. You can have very long char like those Public/Private keys.

It works without it but what I receive in the function is a string like this “1,156.08”. The thousand and decimal separators van vary, that is why I am hoping to be able to use TN(). I need a floating point number in that parameter

Use script to convert to number. Maybe try Number("1,156.08"); or number=Number(number); base on your script;

I already looked for it. It is hard, JavaScript does not know the current locals.

Do you know why I cannot use [=TN( )]?

maybe use
number=number.replace(",","");
number=Number(number.replace(".",","));

Could be one line but not sure with one will run first?

I think I would not work if the local settings are using “,” for decimal operator. The first line of code would convert 12,50 to 1250.

That mean from your example Total = 1.156,08
Can java know how to convert 1156,08 or 1156.08

You have to strip thousand separator first what ever it is.

@emre I think we need your help please :blush:
There are two issues here, both with parameters from Printer Templates to JavaScript.

Can you please help enlighten us?

I tested it and it work. what is not working for you

function testNum(num){
 num=num.replace(",","");
 return Number(num);
}

You have to change whatever your thousand separator is, mine is comma. I think your is dot.
num=num.replace(".",""); should work. or oneline code

num=Number(num.replace(“.”,“”));

1 Like

Did you try {CALL:test.getblah (’[=blah]’,‘blah’)}

1 Like

As long as string contain open and close brackets. No error popup.

Im asking if it passes the string when you enter it like that. Notice the single quotes

In this case, try mixed quotes, like:

<L00>Test 3: {CALL:tests.getVerificationCode("[=TN('{TICKET TOTAL}')]", "abc")}

Do you mean locale? As in regional settings? So the Javascript Number() function does not work?

1 Like

It takes ‘[=blah]’ as an empty string and ‘blah’ as a string with value = blah. It is not a solution