You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iptv/scripts/helpers/utils.js

78 lines
1.9 KiB
JavaScript

3 years ago
const { orderBy } = require('natural-orderby')
const iso6393 = require('@freearhey/iso-639-3')
3 years ago
const transliteration = require('transliteration')
const categories = require('../data/categories')
const regions = require('../data/regions')
const utils = {}
4 years ago
const intlDisplayNames = new Intl.DisplayNames(['en'], {
style: 'narrow',
type: 'region'
})
utils.name2id = function (name) {
return transliteration
.transliterate(name)
.replace(/\+/gi, 'Plus')
.replace(/[^a-z\d]+/gi, '')
}
4 years ago
utils.code2flag = function (code) {
4 years ago
code = code.toUpperCase()
switch (code) {
4 years ago
case 'UK':
return '🇬🇧'
4 years ago
case 'INT':
return '🌍'
3 years ago
case 'UNDEFINED':
return ''
default:
4 years ago
return code.replace(/./g, char => String.fromCodePoint(char.charCodeAt(0) + 127397))
}
}
4 years ago
utils.region2codes = function (region) {
region = region.toUpperCase()
4 years ago
4 years ago
return regions[region] ? regions[region].codes : []
4 years ago
}
4 years ago
utils.code2name = function (code) {
4 years ago
try {
4 years ago
code = code.toUpperCase()
if (regions[code]) return regions[code].name
if (code === 'US') return 'United States'
4 years ago
if (code === 'INT') return 'International'
4 years ago
return intlDisplayNames.of(code)
4 years ago
} catch (e) {
4 years ago
return null
4 years ago
}
}
4 years ago
utils.language2code = function (name) {
4 years ago
const lang = iso6393.find(l => l.name === name)
3 years ago
return lang && lang.code ? lang.code : null
4 years ago
}
3 years ago
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]
})
3 years ago
return orderBy(arr, fields, order)
}
4 years ago
utils.removeProtocol = function (string) {
return string.replace(/(^\w+:|^)\/\//, '')
}
3 years ago
utils.sleep = function (ms) {
return function (x) {
return new Promise(resolve => setTimeout(() => resolve(x), ms))
}
}
4 years ago
module.exports = utils