The JavaScript transformer allows the user to define his/her own script which can perform rather intricate things like conditioning, looping. It can also be used as a way to express small business rules.
For this documentation, a complete reference of JavaScript is out of scope. But we will show a few examples and more importantly talk about the available variables and their types.
The JavaScript transformer returns a single string. The entered script should provide this string as the last line of the script. This is why the template script is as follows (so you can just implement the eval() function):
function eval() { return \"hello \" + values[0]; } eval();
Variables:
Table 3.1. JavaScript variables
Variable | Description |
---|---|
values |
An array of all values in the row (as mapped by the "Columns" property). Using "values" you can reference eg. the first and third values like this: var first = values[0]; var third = values[2]; Note that JavaScript arrays are 0-based. Instead of indexes you can also reference by column name, like this: var idValue = values["id"]; |
column_name * |
Any column name that is also a valid JavaScript and not a reserved variable name will also be added directly to the scope of the script as a variable. For example, if you have two columns, FIRST_NAME and LAST_NAME, you can concatenate them easily, like this: var fullname = FIRST_NAME + " " + LAST_NAME; |
out |
A reference to the system console's "out" stream. If running DataCleaner with the console visible, you can print messages to the console, like this: out.println("Value: " + values[0]); |
log |
A reference to the logging subsystem. Logging can be configured and log messages are stored in files, which makes it a bit more flexible than simply using "out". Here's how you write a few log messages with varying severities: log.debug("This is a DEBUG message, it will probably be disregarded"); log.info("This is a INFO message, it will probably be written to the logs"); log.warn("This is a WARN message, it will most likely be written to the logs"); log.error("This is a ERROR message, it will almost certainly be written to the logs"); |
Data types:
Table 3.2. JavaScript data types
Data type | Description |
---|---|
STRING |
String values are represented as JavaScript strings, which means that they have (among others) methods like: var str = values[0]; // get the length of a string var len = str.length(); // uppercase variant of a string var up = str.toUpperCase(); // lowercase variant of a string var lw = str.toLowerCase(); For more information, we recommend W3 schools JavaScript string reference . |
NUMBER |
Numbers are treated as regular JavaScript numbers, which means that they have (among others) methods and operators like: var num = values[0]; // format with 2 decimals var formattedNumber = num.toFixed(2); // add, subtract, multiply or divide var m = (4 + num * 2 - 1) / 2; For more information, we recommend W3 schools JavaScript number reference and also check out the Math function reference. |
DATE |
Date values are treated as Java dates, which is a bit unusual, but leaves you with almost an identical interface as a regular JavaScript date. Here's a summary of typical methods: var d = values[0]; var year = d.getYear(); var month = d.getMonth(); var date = d.getDate(); var hour = d.getHour(); var minutes = d.getMinutes(); var seconds = d.getSeconds(); // milliseconds since 1970-01-01 var timestamp = d.getTime(); For a full reference, please look at the Java Date class reference . |
BOOLEAN | Boolean (true/false) values are simply booleans, no sugar coating added :) |