You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.3 KiB
JavaScript
70 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
const xtend = require("xtend");
|
|
const rfr = require("rfr");
|
|
|
|
let plugins = [
|
|
rfr("lib/filesets/plugins/youtube-dl"),
|
|
rfr("lib/filesets/plugins/generic-subtitles")
|
|
];
|
|
|
|
function attemptPairing(item, allItems) {
|
|
for (let i = 0; i < plugins.length; i++) {
|
|
let result = plugins[i](item, allItems);
|
|
|
|
if (result.length > 0) {
|
|
return result;
|
|
}
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
module.exports = function(items, options) {
|
|
let indexedItems = items.reduce((total, item) => {
|
|
total[item.name] = item;
|
|
return total;
|
|
}, {});
|
|
|
|
let pairedItems = [];
|
|
let finalItems = [];
|
|
|
|
items.forEach((item) => {
|
|
if (pairedItems.indexOf(item.name) !== -1) {
|
|
return;
|
|
}
|
|
|
|
let pairing = attemptPairing(item, indexedItems).map((pairedItemName) => {
|
|
return indexedItems[pairedItemName];
|
|
});
|
|
|
|
if (pairing.length > 0) {
|
|
pairedItems.push(item.name);
|
|
|
|
pairing.forEach((pairedItem) => {
|
|
pairedItems.push(pairedItem.name);
|
|
});
|
|
|
|
if (options.sorter != null) {
|
|
pairing.sort(options.sorter);
|
|
}
|
|
|
|
finalItems.push(xtend(item, {
|
|
setItems: pairing
|
|
}));
|
|
}
|
|
});
|
|
|
|
// Add all unpaired items to the final list.
|
|
items.forEach((item) => {
|
|
if (pairedItems.indexOf(item.name) === -1 && item.setItems == null) {
|
|
finalItems.push(item);
|
|
}
|
|
})
|
|
|
|
if (options.sorter != null) {
|
|
finalItems.sort(options.sorter);
|
|
}
|
|
|
|
return finalItems;
|
|
} |