Global Functions

Feeder offers built-in functions to convert Java objects into JavaScript objects. In this way, true comparisons can be performed.

Moreover, Feeder offers other functions for scripting comfort.


toBool(val)

The toBool(val) function transforms a value into a real JavaScript boolean. For example in the following excerpt of an import script.

var is_mgr = toBool(obj.get('IS_MANAGER'));

if(is_mgr){
        // do something
}

toDate(val)

(as of Feeder version 1.9)

The toDate(val) function transforms a value into a real JavaScript date. Commonly, it is used for configuring Rule-based Value Changes In Future Time Slice.

var tDate = toDate(obj.get('TerminationDate'));

toJavaDate(val)

The toJavaDate(val) function converts a JavaScript Date into a Java Date. If a field with Date data-type shall be set by calculation, this function must be used.

Assuming the following configured attributes(data-type): Date_Of_Birth (Date), Calculation (Text), TEXT_FIELD (Text), DATE_FIELD (Date). The following scriptlet is part of the “Calculation” configuration.

var dobString = obj.get('Date_Of_Birth');
var dobDate = toJavaDate(obj.get('Date_Of_Birth'));

obj.set('TEXT_FIELD', dobString);
obj.set('DATE_FIELD', dobDate);

toNum(val)

The toNum(val) function converts a Java Number into a JavaScript Number.

var n1, n2, result1, result2;

n1 = obj.get('NUMBER_FIELD');           /* 50 */
n2 = obj.get('NUMBER_FIELD_2');         /*  7 */
result1 = n1 + n2;                      /* 507 */
result2 = toNum(n1) + toNum(n2);        /* 57 */

toProperCase()

The toProperCase(val) function ensures uniform Strings starting with a capital letter. For example it can be applied at import. The code excerpt below effectuates that all imported first names are formatted properly, e.g. ‘COBY-lou’ becomes Coby-Lou’.

var i, name, col;
for (i = 0; i < headers.length; i++) {
    name = toStr(headers[i]);
    col = obj.get(name);
    switch(name){
        case 'mail' :
        obj.set('EMAIL', col.toLowerCase());
        break;
    case 'forename':
        obj.set('FIRSTNAME', col.toProperCase());
        break;
    //...
    default:
        columns[i] = col;
    }
 }

toStr(val)

The toStr(val) function converts a value to a real Javscript String in order to feature real string comparisons. Moreover, it abbreviates the cumbersome writing name = headers[i] + ‘’; when scripting imports or exports.

var checkStr = 'red';
var testStr  = 'blue';

if(checkStr == testStr){
        return "testStr and checkStr are the same color. It is red!";
}
return "The color is different from red";
var i, name, col;
for (i = 0; i < headers.length; i++) {
    name = toStr(headers[i]);
    col = obj.get(name);
    switch(name){
        //...
    }
 }

RegExp.escape(val)

The RegExp.escape(val) function is a custom functionality of Pentos Feeder to easily escape special characters like dots, asterisks or question marks. These characters then conform with requirements in order to be usable for pattern matching, which provides a flexible way to find and change desired values.

The following code demonstrates an advanced calculation of a unique USERNAME attribute. The code will calculate a new USERNAME for an employee, if parts of the first and last name have changed.

  • If the “current”-variable contains hmueller1 and the recent username was hmueller, the USERNAME will not be updated.
  • Whereas a change of last name due to marriage will update the USERNAME, e.g. to hsinger, as the patterns in “current” and “uname” deviate or do not match.
var fn = toStr(obj.get('FIRSTNAME'));
if (!fn) {
        return null;
}
var ln = toStr(obj.get('LASTNAME'));
if (fn && ln) {
        var uname = fn + ln;
        uname = uname.toLowerCase().replace(/ö/g, 'oe').replace(/ä/g, 'ae').replace(/ü/g, 'ue').replace(/[^0-9a-z\.-]/g, '');
        var current = obj.get('USERNAME');
        // The character ^ matches the beginning of a string
        // The rear part d{0,2} of the regular expression will ignore digits in the recent username
        if (current && current.match(new RegExp('^' + RegExp.escape(uname) + '\\d{0,2}$'))) {
                return current;
        }
        return Helper.ensureUnique(obj, 'USERNAME', value, '');
}
return null;