Skip to content

Instantly share code, notes, and snippets.

@jkraszewski
Created May 30, 2018 19:14
Show Gist options
  • Save jkraszewski/fab8fd04f656d77459b4a8a9d429872b to your computer and use it in GitHub Desktop.
Save jkraszewski/fab8fd04f656d77459b4a8a9d429872b to your computer and use it in GitHub Desktop.
test_data = [
['Source IP', 'Source Netmask', 'Destination IP', 'Destination Netmask', 'Port', 'Protocol', 'Action'],
['123.456.789.000', '/30', '111.111.111.111', '/30', '8080', 'TCP', 'Accept'],
['123.23.45.233', '/31', '127.0.0.1', '/8', '22', 'UDP', 'Deny'],
['1.1.1.1', '/31', '1.2.3.4', '/30', '1', 'TCP', 'Deny'],
['10.0.156.35', '/24', '123.32.234.7', '/12', '1337', 'BGP', 'Accept'],
];
// List<List<String>> -> String
// Assumes a non-empty list of non-empty lists of Strings
function create_table(lst){
retVal = "";
size = lst[0].length;
spaces = [lst[0].length];
num_dashes = 0;
for(var x = 0; x < lst[0].length; x++){
spaces[x] = 0;
}
//get spaces info
for(var i = 0; i < lst.length; i++){
tmp_dashes = 0;
for(let y = 0; y < size; y++){
tmp_dashes += lst[i][y].length;
tmp = Math.floor(lst[i][y].length / 8) + 1;
if(tmp > spaces[y]) {
spaces[y] = tmp;
}
tmp = 1;
}
if(tmp_dashes > num_dashes){
num_dashes = tmp_dashes;
}
}
//add tab spaces to dashes
for(var m = 0; m < spaces.length; m++) {
num_dashes += spaces[m];
}
// start adding values to return String
for(var i = 0; i < lst.length; i++){
for(var y = 0; y < lst[i].length; y++) {
retVal += lst[i][y];
this_size = Math.floor(lst[i][y].length / 8);
for(k = this_size; k < spaces[y]; k++) {
retVal += '\t';
}
}
retVal += '\n';
// if after first row, add line of dashes
if(i === 0) {
for(var c = 0; c < num_dashes; c++){
retVal += '-';
}
retVal += '\n';
}
}
return retVal;
}
result = create_table(test_data);
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment