From 1e3b28384d32ca36d133b638e9d48644763b6fd9 Mon Sep 17 00:00:00 2001 From: freearhey Date: Sat, 2 Nov 2019 15:43:41 +0300 Subject: [PATCH] Created sortBy() function --- scripts/format.js | 2 +- scripts/generate.js | 59 ++++++++++++--------------------------------- scripts/helper.js | 27 ++++++++------------- 3 files changed, 26 insertions(+), 62 deletions(-) diff --git a/scripts/format.js b/scripts/format.js index cd521d96f..8d28d3dbe 100644 --- a/scripts/format.js +++ b/scripts/format.js @@ -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}'...`) diff --git a/scripts/generate.js b/scripts/generate.js index 569bfa01b..8cd12d361 100644 --- a/scripts/generate.js +++ b/scripts/generate.js @@ -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()) } } diff --git a/scripts/helper.js b/scripts/helper.js index d938175c9..787b01a98 100644 --- a/scripts/helper.js +++ b/scripts/helper.js @@ -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" }) }