Skip to main content

Code Node

Execute code to customize data processing. This node provides JavaScript programming capabilities to manipulate the input data freely.

Parameters

code

The code to execute and return a value using `return` keyword, similar to a function. The code has access to input data as `input` variable. The code is executed in a secure sandbox environment, so it cannot access external libraries or APIs.

Examples

Basic Data Processing

// Access input items:
const processedItems = items.map((item) => ({
...item,
fullName: item.firstName + " " + item.lastName,
isActive: item.status === "active",
}));

return processedItems;

Complex Data Processing

// Access input data:
// Transform input object into a formatted summary
const { firstName, lastName, age, email, address } = input;

return {
fullName: `${firstName} ${lastName}`,
contactInfo: {
email: email?.toLowerCase(),
displayEmail: email
? `${firstName.toLowerCase()}.${lastName.toLowerCase()}@example.com`
: null,
},
location: address
? {
city: address.city,
country: address.country,
formatted: `${address.city}, ${address.country}`,
}
: null,
ageGroup: age < 18 ? "minor" : age < 65 ? "adult" : "senior",
timestamp: new Date().toISOString(),
};

Complex Business Logic

// Complex scoring algorithm
function calculateScore(user) {
let score = 0;

// Activity score
if (user.lastLoginAt) {
const daysSinceLogin = (Date.now() - new Date(user.lastLoginAt)) / (1000 _ 60 _ 60 \* 24);
score += Math.max(0, 100 - daysSinceLogin);
}

// Engagement score
score += (user.actionsCount || 0) \* 2;

// Premium bonus
if (user.isPremium) {
score \*= 1.5;
}

return Math.round(score);
}

const scoredUsers = users.map(user => ({
...user,
score: calculateScore(user),
tier: calculateScore(user) > 150 ? "gold" :
calculateScore(user) > 50 ? "silver" : "bronze"
}));

return scoredUsers;

Data Validation and Filtering

// Comprehensive data validation
function validateUser(user) {
const errors = [];

if (!user.email || !user.email.includes("@")) {
errors.push("Invalid email");
}

if (!user.name || user.name.length < 2) {
errors.push("Name too short");
}

if (user.age && (user.age < 0 || user.age > 120)) {
errors.push("Invalid age");
}

return errors;
}

const validationResults = inputData.map((user) => {
const errors = validateUser(user);
return {
...user,
isValid: errors.length === 0,
validationErrors: errors,
};
});

// Return only valid users or all with validation status
return validationResults.filter((user) => user.isValid);