From 859cd32f86ed503f0223488ae85f29acac1c435b Mon Sep 17 00:00:00 2001 From: freearhey Date: Thu, 11 Feb 2021 07:16:19 +0300 Subject: [PATCH] Update db.js --- scripts/db.js | 64 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 7 deletions(-) diff --git a/scripts/db.js b/scripts/db.js index 73012da42..f4afae626 100644 --- a/scripts/db.js +++ b/scripts/db.js @@ -28,9 +28,50 @@ db.load = function () { db.channels = { list: [], + filter: null, add(channel) { this.list.push(channel) }, + get() { + let output + if (this.filter) { + switch (this.filter.field) { + case 'countries': + if (!this.filter.value) { + output = this.list.filter(channel => !channel.countries.length) + } else { + output = this.list.filter(channel => + channel.countries.map(c => c.code).includes(this.filter.value) + ) + } + break + case 'languages': + if (!this.filter.value) { + output = this.list.filter(channel => !channel.languages.length) + } else { + output = this.list.filter(channel => + channel.countries.map(c => c.code).includes(this.filter.value) + ) + } + break + case 'category': + if (!this.filter.value) { + output = this.list.filter(channel => !channel.category) + } else { + output = this.list.filter( + channel => channel.category.toLowerCase() === this.filter.value + ) + } + break + } + } else { + output = this.all() + } + + this.filter = null + + return output + }, all() { return this.list }, @@ -40,22 +81,31 @@ db.channels = { return this.list.filter(i => sfwCategories.includes(i.category)) }, forCountry(country) { - if (!country.code) return this.list.filter(channel => !channel.countries.length) + this.filter = { + field: 'countries', + value: country.code + } - return this.list.filter(channel => channel.countries.map(c => c.code).includes(country.code)) + return this }, forLanguage(language) { - if (!language.code) return this.list.filter(channel => !channel.languages.length) + this.filter = { + field: 'languages', + value: language.code + } - return this.list.filter(channel => channel.languages.map(c => c.code).includes(language.code)) + return this }, forCategory(category) { - if (!category.id) return this.list.filter(channel => !channel.category) + this.filter = { + field: 'category', + value: category.id + } - return this.list.filter(channel => channel.category.toLowerCase() === category.id) + return this }, count() { - return this.list.length + return this.get().length }, sortBy(fields) { this.list = utils.sortBy(this.list, fields)