-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/njk add table #117
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { formatTable } from "./formatTable"; | ||
|
||
describe("formatTable", () => { | ||
it.each([ | ||
{ | ||
headers: [], | ||
rows: [], | ||
}, | ||
{ | ||
headers: ["h1", "h2"], | ||
rows: [ | ||
["v11", "v12"], | ||
["v21", "v22", "v23"], | ||
], | ||
}, | ||
{ | ||
headers: null, | ||
rows: [["v11", "v12"]], | ||
}, | ||
{ | ||
headers: ["h1"], | ||
rows: null, | ||
}, | ||
{ | ||
headers: ["h1"], | ||
rows: ["v11"], | ||
}, | ||
])("should throw an error if the input is invalid", ({ headers, rows }) => { | ||
expect(() => formatTable(headers, rows)).toThrow( | ||
"formatTable: Invalid input" | ||
); | ||
}); | ||
|
||
it("should format a table for string values", () => { | ||
const headers = ["h1", "h2", "h3"]; | ||
const rows = [ | ||
["v11", "v12", "v13"], | ||
["v21", "v22", "v23"], | ||
["v31", "v32", "v33"], | ||
["vLong1", "vLong2", "v33"], | ||
["v51", "v52", "v533333333333"], | ||
]; | ||
const expected = `h1 | h2 | h3 | ||
------ | ------ | ------------- | ||
v11 | v12 | v13 | ||
v21 | v22 | v23 | ||
v31 | v32 | v33 | ||
vLong1 | vLong2 | v33 | ||
v51 | v52 | v533333333333`; | ||
const result = formatTable(headers, rows); | ||
expect(result).toBe(expected); | ||
}); | ||
it("should format a table for object values", () => { | ||
const headers = ["h1", "h2", "h3"]; | ||
const rows = [ | ||
[{ r1: "v11" }, { r1: "v12" }, { r1: "v13" }], | ||
[{ r1: "v21" }, { r1: "v22" }, { r1: "v23" }], | ||
[{ r1: "v31" }, { r1: "v32" }, { r1: "v33" }], | ||
[{ r1: "vLong1" }, { r1: "vLong2" }, { r1: "v33" }], | ||
[{ r1: "v51" }, { r1: "v52" }, { r1: "v533333333333" }], | ||
]; | ||
const expected = `h1 | h2 | h3 | ||
------ | ------ | ------------- | ||
v11 | v12 | v13 | ||
v21 | v22 | v23 | ||
v31 | v32 | v33 | ||
vLong1 | vLong2 | v33 | ||
v51 | v52 | v533333333333`; | ||
const result = formatTable(headers, rows, "r1"); | ||
expect(result).toBe(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { get } from "lodash"; | ||
|
||
export function formatTable( | ||
headers: unknown, | ||
rows: unknown, | ||
rowProperty?: string | ||
): string { | ||
if ( | ||
!Array.isArray(headers) || | ||
!Array.isArray(rows) || | ||
!rows.every((r) => Array.isArray(r)) || | ||
headers.length === 0 || | ||
rows.length === 0 || | ||
rows.some((r) => r.length > headers.length) | ||
) { | ||
throw new Error("formatTable: Invalid input"); | ||
} | ||
|
||
const mappedRows = | ||
rows.every((r) => r.every((c) => typeof c === "object")) && | ||
rowProperty && | ||
typeof rowProperty === "string" | ||
? rows.map((r) => r.map((c) => get(c, rowProperty))) | ||
: rows; | ||
|
||
const columnWidths = headers.map((header, i) => { | ||
return Math.max(header.length, ...mappedRows.map((row) => row[i].length)); | ||
}); | ||
|
||
const headerRow = headers.map((header, i) => { | ||
return header.padEnd(columnWidths[i]); | ||
}); | ||
|
||
const separatorRow = columnWidths.map((width) => "-".repeat(width)); | ||
|
||
const bodyRows = mappedRows.map((row) => { | ||
return row.map((cell, i) => { | ||
return cell.padEnd(columnWidths[i]); | ||
}); | ||
}); | ||
|
||
return [headerRow, separatorRow, ...bodyRows] | ||
.map((row) => row.join(" | ")) | ||
.join("\n"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no es equivalente y más corto un some de !isArray?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sip, totalmente!