Data Factory > code-execute-javascript β
TL;DR;
This task allows executing Javascript code to produce a result based on the given input parameters.
Name:
Max execution time: 20 seconds
TIP
The full documentation of the behavior of this task is detailed in the Learning Center.
Technical details β
- This task (or sub-workflow) use the conductor
INLINE_TASKto execute the Javascript code. - The evaluator used is the
Nashornengine. - This engine has some limitation (see below)
js
// In this example, our objective is to concatenate every rule object in the loopOutput object
function f() {
var loopOutput = $.data.loopOutput;
// The main issue is the way Nashorn deals with arrays
// the declaration of an array using `[]` will lead to the creation of a Map object when returned as a result
// Example:
// var data = [];
// data.push('a');
// data.push('b');
// return data;
// Will return
// {0: 'a', 1: 'b'}
// The only slution (that we found) is to use Java ArrayList
var ArrayList = Java.type('java.util.ArrayList');
var data = new ArrayList();
for (var key in loopOutput) {
var value = loopOutput[key];
if (typeof value === 'object') {
if (typeof value['parse_openai_response'] === 'object') {
if (value['parse_openai_response'].result.rules) {
var openaiResponse =
value['parse_openai_response'].result.rules;
for (var i = 0; i < openaiResponse.length; i++) {
data.add(openaiResponse[i]);
}
}
}
}
}
return { rules: data };
}
f();1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
- The code below allows to turn a js file into a string (as it's needed for the
expressionparameter)
js
const fs = require('fs');
process.argv.shift(); // skip node.exe
process.argv.shift(); // skip name of js file
const filePath = process.argv[0];
if (!filePath) {
console.error('No file path provided');
process.exit(1);
}
const destPath = filePath.split('.').slice(0, -1).join('.') + '.min.txt';
console.log(`Reading file: ${filePath}`);
try {
const data = fs.readFileSync(filePath, 'utf8');
// Remove end of line & comments
const result = data
.replace(/\/\/.*/g, '')
.replace(/\n/g, '')
// Remove multiple spaces
.replace(/\s+/g, ' ')
// Esccape \;
.replace(/\\n/g, '\\\\n')
// Escape double quotes
.replace(/"/g, '\\"');
fs.writeFileSync(destPath, result, 'utf8');
} catch (err) {
console.error(err);
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30