From 5fc45851cf9136aa8be9293e26330c4af4145160 Mon Sep 17 00:00:00 2001 From: Nolan Date: Sun, 31 Oct 2021 23:28:28 -0400 Subject: [PATCH 1/2] [mod] cleansing country: overhaul --- source/classes/collections/country.mjs | 91 ++++++++++++++++++++++++++ source/classes/company.mjs | 2 +- source/classes/country.mjs | 84 ------------------------ source/classes/movie.mjs | 2 +- source/main.mjs | 2 +- 5 files changed, 94 insertions(+), 87 deletions(-) create mode 100644 source/classes/collections/country.mjs delete mode 100644 source/classes/country.mjs diff --git a/source/classes/collections/country.mjs b/source/classes/collections/country.mjs new file mode 100644 index 0000000..dcf4e09 --- /dev/null +++ b/source/classes/collections/country.mjs @@ -0,0 +1,91 @@ +/* +import {Config} from './config.mjs' +*/ +import {cleanseIsoCode} from '../helpers/conversions.mjs' + +// ⚠️ [TODO] REMOVE TEMPORARY DEV CODE +const eject = (instance) => (JSON.parse(JSON.stringify(instance))) +class List extends Array { } + + +class Country { + /* STEP 1: INITIALIZE CLASS ATTRIBUTE STRUCTURE */ + // Static config info (ℹ️ must be initialized by app) + static config = undefined + + // Universal country code + 'iso3166-1' = undefined + + // Country name in english + name = undefined + + + /* STEP 2: APPLY NEW INSTANCE CONSTRUCTION */ + constructor (data = { }) { + let self = this // allow the forgetting of "this" + data = {...data} // dont mutate input data + + // If the data already has an instance of this class, + // then there is no point in creating a new instance. + // We can replace "self" instance, thus forgetting it. + if (data.country instanceof Country) { + self = data.country + delete data.country + } + + // The "self" variable can either be "this", + // or another class instance obtained from the data. + self.assignData(data) + + return self // override the returning of "this" + } + + + /* STEP 3: CLEAN AND ASSIGN DATA INPUT TO THE INSTANCE */ + assignData ({country}) { + //+ CLEAN THE GENRE DATA +// + if (country == null) { + return + } + else if (country.ids == null) { + ({country} = Country.parseFromAPI({country})) + } + else { + ({country} = Country.parseFromDB({country})) + } + + //+ ASSIGN THE GENRE DATA +// + this['iso3166-1'] = country['iso3166-1'] + this.name = country.name + } + + + toJSON ( ) { + return this + } + + + static parseFromAPI ({country}) { + const newCountry = eject(new Country()) + newCountry['iso3166-1'] = cleanseIsoCode(country.iso_3166_1) + newCountry.name = country.name + return {country: newCountry} + } + + + static parseFromDB ({country}) { + return {country} + } + + + // TODO: Add "matches" + static matches () {throw new Error('⚠️ Need to implement matches on Country!')} + + // TODO: Add "combine" + static combine () {throw new Error('⚠️ Need to implement combine on Country!')} + + // TODO: Add "sharedMetadata" + static #sharedMetadata () {throw new Error('⚠️ Need to implement #sharedMetadata on Country!')} +} + +export {Country} diff --git a/source/classes/company.mjs b/source/classes/company.mjs index b26f6c6..1b59c8a 100644 --- a/source/classes/company.mjs +++ b/source/classes/company.mjs @@ -1,5 +1,5 @@ import {Config} from './config.mjs' -import {Country} from './country.mjs' +import {Country} from './collections/country.mjs' import {Logo} from './image.mjs' class Company { diff --git a/source/classes/country.mjs b/source/classes/country.mjs deleted file mode 100644 index 659f1c7..0000000 --- a/source/classes/country.mjs +++ /dev/null @@ -1,84 +0,0 @@ -import {cleanseIsoCode} from '../helpers/conversions.mjs' - -import {Config} from './config.mjs' - -class Country { - #config - constructor (data = { }) { - let self = this // allow forgetting of "this" - data = {...data} // dont mutate input data - // If the data already has an instance of this class, - // then there is no point in creating a new instance. - // We can replace "self" instance, thus forgetting it. - if (data.country instanceof Country) { - self = data.country - delete data.country - } - - self.assignDefaults( ) - self.assignData(data) - - // override the returning of "this". - return self - } - - /* STEP 1: INITIALIZE CLASS STRUCTURE */ - assignDefaults ( ) { - // this['iso3166-1'] ?? null - // this.name ??= null - } - - /* STEP 2: CLEAN INPUT DATA */ - assignData ({ - config, - country, - }) { - - //+ FIRST, PREPARE THE CONFIG +// - if (config != undefined) { - this.#config = new Config({...this.#shared, config}) - } - - //+ ASSIGN COUNTRY DATA +// - if (country != undefined) { - - // There is only a name and country-code. - if (country.iso_3166_1 !== undefined) { - this['iso3166-1'] = cleanseIsoCode(country.iso_3166_1) - } - - if (country.name !== undefined) { - this.name = country.name - } - } - - // Clean up class data. - this.assignDefaults( ) - } - - toJSON ( ) { - return this - } - - get #shared ( ) { - return { - country: this, - config: this.#config, - } - } - - static matches (item01, item02) { - if (!(item01 instanceof Country && item02 instanceof Country)) { - return false - } - - return item01.name === item02.name - } - - static combine (item01, item02) { - // ⚠️ complete this function - return item01 - } -} - -export {Country} diff --git a/source/classes/movie.mjs b/source/classes/movie.mjs index 83a57e8..982e04e 100644 --- a/source/classes/movie.mjs +++ b/source/classes/movie.mjs @@ -3,7 +3,7 @@ import {convertToEasyDuration} from '../helpers/conversions.mjs' import {Collection} from './collection.mjs' import {Company} from './company.mjs' import {Config} from './config.mjs' -import {Country} from './country.mjs' +import {Country} from './collections/country.mjs' import {Genre} from './collections/genre.mjs' import {Image, Backdrop, Poster} from './image.mjs' import {Language} from './language.mjs' diff --git a/source/main.mjs b/source/main.mjs index 2dce3c3..97b3701 100644 --- a/source/main.mjs +++ b/source/main.mjs @@ -2,7 +2,7 @@ import {Collection} from './classes/collection.mjs' import {Comment} from './classes/comment.mjs' import {Company} from './classes/company.mjs' import {Config} from './classes/config.mjs' -import {Country} from './classes/country.mjs' +import {Country} from './classes/collections/country.mjs' import {Genre} from './classes/collections/genre.mjs' import { Image, From a5d5606f9f04c7727988f84b5d900dd7d437a451 Mon Sep 17 00:00:00 2001 From: Nolan Date: Sun, 31 Oct 2021 23:37:41 -0400 Subject: [PATCH 2/2] [fmt] comments --- source/classes/collections/country.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/classes/collections/country.mjs b/source/classes/collections/country.mjs index dcf4e09..dba8366 100644 --- a/source/classes/collections/country.mjs +++ b/source/classes/collections/country.mjs @@ -43,7 +43,7 @@ class Country { /* STEP 3: CLEAN AND ASSIGN DATA INPUT TO THE INSTANCE */ assignData ({country}) { - //+ CLEAN THE GENRE DATA +// + //+ CLEAN THE COUNTRY DATA +// if (country == null) { return } @@ -54,7 +54,7 @@ class Country { ({country} = Country.parseFromDB({country})) } - //+ ASSIGN THE GENRE DATA +// + //+ ASSIGN THE COUNTRY DATA +// this['iso3166-1'] = country['iso3166-1'] this.name = country.name }