UNPKG

json-to-table-format

Version:
146 lines (103 loc) 3.33 kB
# JSON to Tabular A TypeScript/JavaScript library that converts JSON data into beautifully formatted tables with various styling options. ## Installation ```bash npm install json-to-table-format # or yarn add json-to-table-format ``` ## Basic Usage ### ES6 Import (Recommended) ```javascript import { jsontoTable } from "json-to-table-format"; const data = [ { name: "John", age: 30, city: "New York" }, { name: "Alice", age: 25, city: "London" }, ]; const result = jsontoTable(data); console.log(result); ``` ### CommonJS Import ```javascript // Method 1: Using default export const jsontoTable = require("json-to-table-format").default; // Method 2: Using named export const { jsontoTable } = require("json-to-table-format"); const data = [ { name: "John", age: 30, city: "New York" }, { name: "Alice", age: 25, city: "London" }, ]; const result = jsontoTable(data); console.log(result); ``` Output: ``` name age city John 30 New York Alice 25 London ``` ## Table Styles ### Simple Style (Default) ```javascript import { jsontoTable } from "json-to-table-format"; const data = [ { name: "John", age: 30, city: "New York" }, { name: "Alice", age: 25, city: "London" }, ]; const result = jsontoTable(data, "simple"); // or just jsontoTable(data) console.log(result); ``` ### ASCII Style with Borders ```javascript import { jsontoTable } from "json-to-table-format"; const data = [ { name: "Alice", age: 25, city: "New York" }, { name: "Bob", age: 30, city: "London" }, { name: "Charlie", age: 35, city: "Paris" }, ]; const result = jsontoTable(data, "ascii"); console.log(result); ``` Output: ``` +----------+--------+-------------+ |name age city | +----------+--------+-------------+ |Alice 25 New York | |Bob 30 London | |Charlie 35 Paris | +----------+--------+-------------+ ``` ## API Reference ### Function: `jsontoTable(data, style?)` Converts JSON data into a tabular string format. #### Parameters: - `data` (Array | Object): JSON data to convert. Can be an array of objects or a single object. - `style` (string, optional): Table style. Options: - `'simple'` (default): Clean table without borders - `'ascii'`: ASCII-style table with borders #### Returns: - `string`: Formatted table as a string #### Examples: ```javascript // Single object const singleData = { name: "John", age: 30, city: "New York" }; const result1 = jsontoTable(singleData); // Array of objects const arrayData = [ { name: "John", age: 30, city: "New York" }, { name: "Alice", age: 25, city: "London" }, ]; const result2 = jsontoTable(arrayData, "ascii"); ``` ## Features - ✅ **Simple and clean API**: Just one function call - ✅ **Multiple table styles**: Simple and ASCII border styles - ✅ **Flexible input**: Accepts single objects or arrays - ✅ **Automatic column sizing**: Adjusts column widths based on content - ✅ **TypeScript support**: Full TypeScript definitions included - ✅ **Cross-platform**: Works in Node.js and browsers ## Contributing Contributions are welcome! Please feel free to submit a Pull Request. ## License MIT