UNPKG

@socketsecurity/lib

Version:

Core utilities and infrastructure for Socket.dev security tools

70 lines (69 loc) 2.42 kB
export type ColumnAlignment = 'left' | 'right' | 'center'; /** * Table column configuration. */ export type TableColumn = { key: string; header: string; align?: ColumnAlignment | undefined; width?: number | undefined; color?: ((value: string) => string) | undefined; }; /** * Format data as an ASCII table with borders. * * @param data - Array of data objects * @param columns - Column configuration * @returns Formatted table string * * @example * import { formatTable } from '@socketsecurity/lib/tables' * import colors from 'yoctocolors-cjs' * * const data = [ * { name: 'lodash', version: '4.17.21', issues: 0 }, * { name: 'react', version: '18.2.0', issues: 2 }, * ] * const columns = [ * { key: 'name', header: 'Package' }, * { key: 'version', header: 'Version', align: 'center' }, * { key: 'issues', header: 'Issues', align: 'right', color: (v) => v === '0' ? colors.green(v) : colors.red(v) }, * ] * console.log(formatTable(data, columns)) * // Output: * // ┌─────────┬─────────┬────────┐ * // │ Package │ Version │ Issues │ * // ├─────────┼─────────┼────────┤ * // │ lodash │ 4.17.21 │ 0 │ * // │ react │ 18.2.0 │ 2 │ * // └─────────┴─────────┴────────┘ */ export declare function formatTable(data: Array<Record<string, unknown>>, columns: TableColumn[]): string; /** * Format data as a simple table without borders. * Lighter weight alternative to formatTable(). * * @param data - Array of data objects * @param columns - Column configuration * @returns Formatted table string * * @example * import { formatSimpleTable } from '@socketsecurity/lib/tables' * import colors from 'yoctocolors-cjs' * * const data = [ * { name: 'lodash', version: '4.17.21' }, * { name: 'react', version: '18.2.0' }, * ] * const columns = [ * { key: 'name', header: 'Package' }, * { key: 'version', header: 'Version' }, * ] * console.log(formatSimpleTable(data, columns)) * // Output: * // Package Version * // ─────── ─────── * // lodash 4.17.21 * // react 18.2.0 */ export declare function formatSimpleTable(data: Array<Record<string, unknown>>, columns: TableColumn[]): string;