2021-09-20 16:08:36 +02:00
|
|
|
const { orderBy } = require('natural-orderby')
|
2021-08-01 18:45:15 +02:00
|
|
|
const iso6393 = require('@freearhey/iso-639-3')
|
2021-09-20 16:08:36 +02:00
|
|
|
const transliteration = require('transliteration')
|
2021-08-30 19:28:44 +02:00
|
|
|
const categories = require('../data/categories')
|
|
|
|
const regions = require('../data/regions')
|
2021-08-01 18:45:15 +02:00
|
|
|
|
|
|
|
const utils = {}
|
2021-01-30 05:32:31 +01:00
|
|
|
const intlDisplayNames = new Intl.DisplayNames(['en'], {
|
|
|
|
style: 'narrow',
|
|
|
|
type: 'region'
|
|
|
|
})
|
2019-08-08 02:29:36 +02:00
|
|
|
|
2021-03-06 19:14:34 +01:00
|
|
|
utils.name2id = function (name) {
|
|
|
|
return transliteration
|
|
|
|
.transliterate(name)
|
|
|
|
.replace(/\+/gi, 'Plus')
|
|
|
|
.replace(/[^a-z\d]+/gi, '')
|
|
|
|
}
|
|
|
|
|
2021-01-30 04:16:44 +01:00
|
|
|
utils.code2flag = function (code) {
|
2021-01-31 18:11:59 +01:00
|
|
|
code = code.toUpperCase()
|
2020-05-06 17:01:36 +02:00
|
|
|
switch (code) {
|
2021-01-31 18:11:59 +01:00
|
|
|
case 'UK':
|
2020-05-06 17:01:36 +02:00
|
|
|
return '🇬🇧'
|
2021-02-08 11:25:29 +01:00
|
|
|
case 'INT':
|
|
|
|
return '🌍'
|
2021-05-10 15:12:22 +02:00
|
|
|
case 'UNDEFINED':
|
2020-09-17 22:30:28 +02:00
|
|
|
return ''
|
2020-05-06 17:01:36 +02:00
|
|
|
default:
|
2021-01-31 18:11:59 +01:00
|
|
|
return code.replace(/./g, char => String.fromCodePoint(char.charCodeAt(0) + 127397))
|
2020-05-06 17:01:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 18:11:59 +01:00
|
|
|
utils.region2codes = function (region) {
|
|
|
|
region = region.toUpperCase()
|
2021-01-30 04:16:44 +01:00
|
|
|
|
2021-01-31 18:11:59 +01:00
|
|
|
return regions[region] ? regions[region].codes : []
|
2021-01-29 23:58:52 +01:00
|
|
|
}
|
|
|
|
|
2021-01-31 18:11:59 +01:00
|
|
|
utils.code2name = function (code) {
|
2021-01-30 05:32:31 +01:00
|
|
|
try {
|
2021-01-31 18:11:59 +01:00
|
|
|
code = code.toUpperCase()
|
|
|
|
if (regions[code]) return regions[code].name
|
|
|
|
if (code === 'US') return 'United States'
|
2021-02-08 11:25:29 +01:00
|
|
|
if (code === 'INT') return 'International'
|
2021-01-31 18:11:59 +01:00
|
|
|
return intlDisplayNames.of(code)
|
2021-01-30 05:32:31 +01:00
|
|
|
} catch (e) {
|
2021-01-31 18:11:59 +01:00
|
|
|
return null
|
2021-01-30 05:32:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 04:16:44 +01:00
|
|
|
utils.language2code = function (name) {
|
2021-01-29 23:58:52 +01:00
|
|
|
const lang = iso6393.find(l => l.name === name)
|
|
|
|
|
2021-07-06 21:32:08 +02:00
|
|
|
return lang && lang.code ? lang.code : null
|
2021-01-29 23:58:52 +01:00
|
|
|
}
|
|
|
|
|
2021-09-20 16:08:36 +02:00
|
|
|
utils.sortBy = function (arr, fields, order = null) {
|
|
|
|
fields = fields.map(field => {
|
|
|
|
if (field === 'resolution.height') return channel => channel.resolution.height || 0
|
|
|
|
if (field === 'status') return channel => channel.status || ''
|
|
|
|
return channel => channel[field]
|
2019-11-02 13:43:41 +01:00
|
|
|
})
|
2021-09-20 16:08:36 +02:00
|
|
|
return orderBy(arr, fields, order)
|
2019-11-02 13:43:41 +01:00
|
|
|
}
|
|
|
|
|
2021-03-27 13:09:20 +01:00
|
|
|
utils.removeProtocol = function (string) {
|
|
|
|
return string.replace(/(^\w+:|^)\/\//, '')
|
|
|
|
}
|
|
|
|
|
2021-08-16 12:07:27 +02:00
|
|
|
utils.sleep = function (ms) {
|
|
|
|
return function (x) {
|
|
|
|
return new Promise(resolve => setTimeout(() => resolve(x), ms))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 04:16:44 +01:00
|
|
|
module.exports = utils
|