traxxx-web/utils/process-summary-template.js

99 lines
2.9 KiB
JavaScript

import { format } from 'date-fns';
import { parse } from 'yaml';
import slugify from '#/utils/slugify.js';
import ellipsis from '#/utils/ellipsis.js';
const genderMap = {
f: 'female',
m: 'male',
t: 'transsexual',
o: 'other',
u: null,
};
const propProcessors = {
channel: (sceneInfo) => sceneInfo.channel?.name || sceneInfo.network?.name,
network: (sceneInfo) => sceneInfo.network?.name || sceneInfo.channel?.name,
actors: (sceneInfo, options) => {
const genders = (options.genders || 'fmtou').split('').map((genderKey) => genderMap[genderKey]);
return sceneInfo.actors
.filter((actor) => genders.includes(actor.gender))
.map((actor) => actor.name);
},
tags: (sceneInfo, options) => sceneInfo.tags
?.filter((tag) => {
if (options.include && !options.include.includes(tag.slug)) {
return false;
}
if (options.exclude?.includes(tag.slug)) {
return false;
}
if (options.priority && tag.priority < options.priority) {
return false;
}
return true;
})
.map((tag) => tag.name),
movie: (sceneInfo) => sceneInfo.movies?.[0]?.title,
date: (sceneInfo, options) => (sceneInfo.effectiveDate ? format(sceneInfo.effectiveDate, options.format || 'yyyy-MM-dd') : ''),
};
function curateValue(value, item) {
return [].concat(value) // account for both arrays (actors, tags) and strings (title, channel)
.slice(0, item.limit || Infinity)
.map((listValue) => (item.slugify ? slugify(listValue, item.slugify) : listValue))
.map((listValue) => ellipsis(listValue, item.slice || Infinity, item.ellipsis || ''))
.map((listValue) => `${item.wrap?.[0] || ''}${listValue}${item.wrap?.[1] || ''}`)
.join(item.delimit || ', ');
}
function traverseTemplate(chain, release, { delimit = ' ' } = {}) {
const results = chain.reduce((result, item) => {
const keys = typeof item === 'string' ? item : item.key;
if ((item.channels && !item.channels.includes(release.channel?.slug)) || item.notChannels?.includes(release.channel?.slug)) {
return result;
}
if ((item.networks && !item.networks.includes(release.network?.slug)) || item.notNetworks?.includes(release.network?.slug)) {
return result;
}
if (keys) {
const value = keys.split('|').reduce((acc, key) => acc
|| propProcessors[key]?.(release, typeof item === 'string' ? { key } : item)
|| release[key], null);
return result.concat(curateValue(value, item));
}
if (item.items) {
const group = traverseTemplate(item.items, release, {
delimit: item.delimit,
});
return result.concat(curateValue(group, item));
}
return result;
}, []);
if (results.length > 0) {
// return `${wrap[0] || ''}${results.filter(Boolean).join(delimit)}${wrap[1] || ''}`;
return results.filter(Boolean).join(delimit);
}
return '';
}
export default function processSummaryTemplate(template, release) {
const chain = parse(template);
return traverseTemplate(chain, release);
}