Skip to content

Commit 5a458c6

Browse files
committed
Add formatting flag
Add formatting flag for output format.
1 parent 6f84219 commit 5a458c6

File tree

5 files changed

+78
-14
lines changed

5 files changed

+78
-14
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Datelta (Date + Delta = Datelta)
22

3-
CLI tool to diff dates.
3+
CLI tool to diff dates with millisecond precision and support for multiple
4+
output formats (e.g. json, yaml).
45

56
## Quick Start
67

@@ -52,3 +53,4 @@ datelta --start "mar 2006" | jq
5253
| ------------------- | -------------------------------- |
5354
| deno task install | Install datelta as an executable |
5455
| deno task uninstall | Uninstall datelta |
56+
| deno task dev | Start development mode |

deno.lock

Lines changed: 42 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { toUTC, floorDivMod, floorMod } from "./util.ts";
1+
import { floorDivMod, floorMod, toUTC } from "./util.ts";
22

33
export interface DateDiff {
44
years: number;

src/main.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
import { dateDiff } from "./lib.ts";
2-
import { parse } from "https://deno.land/std@0.202.0/flags/mod.ts";
2+
import { parseArgs } from "https://deno.land/std@0.217.0/cli/parse_args.ts";
3+
import * as YAML from "https://deno.land/std@0.217.0/yaml/mod.ts";
34

4-
const version = "v0.0.3";
5-
const help = `Usage: datelta ${version}
5+
const version = "v0.1.0";
6+
const help = `datelta ${version}
67
7-
Datelta calculates the difference between 2 dates.
8+
Datelta calculates the difference between 2 dates with millisecond precision.
89
910
Options:
1011
--start Start date (default: now).
1112
--end End date (default: now).
13+
--fmt Output format (default: json, options: json, yaml).
1214
-h, --help Print help.
1315
-V, --version Print version.`;
1416

1517
function main() {
16-
const flags = parse(Deno.args, {
18+
const flags = parseArgs(Deno.args, {
1719
boolean: ["help", "h", "version", "V"],
18-
string: ["start", "end"],
20+
string: ["start", "end", "fmt"],
1921
});
22+
const allowedFmts = ["json", "yaml"];
2023

2124
if (flags.help || flags.h) {
2225
return console.log(help);
@@ -32,12 +35,30 @@ function main() {
3235
endDate = new Date(flags.end);
3336
} else if (flags.start) {
3437
startDate = new Date(flags.start);
38+
} else {
39+
return console.log(help);
40+
}
41+
42+
const fmt = flags.fmt ?? "json";
43+
if (!allowedFmts.includes(fmt)) {
44+
return console.error(
45+
`invalid argument: fmt was "${fmt}" expected one of:`,
46+
allowedFmts,
47+
);
3548
}
3649

37-
const diff = dateDiff(startDate, endDate);
38-
const diffJSON = JSON.stringify(diff);
50+
const dd = { ...dateDiff(startDate, endDate) };
3951

40-
console.log(diffJSON);
52+
if (fmt === "json") {
53+
console.log(JSON.stringify(dd));
54+
} else if (fmt === "yaml") {
55+
console.log(YAML.stringify(dd));
56+
} else {
57+
return console.error(
58+
`invalid argument: fmt was "${fmt}" expected one of:`,
59+
allowedFmts,
60+
);
61+
}
4162
}
4263

4364
if (import.meta.main) {

src/util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ export function toUTC(date: Date): number {
66
date.getHours(),
77
date.getMinutes(),
88
date.getSeconds(),
9-
date.getMilliseconds()
9+
date.getMilliseconds(),
1010
);
1111
}
1212

1313
export function floorDivMod(
1414
n: number,
15-
m: number
15+
m: number,
1616
): [quotient: number, remainder: number] {
1717
const q = Math.floor(n / m);
1818
const r = n % m;

0 commit comments

Comments
 (0)