From 32b86ce84259dfe19fb6a3a14af234c5bbdfa539 Mon Sep 17 00:00:00 2001 From: Aleksandr Statciuk Date: Sat, 5 Feb 2022 02:47:16 +0300 Subject: [PATCH] Create cid.js --- scripts/core/cid.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 scripts/core/cid.js diff --git a/scripts/core/cid.js b/scripts/core/cid.js new file mode 100644 index 000000000..7be12caf3 --- /dev/null +++ b/scripts/core/cid.js @@ -0,0 +1,41 @@ +const file = require('./file') +const transliteration = require('transliteration') + +const cid = {} + +cid.generate = function (title, filepath) { + const name = parseChannelName(title) + const code = parseCountryCode(filepath) + + if (name && code) { + const slug = transliteration + .transliterate(name) + .replace(/\+/gi, 'Plus') + .replace(/[^a-z\d]+/gi, '') + + return `${slug}.${code.toLowerCase()}` + } + + return null +} + +module.exports = cid + +function parseCountryCode(filepath) { + if (!filepath) return null + const basename = file.basename(filepath) + const [code] = basename.split('_') || [null] + + return code +} + +function parseChannelName(title) { + return title + .trim() + .split(' ') + .map(s => s.trim()) + .filter(s => { + return !/\[|\]/i.test(s) && !/\((\d+)P\)/i.test(s) + }) + .join(' ') +}