Files
MangaCircus/chapter-identity.js
T

39 lines
2.0 KiB
JavaScript

'use strict';
// An unnumbered release is not chapter zero. Keep its upstream identity intact.
const UUID = '[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}';
const SPECIAL = new RegExp(`^special-mangadex-(${UUID})$`, 'i');
const VOLUME = /^special-weebcentral-volume-([a-z0-9]{26})$/i;
const SPECIAL_FOLDER = new RegExp(`^(special-mangadex-${UUID}|special-weebcentral-volume-[a-z0-9]{26})(?: (.*))?$`, 'i');
const NUMBERED_FOLDER = /^([a-z]*) ?(\d+(?:\.\d+)?)(?=$|[\s_\-[(])[\s_-]*(.*)$/i;
const isSpecialKey = value => SPECIAL.test(String(value)) || VOLUME.test(String(value));
function specialKey(id) {
const key = `special-mangadex-${String(id).toLowerCase()}`;
if (!isSpecialKey(key)) throw new Error('Unnumbered MangaDex release needs a stable chapter UUID');
return key;
}
function volumeKey(id) {
const key = `special-weebcentral-volume-${String(id).toLowerCase()}`;
if (!VOLUME.test(key)) throw new Error('WeebCentral volume needs a stable 26-character release ID');
return key;
}
function parseSpecialFolder(name) {
const match = SPECIAL_FOLDER.exec(name);
if (!match) return null;
let title = (match[2] || '').trim(), src = '';
const source = /\s*\[([^\]]*)\]$/.exec(title);
if (source) { src = source[1].trim(); title = title.slice(0, source.index).trim(); }
return { key: match[1].toLowerCase(), special: true, releaseKind: VOLUME.test(match[1]) ? 'volume' : 'special', x: false, num: null, title, src };
}
// Reader and scraper must assign the same identity to every numbered folder.
function parseKey(name) {
const match = NUMBERED_FOLDER.exec(name);
if (!match) return null;
const x = match[1].toLowerCase() === 'x', num = parseFloat(match[2]);
let title = match[3].trim(), src = '';
const source = /\s*\[([^\]]*)\]$/.exec(title);
if (source) { src = source[1].trim(); title = title.slice(0, source.index).trim(); }
return { key: (x ? 'x' : '') + num, x, num, title: title.replace(/^[-:.]\s*/, ''), src };
}
module.exports = { isSpecialKey, specialKey, volumeKey, parseSpecialFolder, parseKey };