Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
JonDotsoy committed Oct 12, 2022
0 parents commit c52bd93
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# ndate script

Date format to console.

```shell
Usage: ndate [-] [--zero] [--date-style <full|long|medium|short>] [--time-style <full|long|medium|short>]
[--local <locale>] [--json] [--date <date>] [--help] [-j] [-d <date>]
[-l <locale>] [-z] [-h]
```

Please install with deno:

```shell
deno install ndate.ts
```

Samples use:

```shell
ndate # Tuesday, October 11, 2022 at 5:59:37 PM Chile Summer Time
ndate --local es-CL # martes, 11 de octubre de 2022, 17:59:14 hora de verano de Chile
ndate --local en-CL # Tuesday, October 11, 2022 at 5:59:37 PM Chile Summer Time
```
116 changes: 116 additions & 0 deletions ndate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
const dateStyles = ["full", "long", "medium", "short"] as const;
const timeStyles = ["full", "long", "medium", "short"] as const;
type DateStyle = (typeof dateStyles)[number];
type TimeStyle = (typeof dateStyles)[number];
const toDateStyle = (v: unknown): DateStyle | undefined => typeof v === "string" ? dateStyles.find(dateStyle => dateStyle.startsWith(v)) : undefined;
const toTimeStyle = (v: unknown): DateStyle | undefined => typeof v === "string" ? timeStyles.find(timeStyle => timeStyle.startsWith(v)) : undefined;
const dateParse = (val: string): Date => {
const d = new Date(val);
if (Number.isNaN(Number(d))) {
throw new Error(`the ${Deno.inspect(val)} time stamp no valid`);
}
return d;
}

let dateStyle: DateStyle = 'full';
let timeStyle: TimeStyle = 'full';
let newLine = true;
let local: string | undefined;
let date = new Date();
let json = false;
let stdinRedeable = false;
let showHelp = false;

const transformOptions: Record<string, (next: () => string | undefined) => void> = {
'-': () => { stdinRedeable = true },
'--zero': () => { newLine = false; },
'--date-style': (next) => { dateStyle = toDateStyle(next()) ?? 'full' },
'--time-style': (next) => { timeStyle = toTimeStyle(next()) ?? 'full' },
'--local': (next) => { local = next() },
'--json': () => { json = true },
'--date': (next) => {
const v = next();
if (v) {
date = dateParse(v);
}
},
'--help': () => { showHelp = true },
get "-j"() { return this['--json'] },
get "-d"() { return this["--date"] },
get "-l"() { return this["--local"] },
get "-z"() { return this["--zero"] },
get "-h"() { return this["--help"] },
};

const optionsLabels: Record<string, undefined | { label?: string }> = {
'--date-style': { label: `<${dateStyles.join('|')}>` },
'--time-style': { label: `<${timeStyles.join('|')}>` },
'--local': { label: `<locale>` },
'--date': { label: `<date>` },
get "-j"() { return this['--json'] },
get "-d"() { return this["--date"] },
get "-l"() { return this["--local"] },
get "-z"() { return this["--zero"] },
get "-h"() { return this["--help"] },
};

for (
let argsCursor = Deno.args[Symbol.iterator](),
{ value: arg, done } = argsCursor.next();
!done;
{ value: arg, done } = argsCursor.next()
) {
const next = (): string | undefined => argsCursor.next().value;
if (typeof arg === "string" && arg in transformOptions) { transformOptions[arg](next); }
}

const run = async () => {
if (showHelp) {
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) {
console.log(line);
}

return;
}

if (stdinRedeable) {
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 dateStr = json ? date.toJSON() : date.toLocaleString(local, { dateStyle, timeStyle });

Deno.stdout.write(new TextEncoder().encode(dateStr))

if (newLine) Deno.stdout.write(
new TextEncoder().encode(`\n`)
)
}

await run();

0 comments on commit c52bd93

Please sign in to comment.