iptv/scripts/helpers/utils.js

100 lines
2.4 KiB
JavaScript
Raw Normal View History

2021-03-06 19:14:34 +01:00
const transliteration = require('transliteration')
2021-08-01 18:45:15 +02:00
const iso6393 = require('@freearhey/iso-639-3')
2021-01-31 18:11:59 +01:00
const categories = require('./categories')
2021-08-01 18:45:15 +02:00
const regions = require('./regions')
const utils = {}
2021-01-30 05:32:31 +01:00
const intlDisplayNames = new Intl.DisplayNames(['en'], {
style: 'narrow',
type: 'region'
})
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-01-30 04:16:44 +01:00
utils.sortBy = function (arr, fields) {
2019-11-02 13:43:41 +01:00
return arr.sort((a, b) => {
2020-04-11 03:33:53 +02:00
for (let field of fields) {
2020-05-04 15:23:03 +02:00
let propA = a[field] ? a[field].toLowerCase() : ''
let propB = b[field] ? b[field].toLowerCase() : ''
2021-08-01 07:26:01 +02:00
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
2019-11-02 13:43:41 +01:00
}
return 0
})
}
2021-03-27 13:09:20 +01:00
utils.removeProtocol = function (string) {
return string.replace(/(^\w+:|^)\/\//, '')
}
2021-08-01 18:45:15 +02:00
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)
}
2021-08-01 07:26:01 +02:00
2021-08-01 18:45:15 +02:00
if (exclude) {
const excluded = exclude.split(',').map(filename => `channels/${filename}.m3u`)
return arr.filter(i => excluded.indexOf(i.url) === -1)
2021-08-01 07:26:01 +02:00
}
2021-08-01 18:45:15 +02:00
return arr
2021-08-01 05:24:51 +02:00
}
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