ECMAScript Examples

From CoolSolutionsWiki

Contents

toTitleCase

The function toTitleCase facilitates the transformation of the Job Titles to Title Case. It caters for the situation where brackets could be used within the Job Title and also caters for key acronyms (the array "acronyms") which should be fully capitalised.

function toTitleCase(titleInput)
	{
    var wholeTitle = String(titleInput);
	var splitTitle = new Array();
	splitTitle = wholeTitle.split(' ');
	wholeTitle = '';
	
	var acronyms = new Array("it");

	for (count = 0; count < splitTitle.length; count++)
		{
		acronymFlag = 0;
		splitTitle[count] = splitTitle[count].toLowerCase();
		
		for (acount = 0; acount < acronyms.length; acount++)
			{
			if (splitTitle[count] == acronyms[acount])
				{
				acronymFlag = 1;
				}
			}
		
		if (acronymFlag == 0)
			{
			punctuationIndex=splitTitle[count].search(/[\(\[\{]/);
			if (punctuationIndex == 0)
				{
				splitTitle[count] = splitTitle[count].substr(0,1) + splitTitle[count].substr(1,1).toUpperCase() + splitTitle[count].substr(2);
				}
			else
				{
				splitTitle[count] = splitTitle[count].substr(0,1).toUpperCase() + splitTitle[count].substr(1);
				}
			}
		else
			{
			splitTitle[count] = splitTitle[count].toUpperCase();
			}
		wholeTitle = wholeTitle + splitTitle[count] + " ";
		}
	wholeTitle = wholeTitle.substr(0,wholeTitle.length - 1);

	return wholeTitle;
	}

nameCapitalise

The function nameCapitalise facilitates the transformation of the names to Title Case, but leaves key exceptions (the array "exceptions") in lower case. This was specifically to cater for names like "Johannes van Rooyen". It also caters for capitalising names like "S'Bu" and "Amon-Ra" properly. Unfortunately it does not (yet) cater for capitalising names like "McDonald" or "o'Shaunessy" properly.

function nameCapitalise(name)
	{
	var wholeName = String(name);
	var splitName = new Array();
	splitName = wholeName.split(' ');
	wholeName = '';
	
	var exceptions = new Array("van","der","du","de");
	var special = new Array("'","-");
	
	for (count = 0; count < splitName.length; count++)
		{
		exceptionFlag = 0;
		splitName[count] = splitName[count].toLowerCase();
		
		for (ecount = 0; ecount < exceptions.length; ecount++)
			{
			if (splitName[count] == exceptions[ecount])
				{
				exceptionFlag = 1;
				}
			}
		
		if (exceptionFlag == 0)
			{
			splitName[count] = splitName[count].substr(0,1).toUpperCase() + splitName[count].substr(1);
			}
		
		for (scount = 0; scount < special.length; scount++)
			{
			var specialName = new Array();
			specialIndex=splitName[count].search(special[scount]);
			if (specialIndex > -1)
				{
				var specialName = new Array();
				specialName = splitName[count].split(special[scount]);
				splitName[count] = '';
				for (sncount = 0; sncount < specialName.length; sncount++)
					{
					specialName[sncount] = specialName[sncount].substr(0,1).toUpperCase() + specialName[sncount].substr(1);
					splitName[count] = splitName[count] + specialName[sncount] + special[scount];
					}
				splitName[count] = splitName[count].substr(0,splitName[count].length - 1);
				}
			}
		
		wholeName = wholeName + splitName[count] + " ";
		}
	wholeName = wholeName.substr(0,wholeName.length - 1);
	return wholeName;
	}

Parse and Serialize Nodesets

Below is the example code to parse from XML in a String into a Nodeset. And to Serialize from a Nodeset into a XML String.

function parse(String)
{
	var nodeSet = new Packages.com.novell.xml.xpath.NodeSet();
	var factory = Packages.com.novell.xml.parser.XMLParserFactory.newParser();
	var stringreader = Packages.java.io.StringReader;
	var strinput = new stringreader(InString);
	var doc = factory.parse(strinput);
	nodeSet.add(doc);
	return nodeSet;
}

function serialize(nodeSet)
{
	var stringWriter = new java.io.StringWriter();
	for (var node = nodeSet.first(); node; node=nodeSet.next())
	{
		var domWriter = new Packages.com.novell.xml.dom.DOMWriter(node, stringWriter);
		domWriter.setIndent(true);
		domWriter.write();
		domWriter.flush();
		stringWriter.write('\n');
	}
	return stringWriter.toString();
}

Other Resources

There is a Cool Solutions article listing a number of other ECMA functions of use in Identity Manager installations.

Open Call for IDM ECMA functions