Created sortBy() function

This commit is contained in:
freearhey 2019-11-02 15:43:41 +03:00
parent cb2fbc6962
commit 1e3b28384d
3 changed files with 26 additions and 62 deletions

View File

@ -147,7 +147,7 @@ async function main() {
if(verbose) {
console.log(`Sorting channels...`)
}
channels = helper.sortByTitleAndUrl(channels)
channels = helper.sortBy(channels, ['title', 'url'])
if(!debug) {
console.log(`Updating '${country.url}'...`)

View File

@ -90,14 +90,7 @@ function generateIndex() {
const filename = `${ROOT_DIR}/index.m3u`
helper.createFile(filename, '#EXTM3U\n')
const channels = list.all.sort((a, b) => {
if(a.title.toLowerCase() < b.title.toLowerCase()) { return -1 }
if(a.title.toLowerCase() > b.title.toLowerCase()) { return 1 }
if(a.url < b.url) { return -1 }
if(a.url > b.url) { return 1 }
return 0
})
const channels = helper.sortBy(list.all, ['title', 'url'])
for(let channel of channels) {
helper.appendToFile(filename, channel.toString())
}
@ -107,7 +100,8 @@ function generateCountryIndex() {
const filename = `${ROOT_DIR}/index.country.m3u`
helper.createFile(filename, '#EXTM3U\n')
for(let channel of list.all) {
const channels = helper.sortBy(list.all, ['countryName', 'title', 'url'])
for(let channel of channels) {
const group = channel.group
channel.group = channel.countryName
helper.appendToFile(filename, channel.toString())
@ -119,16 +113,7 @@ function generateLanguageIndex() {
const filename = `${ROOT_DIR}/index.language.m3u`
helper.createFile(filename, '#EXTM3U\n')
const channels = list.all.sort((a, b) => {
if(a.language < b.language) { return -1 }
if(a.language > b.language) { return 1 }
if(a.title.toLowerCase() < b.title.toLowerCase()) { return -1 }
if(a.title.toLowerCase() > b.title.toLowerCase()) { return 1 }
if(a.url < b.url) { return -1 }
if(a.url > b.url) { return 1 }
return 0
})
const channels = helper.sortBy(list.all, ['language', 'title', 'url'])
for(let channel of channels) {
const group = channel.group
channel.group = channel.language
@ -141,16 +126,7 @@ function generateCategoryIndex() {
const filename = `${ROOT_DIR}/index.category.m3u`
helper.createFile(filename, '#EXTM3U\n')
const channels = list.all.sort((a, b) => {
if(a.group < b.group) { return -1 }
if(a.group > b.group) { return 1 }
if(a.title.toLowerCase() < b.title.toLowerCase()) { return -1 }
if(a.title.toLowerCase() > b.title.toLowerCase()) { return 1 }
if(a.url < b.url) { return -1 }
if(a.url > b.url) { return 1 }
return 0
})
const channels = helper.sortBy(list.all, ['group', 'title', 'url'])
for(let channel of channels) {
helper.appendToFile(filename, channel.toString())
}
@ -160,18 +136,7 @@ function generateFullIndex() {
const filename = `${ROOT_DIR}/index.full.m3u`
helper.createFile(filename, '#EXTM3U\n')
const channels = list.all.sort((a, b) => {
if(a.countryName < b.countryName) { return -1 }
if(a.countryName > b.countryName) { return 1 }
if(a.group < b.group) { return -1 }
if(a.group > b.group) { return 1 }
if(a.title.toLowerCase() < b.title.toLowerCase()) { return -1 }
if(a.title.toLowerCase() > b.title.toLowerCase()) { return 1 }
if(a.url < b.url) { return -1 }
if(a.url > b.url) { return 1 }
return 0
})
const channels = helper.sortBy(list.all, ['countryName', 'group', 'title', 'url'])
for(let channel of channels) {
const group = channel.group
channel.group = [ channel.countryName, channel.group ].filter(i => i).join(';')
@ -188,7 +153,9 @@ function generateCountries() {
let country = list.countries[cid]
const filename = `${outputDir}/${cid}.m3u`
helper.createFile(filename, '#EXTM3U\n')
for(let channel of country) {
const channels = helper.sortBy(Object.values(country), ['title', 'url'])
for(let channel of channels) {
helper.appendToFile(filename, channel.toString())
}
}
@ -202,7 +169,9 @@ function generateCategories() {
let category = list.categories[cid]
const filename = `${outputDir}/${cid}.m3u`
helper.createFile(filename, '#EXTM3U\n')
for(let channel of category) {
const channels = helper.sortBy(Object.values(category), ['title', 'url'])
for(let channel of channels) {
helper.appendToFile(filename, channel.toString())
}
}
@ -216,7 +185,9 @@ function generateLanguages() {
let language = list.languages[lid]
const filename = `${outputDir}/${lid}.m3u`
helper.createFile(filename, '#EXTM3U\n')
for(let channel of language) {
const channels = helper.sortBy(Object.values(language), ['title', 'url'])
for(let channel of channels) {
helper.appendToFile(filename, channel.toString())
}
}

View File

@ -12,6 +12,16 @@ const markdownInclude = require('markdown-include')
let cache = {}
let helper = {}
helper.sortBy = function(arr, fields) {
return arr.sort((a, b) => {
for(let field of fields) {
if(a[field].toLowerCase() < b[field].toLowerCase()) { return -1 }
if(a[field].toLowerCase() > b[field].toLowerCase()) { return 1 }
}
return 0
})
}
helper.createDir = function(dir) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
@ -97,23 +107,6 @@ helper.getEPGFile = function(url) {
})
}
helper.sortByTitleAndUrl = function(arr) {
return arr.sort(function(a, b) {
var titleA = a.title.toLowerCase()
var titleB = b.title.toLowerCase()
var urlA = a.url.toLowerCase()
var urlB = b.url.toLowerCase()
if(titleA < titleB) return -1
if(titleA > titleB) return 1
if(urlA < urlB) return -1
if(urlA > urlB) return 1
return 0
})
}
helper.readFile = function(filename) {
return fs.readFileSync(path.resolve(__dirname) + `/../${filename}`, { encoding: "utf8" })
}