Monday, April 1, 2013

QT008: Quick References for Mapping with JavaScript

Quick Tip #008: Quick References for Mapping with JavaScript

JavaScript is a simple scripting language that uses a syntax that is derived from the Java Programming Language.  Because of its simplicity and pervasiveness, JavaScript is commonly used for mapping data in middleware platforms such as IBM Cast Iron and Boomi.  In this post we will run through a quick example in CIOS provide a couple of resources from around the web for more in depth study of JavaScript.

A Quick Example: Parsing Email Addresses

In this example, we will present a brief example for parsing the first email address out of a given string in Cast Iron.  Cast Iron provides a number of built in custom functions for performing numerous tasks.  These functions are available in the functions tab and can be dragged into any map to translate inputs from the source side to outputs on the target side.  If there is no suitable built in function you may write a custom function using JavaScript.  To do so, click the Add New Custom Function . . . link at the bottom of the functions tab.


There are two tabs, the first allows you to configure the name and return type as well as the input parameters for your custom JavaScript function.  For this example the return type will be a string and we only require one string input, which we will call input.  Click next to define the body of your custom function.


In Cast Iron, you define the body of a custom JavaScript function.  You can pass an arbitrary number of string, boolean, or number parameters to your function, but you may only return a single value.  Because of the nature of JavaScript functions, it is actually possible to nest functions within your function should you need to get creative.  See some of the tutorials discussed below for other advanced features of the JavaScript language.  In this example we will use the string function match() to apply a regular expression to the input string.  The match function applies a regular expression to a string and returns an array of all matches to the regular expression.  We create a variable to store this array and test to see if it contains at least one value.  If it does we return the first match, otherwise we return null.  It is as simple as that.  You may use your custom function in any map as you would use any of the built in functions.

References

Mozilla - The source for JavaScript, JavaScript was originally included in the Netscape Browser which became the the Mozilla Project.  Mozilla is the authoritative source for JavaScript and contains numerous resources including documentation and tutorials.
W3 Schools - W3 Schools provides tools and documentation for web technologies such as CSS, HTML, and of course JavaScript.
regular-expressions.info - One of the best guides on the web for working with and learning about regular expressions.

No comments:

Post a Comment