-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract table formatting (#103)
* refactor: extract table formatting function Extract logic for formatting packument for viewing into separate function. * refactor: extract helper type Extract type that represents the version-part of a packument. Shorter than always rewriting the properties manually. * refactor: extract module Extract table formatting logic to own module. This way other output formatting functions can be grouped together an reused.
- Loading branch information
1 parent
6fe0806
commit 1a049cf
Showing
3 changed files
with
71 additions
and
52 deletions.
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,45 @@ | ||
import Table from "cli-table"; | ||
import { | ||
tryGetLatestVersion, | ||
UnityPackument, | ||
VersionedPackument, | ||
} from "./types/packument"; | ||
import assert from "assert"; | ||
|
||
/** | ||
* A type describing the minimum required properties of a packument | ||
* for being able to be formatted as a table. | ||
*/ | ||
type TableablePackument = Pick<UnityPackument, "name" | "time" | "date"> & | ||
VersionedPackument; | ||
|
||
/** | ||
* Formats an array of packuments as a table. The table is returned | ||
* as a single string (Includes line breaks). | ||
* @param packuments The packuments | ||
*/ | ||
export function formatAsTable(packuments: TableablePackument[]): string { | ||
const table = new Table({ | ||
head: ["Name", "Version", "Date"], | ||
colWidths: [42, 20, 12], | ||
}); | ||
|
||
function getTableRow( | ||
packument: TableablePackument | ||
): [string, string, string] { | ||
const name = packument.name; | ||
const version = tryGetLatestVersion(packument); | ||
let date = ""; | ||
if (packument.time && packument.time.modified) | ||
date = packument.time.modified.split("T")[0]!; | ||
if (packument.date) { | ||
date = packument.date.toISOString().slice(0, 10); | ||
} | ||
assert(version !== undefined); | ||
return [name, version, date]; | ||
} | ||
|
||
const rows = packuments.map(getTableRow); | ||
rows.forEach((row) => table.push(row)); | ||
return table.toString(); | ||
} |
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