MCPcopy Create free account
hub / github.com/PicoCreator/smol-dev-js / standardTable

Method standardTable

src/cli/OutputHandler.js:499–601  ·  view source on GitHub ↗

* Standard output log, in a table format * * This is enabled if either standard, or table mode is enabled * * @param {Array } table collection of objects, to output * @param {Array } keys collumn keys to output * @param {Array } width collumn widths to output,

(table, keys = null, width = null)

Source from the content-addressed store, hash-verified

497 * @param {Array<Int>} width collumn widths to output, if null this is auto detected
498 */
499 standardTable(table, keys = null, width = null) {
500 // Does nothing if not enabled
501 if(!(this.stdOutput || this.tableOutput)) {
502 return;
503 }
504
505 // Normalize the table
506 table = normalizeObject(table);
507
508 // Lets setup the default width sizing
509 if( width == null ) {
510 width = [];
511
512 // Lets iterate each key
513 // and configure the width
514 for(let i=0; i<keys.length; ++i) {
515 // Get the key
516 let key = keys[i];
517
518 // Calculate key width, given output
519 let keyWidth = key.length;
520
521 // Lets loop through each table item
522 for(let t=0; t<table.length; ++t) {
523
524 // Extract line value
525 // with the relevent key
526 let tableItem = table[t];
527 let value = tableItem[key];
528
529 // NULL handling
530 if( value == null ) {
531 value = "";
532 }
533
534 // Non string value handling
535 if( !(typeof value === 'string' || value instanceof String) ) {
536 value = JSON.stringify(value);
537 }
538
539 // Normalize the length
540 // unless its the last key
541 keyWidth = Math.max(keyWidth, value.length || 0);
542 if( keyWidth > 25 && i<(keys.length - 1) ) {
543 keyWidth = 25;
544 break;
545 }
546 }
547
548 // Lets save the key width
549 width[ i ] = keyWidth;
550 }
551 }
552
553 // @TODO - refactor this segment below to use
554 // standardHeader/row aka DRY cleanup
555
556 // Lets handle the headers

Callers 1

tableMethod · 0.95

Calls 3

normalizeObjectFunction · 0.85
fixedWidthStringFunction · 0.85
dashLineStringFunction · 0.85

Tested by

no test coverage detected