iptv/scripts/helpers/utils.js

87 lines
2.2 KiB
JavaScript
Raw Normal View History

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