Update utils.js

This commit is contained in:
Aleksandr Statciuk 2021-09-20 17:08:36 +03:00
parent 7bcd1fedd4
commit d0766d5e39
1 changed files with 8 additions and 14 deletions

View File

@ -1,5 +1,6 @@
const transliteration = require('transliteration') const { orderBy } = require('natural-orderby')
const iso6393 = require('@freearhey/iso-639-3') const iso6393 = require('@freearhey/iso-639-3')
const transliteration = require('transliteration')
const categories = require('../data/categories') const categories = require('../data/categories')
const regions = require('../data/regions') const regions = require('../data/regions')
@ -54,20 +55,13 @@ utils.language2code = function (name) {
return lang && lang.code ? lang.code : null return lang && lang.code ? lang.code : null
} }
utils.sortBy = function (arr, fields) { utils.sortBy = function (arr, fields, order = null) {
return arr.sort((a, b) => { fields = fields.map(field => {
for (let field of fields) { if (field === 'resolution.height') return channel => channel.resolution.height || 0
let propA = a[field] ? a[field].toLowerCase() : '' if (field === 'status') return channel => channel.status || ''
let propB = b[field] ? b[field].toLowerCase() : '' return channel => channel[field]
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
}) })
return orderBy(arr, fields, order)
} }
utils.removeProtocol = function (string) { utils.removeProtocol = function (string) {