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

100 lines
2.4 KiB
JavaScript

const transliteration = require('transliteration')
const iso6393 = require('@freearhey/iso-639-3')
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 '🌍'
4 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)
4 years ago
return lang && lang.code ? lang.code : null
4 years ago
}
4 years ago
utils.sortBy = function (arr, fields) {
return arr.sort((a, b) => {
for (let field of fields) {
5 years ago
let propA = a[field] ? a[field].toLowerCase() : ''
let propB = b[field] ? b[field].toLowerCase() : ''
3 years ago
if (propA === 'undefined') return 1
if (propB === 'undefined') return -1
if (propA === 'other') return 1
if (propB === 'other') return -1
if (propA < propB) return -1
if (propA > propB) return 1
}
return 0
})
}
4 years ago
utils.removeProtocol = function (string) {
return string.replace(/(^\w+:|^)\/\//, '')
}
utils.filterPlaylists = function (arr, include = '', exclude = '') {
if (include) {
const included = include.split(',').map(filename => `channels/${filename}.m3u`)
return arr.filter(i => included.indexOf(i.url) > -1)
}
3 years ago
if (exclude) {
const excluded = exclude.split(',').map(filename => `channels/${filename}.m3u`)
return arr.filter(i => excluded.indexOf(i.url) === -1)
3 years ago
}
return arr
3 years ago
}
3 years ago
utils.sleep = function (ms) {
return function (x) {
return new Promise(resolve => setTimeout(() => resolve(x), ms))
}
}
4 years ago
module.exports = utils