'use strict';
const xml = require('xml');
/**
* Check first argument. If true - push last argument to second argument
*
* @param condition
* @param array
* @param data
*/
function pushIfConditionTrue(condition, array, data) {
if (condition) {
array.push(data);
}
}
/**
* @param related
* @param itemValues
* @param relatedInfinity
*/
function addRelated(related, itemValues, relatedInfinity) {
let relatedResult = related.map(function (rel) {
return {
link: [{
_attr: {
'url': rel.link,
'img': rel.image_url
}
}, rel.text]
};
});
if (relatedInfinity) {
relatedResult.push({
_attr: {'type': 'infinity'}
});
}
pushIfConditionTrue(related, itemValues, {'yandex:related': relatedResult});
}
/**
* @param item
* @param itemValues
*/
function pushGoals(item, itemValues) {
if (item.goals.length > 0) {
item.goals.forEach(goal => itemValues.push({
'turbo:goal': {
_attr: {
type: goal.type || 'yandex',
'turbo-goal-id': goal.id,
name: goal.name,
id: goal.counter_id,
}
}
}));
}
}
/**
* Items processing
* @param items
* @param channel
*/
function items(items, channel) {
items.forEach(function (item) {
let itemValues = [];
itemValues.push({_attr: {'turbo': item.turboEnabled ? 'true' : 'false'}});
itemValues.push({link: item.url});
pushIfConditionTrue(item.turboSource, itemValues, {'turbo:source': item.turboSource});
pushIfConditionTrue(item.turboTopic, itemValues, {'turbo:topic': item.turboTopic});
pushIfConditionTrue(item.date, itemValues, {pubDate: new Date(item.date).toUTCString()});
pushIfConditionTrue(item.author, itemValues, {author: item.author});
let img = '', menu = '';
if (item.image_url) {
img = '