From 9a96aaa4fec7ff0e0cebac8aa5c8fb131fa2ee4d Mon Sep 17 00:00:00 2001 From: freearhey Date: Sun, 31 Jan 2021 20:11:59 +0300 Subject: [PATCH] Update utils.js --- scripts/utils.js | 196 ++++------------------------------------------- 1 file changed, 16 insertions(+), 180 deletions(-) diff --git a/scripts/utils.js b/scripts/utils.js index 6981caa2c..f48e79474 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -6,6 +6,8 @@ const urlParser = require('url') const escapeStringRegexp = require('escape-string-regexp') const markdownInclude = require('markdown-include') const iso6393 = require('iso-639-3') +const regions = require('./regions') +const categories = require('./categories') const intlDisplayNames = new Intl.DisplayNames(['en'], { style: 'narrow', type: 'region' @@ -13,198 +15,32 @@ const intlDisplayNames = new Intl.DisplayNames(['en'], { const utils = {} -utils.supportedCategories = [ - { - name: 'Auto', - id: 'auto', - nsfw: false - }, - { - name: 'Business', - id: 'business', - nsfw: false - }, - { - name: 'Classic', - id: 'classic', - nsfw: false - }, - { - name: 'Comedy', - id: 'comedy', - nsfw: false - }, - { - name: 'Documentary', - id: 'documentary', - nsfw: false - }, - { - name: 'Education', - id: 'education', - nsfw: false - }, - { - name: 'Entertainment', - id: 'entertainment', - nsfw: false - }, - { - name: 'Family', - id: 'family', - nsfw: false - }, - { - name: 'Fashion', - id: 'fashion', - nsfw: false - }, - { - name: 'Food', - id: 'food', - nsfw: false - }, - { - name: 'General', - id: 'general', - nsfw: false - }, - { - name: 'Health', - id: 'health', - nsfw: false - }, - { - name: 'History', - id: 'history', - nsfw: false - }, - { - name: 'Hobby', - id: 'hobby', - nsfw: false - }, - { - name: 'Kids', - id: 'kids', - nsfw: false - }, - { - name: 'Legislative', - id: 'legislative', - nsfw: false - }, - { - name: 'Lifestyle', - id: 'lifestyle', - nsfw: false - }, - { - name: 'Local', - id: 'local', - nsfw: false - }, - { - name: 'Movies', - id: 'movies', - nsfw: false - }, - { - name: 'Music', - id: 'music', - nsfw: false - }, - { - name: 'News', - id: 'news', - nsfw: false - }, - { - name: 'Quiz', - id: 'quiz', - nsfw: false - }, - { - name: 'Religious', - id: 'religious', - nsfw: false - }, - { - name: 'Sci-Fi', - id: 'sci-fi', - nsfw: false - }, - { - name: 'Shop', - id: 'shop', - nsfw: false - }, - { - name: 'Sport', - id: 'sport', - nsfw: false - }, - { - name: 'Travel', - id: 'travel', - nsfw: false - }, - { - name: 'Weather', - id: 'weather', - nsfw: false - }, - { - name: 'XXX', - id: 'xxx', - nsfw: true - } -] - utils.code2flag = function (code) { + code = code.toUpperCase() switch (code) { - case 'uk': + case 'UK': return '🇬🇧' - case 'int': - return '🌎' - case 'unsorted': + case 'UNSORTED': return '' default: - return code - .toUpperCase() - .replace(/./g, char => String.fromCodePoint(char.charCodeAt(0) + 127397)) + return code.replace(/./g, char => String.fromCodePoint(char.charCodeAt(0) + 127397)) } } -utils.code2name = function (code) { - switch (code.toLowerCase()) { - case 'int': - return 'International' - case 'us': - return 'United States' - } +utils.region2codes = function (region) { + region = region.toUpperCase() - try { - return intlDisplayNames.of(code.toUpperCase()) - } catch (e) { - return null - } + return regions[region] ? regions[region].codes : [] } -utils.codeIsValid = function (code) { - switch (code.toLowerCase()) { - case 'int': - return true - case 'us': - return true - } - +utils.code2name = function (code) { try { - intlDisplayNames.of(code.toUpperCase()) - - return true + code = code.toUpperCase() + if (regions[code]) return regions[code].name + if (code === 'US') return 'United States' + return intlDisplayNames.of(code) } catch (e) { - return false + return null } } @@ -365,7 +201,7 @@ utils.writeToLog = function (country, msg, url) { } utils.filterNSFW = function (arr) { - const sfwCategories = utils.supportedCategories.filter(c => !c.nsfw).map(c => c.name) + const sfwCategories = categories.filter(c => !c.nsfw).map(c => c.name) return arr.filter(i => sfwCategories.includes(i.category)) }