Created
May 4, 2015 15:16
-
-
Save victorpavlenko/2636cf7aafdd852981f9 to your computer and use it in GitHub Desktop.
Задание 2 JSON to table
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
var jsn = require('./json.json'); | |
function Table(jsn){ | |
this.colWidths = function(jsn){ | |
return jsn.map(function(arr, i) { | |
var mass = []; | |
return arr.reduce(function(max, row, item, ar) { | |
row = row.toString().length; | |
mass[item] = row; | |
return mass; | |
}, 0); | |
}); | |
}; | |
this.colHeight = function(jsn){ | |
return jsn.length | |
}; | |
this.cellWidth = function(jsn){ | |
var colWidths = this.colWidths(jsn); | |
var maxCell = this.maxCell(jsn); | |
var colHeight = this.colHeight(jsn); | |
var arrayMaxCellLength = []; | |
var arrayCol = []; | |
for ( var i = 0 ; i < maxCell; i++){ | |
arrayCol[i] = []; | |
for ( var j = 0 ; j < colHeight; j++){ | |
var val = colWidths[j][i]; | |
if(val === undefined) { | |
val = 0 | |
} | |
arrayCol[i].push(val); | |
} | |
arrayMaxCellLength.push( Math.max.apply(null, arrayCol[i])); | |
} | |
return arrayMaxCellLength; | |
}; | |
this.maxCell = function(jsn){ | |
return jsn.reduce(function(prev, next, item, ar){ | |
if(prev > next.length){ | |
return prev | |
}else{ | |
return next.length | |
} | |
}, 0) | |
}; | |
this.separatorAmount = function(){ | |
var separatorAmount = this.maxCell(jsn) - 1; | |
return separatorAmount | |
} | |
this.rowWidth = function(jsn){ | |
var rowWidth = this.cellWidth(jsn).reduce(function(max, prev, item, ar){ | |
return max + prev | |
}, 0); | |
return this.separatorAmount() + rowWidth | |
}; | |
}; | |
function Create(jsn){ | |
this.sizeData = new Table(jsn) | |
this.go = []; | |
this.arrBorder = function(){ | |
var arrBorder = []; | |
var rowWidth = this.sizeData.rowWidth(jsn); | |
for(i = 0; i < rowWidth; i++){ | |
arrBorder.push('-') | |
} | |
arrBorder.unshift('+'); | |
arrBorder.push('+'); | |
arrBorder.push('\r\n'); | |
return arrBorder.join(''); | |
}; | |
this.arrHeader = function(){ | |
var arrHeader = []; | |
var cellWidth = this.sizeData.cellWidth(jsn); | |
arrHeader.unshift('|'); | |
jsn[0].forEach(function(value, item, arr) { | |
while( value.length < cellWidth[item] ){ | |
value = value + ' ' | |
} | |
arrHeader.push(value); | |
arrHeader.push('|') | |
}); | |
arrHeader.push('\r\n'); | |
return arrHeader.join(''); | |
}; | |
this.arrBody = function(){ | |
var arrBody = [[]]; | |
var cellWidth = this.sizeData.cellWidth(jsn); | |
var colHeight = this.sizeData.colHeight(jsn); | |
for(var i = 1; i < colHeight; i++){ | |
itemArr = []; | |
jsn[i].forEach(function(value, item, arr) { | |
while(value.length != cellWidth[item]){ | |
value = value + ' '; | |
} | |
itemArr.push(value); | |
itemArr.push('|') | |
}); | |
itemArr.unshift('|'); | |
itemArr.push('\r\n'); | |
arrBody.push(itemArr.join('')); | |
} | |
return arrBody.join(''); | |
}; | |
this.create = function(){ | |
this.go.push(this.arrBorder()); | |
this.go.push(this.arrHeader()); | |
this.go.push(this.arrBorder()); | |
this.go.push(this.arrBody()); | |
this.go.push(this.arrBorder()); | |
console.log(this.go.join('')) | |
} | |
} | |
var createTable = new Create(jsn); | |
createTable.create(); |
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
[ | |
["String", "Number", "Color", "Pring"], | |
["string 1", 1, "red", "12"], | |
["strasdasding 2", 2, "blasdasdue"], | |
["string 2", 2, "blasdasdue"], | |
["string 2", 2, "blasdasdue"], | |
["string 2", 2, "blasdasasdasddue"], | |
["string 2", 2, "blasdasdue"], | |
["string 2", 2, "blasdasdue"], | |
["strasdasdasding 2", 2, "blasdasdue"], | |
["string 2", 2, "blasdasdue"], | |
["string 2", 2, "blasdasdue"], | |
["string 2", 2, "blasdasdue"], | |
["string 2", 2, "blasdasdue"], | |
["string 2", 2, "blasdasdue"], | |
["string 2", 2, "blaasassasdasdue", 2, "blaasassasdasdue"], | |
["string 2", 2, "blasdasdue"], | |
["string 2", 2, "blasdasdue"], | |
[], | |
["string 3", 3, "blubloong"] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment