Update utils.js

This commit is contained in:
freearhey 2021-01-31 20:11:59 +03:00
parent 96588a97d0
commit 9a96aaa4fe

View File

@ -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,201 +15,35 @@ 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.region2codes = function (region) {
region = region.toUpperCase()
return regions[region] ? regions[region].codes : []
}
utils.code2name = function (code) {
switch (code.toLowerCase()) {
case 'int':
return 'International'
case 'us':
return 'United States'
}
try {
return intlDisplayNames.of(code.toUpperCase())
code = code.toUpperCase()
if (regions[code]) return regions[code].name
if (code === 'US') return 'United States'
return intlDisplayNames.of(code)
} catch (e) {
return null
}
}
utils.codeIsValid = function (code) {
switch (code.toLowerCase()) {
case 'int':
return true
case 'us':
return true
}
try {
intlDisplayNames.of(code.toUpperCase())
return true
} catch (e) {
return false
}
}
utils.language2code = function (name) {
const lang = iso6393.find(l => l.name === name)
@ -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))
}