Skip to content
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 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v16.3.0

- Add `@alanszp/nunjucks-utils`: Add `formatTable` for supporting markdown tables
- Add `@alanszp/nunjucks-utils`: Add `groupBy`

## v16.2.2

- Add `@alanszp/nunjucks-utils`: Fix implementation of formatString and add text for old js versions.
Expand Down
72 changes: 72 additions & 0 deletions packages/nunjucks-utils/src/filters/formatTable.test.ts
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);
});
});
45 changes: 45 additions & 0 deletions packages/nunjucks-utils/src/filters/formatTable.ts
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)) ||
Copy link
Collaborator

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sip, totalmente!

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");
}
1 change: 1 addition & 0 deletions packages/nunjucks-utils/src/filters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from "./rejectBy";
export * from "./relativeDateToFixed";
export * from "./renderMoodEmoji";
export * from "./safePercentage";
export * from "./formatTable";
5 changes: 4 additions & 1 deletion packages/nunjucks-utils/src/registerFilters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Environment } from "nunjucks";
import { map, orderBy, take, uniqBy, get, partial } from "lodash";
import { map, orderBy, take, uniqBy, get, partial, groupBy } from "lodash";
import {
calculateProperty,
filterBy,
Expand All @@ -8,6 +8,7 @@ import {
formatNumber,
formatObject,
formatString,
formatTable,
rejectBy,
relativeDateRangeToFixed,
relativeDateRangeToFixedArray,
Expand Down Expand Up @@ -36,6 +37,8 @@ export function registerFilters(nj: Environment): Environment {
nj.addFilter("take", take);
nj.addFilter("orderBy", orderBy);
nj.addFilter("uniqBy", uniqBy);
nj.addFilter("groupBy", groupBy);
nj.addFilter("formatTable", formatTable);

return nj;
}
Loading