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

@ -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" })
}