web stats
Bad result on length property in Javascript - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 04-10-2018, 07:12 AM
rkMC rkMC is offline
OBX.1 Kenobi
 
Join Date: May 2017
Posts: 48
rkMC is on a distinguished road
Question Bad result on length property in Javascript

Mirth Connect 3.5.1
Java 8.161

I am processing HL7 and attempting to handle different length date fields within a function. I pass the date into the function as a string field (dateString).

If the date is YYYYMMDDhhmmss, then the property of dateString.length = 14.
But, if the date is YYYYMMDDhhmm and is missing the seconds, like on an admission or discharge date, then I get a peculiar result from the dateString.length property

This is my logger.error debug line:
[2018-04-10 04:23:29,116] ERROR (transformer:?): 201803081225 length of date = function length() {/*int length()*/}

Does anyone know why I'm not getting the value of 12 from the length property? Without getting a legitimate value from the string length, my function does not work as designed.

Any suggestions would be greatly appreciated.
Attached Files
File Type: xml Format Dates for JSON.xml (2.7 KB, 2 views)
Reply With Quote
  #2  
Old 04-10-2018, 09:28 AM
odo odo is online now
OBX.3 Kenobi
 
Join Date: Feb 2017
Location: Luxembourg
Posts: 149
odo is on a distinguished road
Default

Quote:
Originally Posted by rkMC View Post
[2018-04-10 04:23:29,116] ERROR (transformer:?): 201803081225 length of date = function length() {/*int length()*/}

Does anyone know why I'm not getting the value of 12 from the length property?
It's a Java string. Using .length() instead of .length should do the trick.
Reply With Quote
  #3  
Old 04-10-2018, 10:27 AM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 1,058
agermano is on a distinguished road
Default

On java strings length is a function, so you need parentheses. On javascript strings length is a property. In code templates I will often force strings to one or the other so that I know what I'm dealing with.

Java has a lot tools for working with dates, and Mirth provides the DateUtil class in the User API to make it really easy.

I shortened your function and tried to comment what I changed. You could shorten it even further by changing option to a boolean instead of a string.

Code:
/**
	Function is meant to accept an option and an HL7 date string and reformat into the JSON format
	N - Normal "yyyy-MM-dd HH:mm:ss.0"
	L - Letters "yyyy-MM-ddTHH:mm:ssZ"

	@param {String} option - L to add letters, N for normal JSON format
	@param {String} dateString - date to be reformatted
	@return {String} date to be returned in one of two formats
*/
function formatDateforJSON(option, dateString) {

	// Validate the arguments or issue errors
	// convert option to javascript string (switch statements don't like java strings)
	var normal;
	switch (String(option)) {
		case 'N':
			normal = true;
			break;
		case 'L':
			normal = false;
			break;
		default:
			// no need to wrap in try/catch if you intend to throw the error anyway
			throw new Error('Invalid Option given. Must be L or N.');
	}
	
	// Normalize dateString to length of 14
	// The string concatenation also converts to a javascript string if dateString is a java.lang.String
	// Reduce the number of zeroes if you want to set a minimum length for dateString
	// DateUtil.convertDate will throw an exception if dateString is too short.
	// i.e. add 6 zeroes instead of 14 if you want to require 8 characters for the date
	dateString = (dateString + '00000000000000').slice(0,14);

	var inpattern = 'yyyyMMddHHmmss';
	var outpattern = normal ? 'yyyy-MM-dd HH:mm:ss.S' : "yyyy-MM-dd'T'HH:mm:ss'Z'";

	// DateUtil returns a java.lang.String. Return a javascript string instead
	return String(DateUtil.convertDate(inpattern, outpattern, dateString));
}
Reply With Quote
  #4  
Old 04-10-2018, 12:00 PM
siddharth siddharth is offline
Mirth Guru
 
Join Date: Feb 2013
Posts: 835
siddharth is on a distinguished road
Default

One question though, how do you know when its a java string and when its javascript? when a date field is drag-and-dropped in the .toString() form on this function, shouldn't it be already a javascript string?
__________________
HL7v2.7 Certified Control Specialist!
Reply With Quote
  #5  
Old 04-10-2018, 12:46 PM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 1,058
agermano is on a distinguished road
Default

Yeah, anything you drag-and-drop from the inbound message template should be a javascript string.

If you call a Java function you have added as a resource or through the User API you'll likely get a Java string.

Strings you put into a mapper variable also get converted to Java strings, so if you're using $('dateString') it's probably a Java string.

Like I said, it's easiest to just force it to be whatever you want to work with in the code template, and then it doesn't matter how you called it.

Code:
function convertString(ambiguousString) {
  var javaString = new java.lang.String(ambiguousString);
  var jsString = String(ambiguousString);
  var jsString2 = 'javascript string literal' + ambiguousString;
}
Reply With Quote
  #6  
Old 04-11-2018, 11:58 AM
rkMC rkMC is offline
OBX.1 Kenobi
 
Join Date: May 2017
Posts: 48
rkMC is on a distinguished road
Default

Thanks agermano.

This code was a great help.
Reply With Quote
Reply

Tags
length

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 05:25 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2020, vBulletin Solutions, Inc.
Mirth Corporation