JS works but not in transform?!

I wrote a function which formats phone numbers; if I take off the transform script the number appears on the sitemap (unformatted as expected; with the transform it is empty):

(function(phoneNumber){
  // 170305 MaxG: format phone numbers
  var result;

  if (phoneNumber.substr(0, 1) == "0") {
    
    phonePrefix = phoneNumber.substr(1, 1);

    switch (phonePrefix) {
      case "4":
        result = phoneNumber.replace(/((\d{4})(\d{3})(\d{3}))/g, '$2 $3 $4');
        break;
        
      default:
        result = phoneNumber.replace(/((\d{2})(\d{4})(\d{4}))/g, '$2 $3 $4');
    }
  } else {
    if (phoneNumber.substr(0, 1) == "1") {
      phoneNumberLength = phoneNumber.length;
      
      switch (phoneNumberLength) {
        // (6) 13 13 13; (10) 1300 123 456; 1800 123 456
        case 6:
          result = phoneNumber.replace(/((\d{2})(\d{2})(\d{2}))/g, '$2 $3 $4');
          break;
      
        case 10:
          result = phoneNumber.replace(/((\d{4})(\d{3})(\d{3}))/g, '$2 $3 $4');
          break;
      
        default:
          result = phoneNumber;
      }
    }
  }
  return result
})(input)

called in items like so:

String  fBox_LastCallerNumber		"Last caller's number [JS(fb_phoneNumber.js):%s]"	<phone>

I tested this as JavaScript (the same as above without the OH wrapper) which works:

// test code; works
number ="0411511332";
number ="0754286595";
number ="1300511002";
number ="131213";

function phoneNumber(number){
  // 170305 MaxG: format phone numbers
  var result;

  if (phoneNumber.substr(0, 1) == "0") {
    
    phonePrefix = phoneNumber.substr(1, 1);

    switch (phonePrefix) {
      case "4":
        result = phoneNumber.replace(/((\d{4})(\d{3})(\d{3}))/g, '$2 $3 $4');
        break;
        
      default:
        result = phoneNumber.replace(/((\d{2})(\d{4})(\d{4}))/g, '$2 $3 $4');
    }
  } else {
    if (phoneNumber.substr(0, 1) == "1") {
      phoneNumberLength = phoneNumber.length;
      
      switch (phoneNumberLength) {
        // (6) 13 13 13; (10) 1300 123 456; 1800 123 456
        case 6:
          result = phoneNumber.replace(/((\d{2})(\d{2})(\d{2}))/g, '$2 $3 $4');
          break;
      
        case 10:
          result = phoneNumber.replace(/((\d{4})(\d{3})(\d{3}))/g, '$2 $3 $4');
          break;
      
        default:
          result = phoneNumber;
      }
    }
  }
  return result
}

I can’t find any mistake; any hints appreciated.

the log shows:

2017-07-20 13:04:37.181 [INFO ] [o.o.b.f.i.FritzboxTr064Binding] - found name match Max in phonebook by comparing 2001151140 with 2001151
2017-07-20 13:04:37.232 [INFO ] [.model.script.fbLastCall1.rule] - Call coming in...
2017-07-20 13:04:37.254 [INFO ] [.model.script.fbLastCall2.rule] - tmpLastCallerNumber..:
2017-07-20 13:04:37.271 [INFO ] [.model.script.fbLastCall3.rule] - tmpName..............: Max (Mobile)
2017-07-20 13:04:37.276 [INFO ] [.model.script.fbLastCall4.rule] - tmpNumber............:
2017-07-20 13:04:37.298 [INFO ] [.model.script.fbLastCall5.rule] - fBox_LastCallerName..: fBox_LastCallerName (Type=StringItem, State=Max (Mobile))
2017-07-20 13:04:37.309 [INFO ] [.model.script.fbLastCall6.rule] - fBox_LastCallerNumber: fBox_LastCallerNumber (Type=StringItem, State=)
2017-07-20 13:04:42.284 [INFO ] [o.o.b.f.i.FritzboxTr064Binding] - Trying to resolve number 0411511332 to name comparing 7 characters

the rule going with it:

rule "Fritzbox: Incoming call, get number and name from phonebook"
when
  Item fbox_Ringing changed from OFF to ON
then
  logInfo("fbLastCall1.rule", "Call coming in...")

  // get incoming call data (array)
  val CallType InCall            = fbox_IncomingCall.state as CallType
  val String tmpLastCallerNumber = InCall.destNum.toString()
  var String tmpLastCallerName   = fbox_IncomingCallResolved.state.toString()
  
  logInfo("fbLastCall2.rule", "tmpLastCallerNumber..: {}", tmpLastCallerNumber)

  // remove the called (own) number from caller string
  tmpLastCallerName = tmpLastCallerName.removeStart("0754286585##")

  if(tmpLastCallerName.startsWith("Name not found for")) {
    tmpLastCallerName = "Unknown"
  }

  logInfo("fbLastCall3.rule", "tmpName..............: {}", tmpLastCallerName)
  logInfo("fbLastCall4.rule", "tmpNumber............: {}", tmpLastCallerNumber)

  // update name and number
  fBox_LastCallerName.postUpdate(tmpLastCallerName)
  fBox_LastCallerNumber.postUpdate(tmpLastCallerNumber)

  logInfo("fbLastCall5.rule", "fBox_LastCallerName..: {}", fBox_LastCallerName)
  logInfo("fbLastCall6.rule", "fBox_LastCallerNumber: {}", fBox_LastCallerNumber)
end

I’m far from an expert in the JS transform. I don’t see anything that jumps out at me as obvious.

Does it work if you call it from a Rule?

val num = transform("JS", "fb_phoneNumber.js", fBox_LastCallerNumber.state.toString)

There could be a bug when using it in the label. If not, perhaps seeing what it is returning or doing from within a rule could be informative.

1 Like

Alright… added your line to the rule… this is what happened:

2017-07-25 12:44:16.880 [INFO ] [o.o.b.f.i.FritzboxTr064Binding] - Trying to resolve number 0292004444 to name comparing 7 characters
2017-07-25 12:44:16.880 [INFO ] [o.o.b.f.i.FritzboxTr064Binding] - Trying to resolve number 0292004444 to name comparing 7 characters
2017-07-25 12:44:16.885 [INFO ] [o.o.b.f.i.FritzboxTr064Binding] - found name match Freda Chung - Clicks IT in phonebook by comparing 4044002920 with 4044002
2017-07-25 12:44:17.444 [INFO ] [.model.script.fbLastCall1.rule] - Call coming in...
2017-07-25 12:44:21.225 [INFO ] [.model.script.fbLastCall2.rule] - tmpLastCallerNumber..: 0292004444
2017-07-25 12:44:23.001 [INFO ] [.model.script.fbLastCall3.rule] - tmpName..............: Freda Chung - Clicks IT (Work)
2017-07-25 12:44:23.836 [INFO ] [.model.script.fbLastCall4.rule] - tmpNumber............: 0292004444
2017-07-25 12:44:26.173 [INFO ] [.model.script.fbLastCall5.rule] - fBox_LastCallerName..: fBox_LastCallerName (Type=StringItem, State=Freda Chung - Clicks IT (Work))
2017-07-25 12:44:26.961 [INFO ] [.model.script.fbLastCall6.rule] - fBox_LastCallerNumber: fBox_LastCallerNumber (Type=StringItem, State=0292004444)
2017-07-25 12:44:27.586 [INFO ] [.model.script.fbLastCall7.rule] - num..................: null

2017-07-25 13:03:49.970 [INFO ] [o.o.b.f.i.FritzboxTr064Binding] - Trying to resolve number 0292004444 to name comparing 7 characters
2017-07-25 13:03:49.970 [INFO ] [o.o.b.f.i.FritzboxTr064Binding] - Trying to resolve number 0292004444 to name comparing 7 characters
2017-07-25 13:03:49.976 [INFO ] [o.o.b.f.i.FritzboxTr064Binding] - found name match Freda Chung - Clicks IT in phonebook by comparing 4444002920 with 4044002
2017-07-25 13:03:55.529 [INFO ] [o.o.b.f.i.FritzboxTr064Binding] - Trying to resolve number 0292004444 to name comparing 7 characters
2017-07-25 13:03:55.529 [INFO ] [o.o.b.f.i.FritzboxTr064Binding] - Trying to resolve number 0292004444 to name comparing 7 characters
2017-07-25 13:03:55.533 [INFO ] [o.o.b.f.i.FritzboxTr064Binding] - found name match Freda Chung - Clicks IT in phonebook by comparing 4444002920 with 4044002

2017-07-25 13:15:45.557 [INFO ] [o.o.b.f.i.FritzboxTr064Binding] - Trying to resolve number 0754264884 to name comparing 7 characters
2017-07-25 13:15:45.557 [INFO ] [o.o.b.f.i.FritzboxTr064Binding] - Trying to resolve number 0754264884 to name comparing 7 characters
2017-07-25 13:15:45.563 [INFO ] [o.o.b.f.i.FritzboxTr064Binding] - found name match Ian Smith in phonebook by comparing 4884624570 with 4884624
2017-07-25 13:15:45.564 [INFO ] [o.o.b.f.i.FritzboxTr064Binding] - found name match Ian Smith in phonebook by comparing 4884624570 with 4884624
2017-07-25 13:15:45.609 [INFO ] [.model.script.fbLastCall1.rule] - Call coming in...
2017-07-25 13:15:46.478 [INFO ] [.model.script.fbLastCall2.rule] - tmpLastCallerNumber..:
2017-07-25 13:15:46.502 [INFO ] [.model.script.fbLastCall3.rule] - tmpName..............: Ian Smith (Home)
2017-07-25 13:15:46.511 [INFO ] [.model.script.fbLastCall4.rule] - tmpNumber............:
2017-07-25 13:15:46.537 [INFO ] [.model.script.fbLastCall5.rule] - fBox_LastCallerName..: fBox_LastCallerName (Type=StringItem, State=Ian Smith (Home))
2017-07-25 13:15:46.551 [INFO ] [.model.script.fbLastCall6.rule] - fBox_LastCallerNumber: fBox_LastCallerNumber (Type=StringItem, State=)
2017-07-25 13:15:46.557 [INFO ] [.model.script.fbLastCall7.rule] - num..................: 02 9200 4444
2017-07-25 13:15:51.186 [INFO ] [o.o.b.f.i.FritzboxTr064Binding] - Trying to resolve number 0754264884 to name comparing 7 characters
2017-07-25 13:15:51.186 [INFO ] [o.o.b.f.i.FritzboxTr064Binding] - Trying to resolve number 0754264884 to name comparing 7 characters
2017-07-25 13:15:51.190 [INFO ] [o.o.b.f.i.FritzboxTr064Binding] - found name match Ian Smith in phonebook by comparing 4884624570 with 4884624
2017-07-25 13:15:51.190 [INFO ] [o.o.b.f.i.FritzboxTr064Binding] - found name match Ian Smith in phonebook by comparing 4884624570 with 4884624

num is NULL after the first call; however, in the second call it has the number of the first call – and is formatted correctly, so the JS works.

weird…

At this point all I can recommend is filling a big report. If it works from the rule it should work from the item.