PDA

View Full Version : Array filter function


mcmahand
December 6th, 2011, 06:13 AM
In a rule I'm working on (code below), I tried to make use of a filter function on an array. I believe it is only available in newer versions of JavaScript.

When I validate from the rule editor, results are returned as expected. However, previewing a page with the text rule in use shows the name of function in braces instead of the result. And the code errors out when composing ("Rule_CSZ, line 9: TypeError: csz.filter is not a function").


//Rule_CSZ
//Builds City/State/Zip address line
//Places comma & space between City & State when both have data
//Otherwise adds spaces between fields with data

var csz = [Field("City"), Field("St")];

csz = [csz.filter(function(val) { return val !== ""; }).join(", "), Field("ZIP+4")];

return csz.filter(function(val) { return val !== ""; }).join(" ");


Is the filter function not really available through FP? Or am I not properly building the array? Any thoughts would be appreciated.

Thanks,

Dan Korn
December 6th, 2011, 11:09 AM
The results are different because there are actually different versions of JavaScript used at rule validation and composition times on Mac. Specifically, the Acrobat plug-in on Mac uses the JavaScript 1.7 engine, while the FusionPro composition app on Mac, and all versions of FusionPro on Windows (Desktop and Server) use the JavaScript 1.5 engine. This discrepancy is the result of changes to the architecture of the Acrobat plug-in on Mac to support Intel processors. (You're actually one of a very few users to notice this!)

The upshot is that, while the rule editor on Mac will let you use some newer JavaScript features, you need to stick with JavaScript 1.5 features for composition.

However, there is a way to make the rule work as written, for composition on either Windows or Mac. The JavaScript reference page for the Array.filter function (https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter) has this compatibility code, which you can paste into your JavaScript Globals:
if (!Array.prototype.filter)
{
Array.prototype.filter = function(fun /*, thisp */)
{
"use strict";

if (this === void 0 || this === null)
throw new TypeError();

var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();

var res = [];
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in t)
{
var val = t[i]; // in case fun mutates this
if (fun.call(thisp, val, i, t))
res.push(val);
}
}

return res;
};
}

Dan Korn
January 18th, 2012, 08:10 AM
The results are different because there are actually different versions of JavaScript used at rule validation and composition times on Mac. Specifically, the Acrobat plug-in on Mac uses the JavaScript 1.7 engine, while the FusionPro composition app on Mac, and all versions of FusionPro on Windows (Desktop and Server) use the JavaScript 1.5 engine. This discrepancy is the result of changes to the architecture of the Acrobat plug-in on Mac to support Intel processors. (You're actually one of a very few users to notice this!)

The upshot is that, while the rule editor on Mac will let you use some newer JavaScript features, you need to stick with JavaScript 1.5 features for composition.
FusionPro 8.0 will incorporate the JavaScript 1.7 engine on all platforms, which will make Array extra functions (https://developer.mozilla.org/en/JavaScript/New_in_JavaScript/1.6#Array_extras) such as Array.filter(), as well as other enhancements in JavaScript 1.6 (https://developer.mozilla.org/en/JavaScript/New_in_JavaScript/1.6) and JavaScript 1.7 (https://developer.mozilla.org/en/JavaScript/New_in_JavaScript/1.7), available by default in all rules.