Search code examples
mirth

Mirth String Handling


I'm using the code below to try and strip the file extension off the incoming file and replace it with "ACK";

Can't use .lastIndexOf as it's not available in Rhino.

var _filename       = String(sourceMap.get('originalFilename'));
pos = -1;
var search = ".";

for(var i = 0; i < _filename.length - search.length; i++) {
    if (_filename.substr(i, search.length) == search) {
        pos = i;
    }
}
logger.info('_pos:' + _pos);

Every time I get a pos value of -1 i.e. Last full stop position not found.

BUT if I hardcode the filename in as "2020049.259317052.HC.P.F3M147-G" it works perfectly.

Is it something to do with the sourceMap.get('originalFilename') supplying a non-string or different character set ?


Solution

  • New Answer

    1. After reviewing and testing what agermano said he is correct.
    2. In your sample code you are setting pos = i but logging _pos
    3. New answer var newFilename = _filename.slice(0, _filename.lastIndexOf('.'))

    Older Answer

    First, you are mixing JavaScript types and Java types.

    var _filename       = String(sourceMap.get('originalFilename'));
    

    Instead, do

    var _filename       = '' + sourceMap.get('originalFilename');
    

    This will cause a type conversion from Java String to JS string.

    Secondly, there is an easier way to do what you are trying to do.

    var _filenameArr = ('' + sourceMap.get('originalFilename')).split('.');
    _filenameArr.pop()                     // throw away last item
    var _filename = _filenameArr.join('.') // rejoin the array with out the last item
    
    logger.info('_filename:' + _filename)