-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.ts
129 lines (115 loc) · 3.13 KB
/
handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import {
dateParse,
toDateStyle,
toHourCycle,
toTimeStyle,
} from "./lib/common.ts";
import { renderDateTemplate } from "./lib/renderDateTemplate.ts";
import { makeFlags } from "./makeFlags.ts";
const renderSheet = (date: Date) => {
const year = date.getFullYear();
const month = date.getMonth() + 1;
const dayOfMonth = date.getDate();
const hours = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
return `=DATE(${year};${month};${dayOfMonth})+TIME(${hours};${minutes};${seconds})`;
};
const makeHelpDialog = function* (
transformOptions: Record<
string,
unknown
>,
optionsLabels: Record<
string,
{
label?: string | undefined;
} | undefined
>,
) {
const items = Object.keys(transformOptions)
.map((item) => {
const label = optionsLabels[item]?.label;
return label ? `[${item} ${label}]` : `[${item}]`;
});
const textUsage = `Usage: ndate`;
const lines: string[] = [];
let currentLine: string | undefined;
for (const item of items) {
if (!currentLine) {
currentLine = lines.length
? `${" ".repeat(textUsage.length)}`
: `${textUsage}`;
}
currentLine = `${currentLine} ${item}`;
if (currentLine.length > 80) {
lines.push(currentLine);
currentLine = undefined;
}
}
if (currentLine) lines.push(currentLine);
for (const line of lines) {
yield new TextEncoder().encode(line);
yield new TextEncoder().encode("\n");
}
};
export const handler = async function* (
args: string[],
): AsyncGenerator<Uint8Array> {
let {
hourCycle,
dateStyle,
timeStyle,
insertFinalNewLine,
local,
timeZone,
date,
outputAsEpoch,
outputAsEpochMS,
outputAsJSON,
outputAsUTC,
stdinReadable,
showHelp,
template,
transformOptions,
outputAsSheet,
optionsLabels,
} = makeFlags(args);
if (showHelp) {
yield* makeHelpDialog(transformOptions, optionsLabels);
return;
}
if (timeZone) Deno.env.set(`TZ`, timeZone);
if (local) Deno.env.set(`LANG`, local);
if (stdinReadable) {
const buff = new Uint8Array(256);
await Deno.stdin.read(buff);
const text = new TextDecoder().decode(
buff.subarray(0, buff.findIndex((p) => p === 0)),
).trim();
date = dateParse(text);
}
const toOutput = () => {
if (template) {
return renderDateTemplate(template, date, local, {
dateStyle: toDateStyle(dateStyle),
timeStyle: toTimeStyle(timeStyle),
timeZone,
hourCycle: toHourCycle(hourCycle),
});
}
if (outputAsEpochMS) return Math.floor(date.getTime()).toString();
if (outputAsEpoch) return Math.floor(date.getTime() / 1000).toString();
if (outputAsJSON) return date.toJSON();
if (outputAsUTC) return date.toUTCString();
if (outputAsSheet) return renderSheet(date);
return date.toLocaleString(local, {
dateStyle: toDateStyle(dateStyle),
timeStyle: toTimeStyle(timeStyle),
timeZone,
hourCycle: toHourCycle(hourCycle),
});
};
yield new TextEncoder().encode(toOutput());
if (insertFinalNewLine) yield new TextEncoder().encode(`\n`);
};