UNPKG

json-to-table-format

Version:
133 lines (125 loc) 3.45 kB
import { getData, getStartPoints, getRows } from "../src/lib"; // Example 1: Basic Usage with Simple Style console.log("=== Example 1: Basic Usage with Simple Style ==="); const basicData = [ { name: "John", age: 30, city: "New York" }, { name: "Alice", age: 25, city: "London" }, { name: "Bob", age: 35, city: "Paris" }, ]; const headers = ["name", "age", "city"]; const data = getData(basicData); const startPoints = getStartPoints(data, headers); const rows = getRows(data, headers, startPoints); console.log(rows.join("\n")); // Example 2: ASCII Style Table console.log("\n=== Example 2: ASCII Style Table ==="); const asciiRows = getRows(data, headers, startPoints, "ascii"); console.log(asciiRows.join("\n")); // Example 3: Complex Data with ASCII Style console.log("\n=== Example 3: Complex Data with ASCII Style ==="); const complexData = [ { name: "John Smith", age: 30, active: true, address: { street: "123 Main St", city: "New York", country: "USA", coordinates: { lat: 40.7128, lng: -74.006, }, }, hobbies: [ { activity: "reading", level: "advanced" }, { activity: "gaming", level: "intermediate", platforms: ["PC", "PS5"] }, { activity: "coding", level: "expert" }, ], lastLogin: "2025-05-28T10:00:00Z", score: 85.5, metadata: { source: "internal", tags: ["employee", "senior"], }, projects: ["Project Alpha", "Project Beta"], isVerified: true, lastUpdated: "2025-05-28T12:00:00Z", }, { name: "Alice Johnson", age: 25, active: false, address: { street: "456 Park Ave", city: "London", country: "UK", coordinates: { lat: 51.5074, lng: -0.1278, }, }, hobbies: [ { activity: "painting", level: "beginner" }, { activity: "dancing", level: "professional" }, ], lastLogin: null, score: 92.0, metadata: { source: "external", tags: ["contractor"], }, projects: ["Project Gamma"], isVerified: false, lastUpdated: null, }, { name: "Bob", age: undefined, active: true, address: { street: null, city: "Tokyo", country: "Japan", coordinates: null, }, hobbies: [], lastLogin: "2025-05-29T08:30:00Z", score: null, metadata: { source: "internal", tags: [], }, projects: [], isVerified: true, lastUpdated: "2025-05-29T09:00:00Z", }, ]; // Get all columns from the first object const complexDataProcessed = getData(complexData); const complexHeaders = Object.keys(complexDataProcessed[0]); const complexStartPoints = getStartPoints(complexDataProcessed, complexHeaders); const complexRows = getRows( complexDataProcessed, complexHeaders, complexStartPoints, "ascii" ); console.log(complexRows.join("\n")); // Example 4: Varying Name Lengths console.log("\n=== Example 4: Varying Name Lengths ==="); const varyingData = [ { name: "John", age: 30, city: "New York" }, { name: "Alice", age: 25, city: "London" }, { name: "Christopher", age: 35, city: "Paris" }, { name: "Bob", age: 28, city: "Tokyo" }, ]; const varyingDataProcessed = getData(varyingData); const varyingStartPoints = getStartPoints(varyingDataProcessed, headers); const varyingRows = getRows( varyingDataProcessed, headers, varyingStartPoints, "ascii" ); console.log(varyingRows.join("\n"));