-
-
Save sumskyi/22380de17bbe840309fc8c7b407a832c to your computer and use it in GitHub Desktop.
task.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const data = `city,population,area,density,country | |
Shanghai,24256800,6340,3826,China | |
Delhi,16787941,1484,11313,India | |
Lagos,16060303,1171,13712,Nigeria | |
Istanbul,14160467,5461,2593,Turkey | |
Tokyo,13513734,2191,6168,Japan | |
Sao Paulo,12038175,1521,7914,Brazil | |
Mexico City,8874724,1486,5974,Mexico | |
London,8673713,1572,5431,United Kingdom | |
New York City,8537673,784,10892,United States | |
Bangkok,8280925,1569,5279,Thailand`; | |
let clone = (any) => { return JSON.parse(JSON.stringify(any)) } | |
class CityParser { | |
constructor(data, separator) { | |
this.separator = separator; | |
this.lines = data.split('\n').map((line) => line.trim()); | |
this.header = this.lines.shift(); | |
} | |
call() { | |
this.#makeTable(); | |
this.#addDensityPercentageColumn(); | |
this.#sortByPercentage(); | |
return this.table; | |
} | |
#makeTable() { | |
const cityStruct = this.#makeStruct(this.header); | |
this.table = this.lines.reduce((acc, value) => { | |
acc.push(new cityStruct(...value.split(this.separator))); | |
return acc; | |
}, [] | |
); | |
} | |
#makeStruct(keys) { | |
if (!keys) return null; | |
const keyArray = keys.split(this.separator); | |
const keyCount = keyArray.length; | |
return class { | |
constructor(...args) { | |
for (let i = 0; i < keyCount; i++) { | |
this[keyArray[i]] = args[i]; | |
} | |
} | |
}; | |
} | |
#addDensityPercentageColumn() { | |
this.table = clone(this.table).reduce((acc, value) => { | |
const currentVaue = { | |
...value, | |
percentage: (Math.round((value.density * 100) / this.#maxDensity).toString()) | |
}; | |
acc.push(currentVaue); | |
return acc; | |
}, []); | |
return this.table; | |
} | |
#sortByPercentage () { | |
this.table = this.table.sort((a, b) => b.percentage - a.percentage); | |
} | |
get #maxDensity(){ | |
return this.table.sort((a, b) => a.density - b.density)[this.table.length - 1].density; | |
} | |
} | |
class Printer { | |
constructor(table) { | |
this.table = table; | |
} | |
call() { | |
this.maxLengthData = this.#maxLengthsData(); | |
const padRight = 3; | |
const printRow = (city) => { | |
return city.city.padEnd(this.maxLengthData.city + padRight) + | |
city.population.padStart(this.maxLengthData.population + padRight) + | |
city.area.padStart(this.maxLengthData.area + padRight) + | |
city.density.padStart(this.maxLengthData.density + padRight) + | |
city.country.padStart(this.maxLengthData.country + padRight) + | |
city.percentage.padStart(this.maxLengthData.percentage + padRight) | |
; | |
} | |
for(const city of this.table) { | |
console.log(printRow(city)); | |
} | |
} | |
#maxLengthsData(){ | |
const maxLength = (col) => { | |
const maxBy = clone(this.table).sort((a, b) => b[col].length - a[col].length)[0]; | |
return maxBy[col].length; | |
} | |
return { | |
city: maxLength('city'), | |
population: maxLength('population'), | |
area: maxLength('area'), | |
density: maxLength('density'), | |
country: maxLength('country'), | |
percentage: maxLength('percentage') | |
} | |
} | |
} | |
let cities = new CityParser(data, ',').call(); | |
new Printer(cities).call(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment