CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-snazzy

Format JavaScript Standard Style as Stylish (i.e. snazzy) output

94

1.18x
Overview
Eval results
Files

task.mdevals/scenario-1/

Table Formatter

Build a simple tool that formats structured data into aligned, readable tables for terminal output.

Requirements

Your tool should accept an array of data rows where each row is an array of values. It should output a formatted table where columns are properly aligned and spaced.

Input format:

  • An array of arrays, where each inner array represents a row
  • Each value in a row should be displayed in its own column
  • Values may contain ANSI color codes (terminal color escape sequences)

Output format:

  • Columns should be aligned based on the visual width of text (ignoring invisible ANSI codes)
  • Different columns can have different alignment (left, right, or center)
  • Rows should be separated by newlines
  • Proper spacing should be added between columns

Column alignment:

  • First column: no specific alignment (default)
  • Second column: right-aligned
  • Third column: left-aligned

Implementation

@generates

Create a function formatTable(rows, options) that:

  • Takes an array of rows (where each row is an array of string values)
  • Takes an options object that includes alignment configuration
  • Returns a formatted string with aligned columns
  • Handles ANSI color codes correctly when calculating column widths

Example

Input:

const rows = [
  ['100', '5', 'First row'],
  ['2', '300', 'Second row'],
  ['3000', '1', 'Third row']
];

formatTable(rows, { align: ['', 'r', 'l'] });

Expected output (note the alignment):

100     5  First row
2     300  Second row
3000    1  Third row

Test Cases

  • Formatting a simple table with numeric and text data returns properly aligned columns @test
  • Formatting a table with ANSI-colored text maintains correct alignment despite invisible color codes @test
  • Empty input array returns an empty string @test

API

/**
 * Formats an array of rows into an aligned table string
 * @param {Array<Array<string>>} rows - Array of rows, each row is an array of column values
 * @param {Object} options - Formatting options
 * @param {Array<string>} options.align - Array of alignment strings: '' (none), 'r' (right), 'l' (left)
 * @returns {string} Formatted table string with aligned columns
 */
function formatTable(rows, options) {
  // Implementation here
}

module.exports = { formatTable };

Dependencies { .dependencies }

text-table { .dependency }

Provides table formatting with column alignment support.

strip-ansi { .dependency }

Removes ANSI color codes from strings to calculate accurate visual widths.

Install with Tessl CLI

npx tessl i tessl/npm-snazzy

tile.json