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

Strategy Help Message #17

Closed
wants to merge 10 commits into from
40 changes: 40 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
# Changelog

## 0.4.0-morgan (2023-10-28)


### ⚠ BREAKING CHANGES

* configure strategy release by release name

### Features

* add execution header on ndate ([4b1ba2f](https://github.com/JonDotsoy/ndate/commit/4b1ba2fa5f22330f264046f046aacec40ebc531c))
* Add flag --epoch, --epoch-ms and --time-zone ([c49ceac](https://github.com/JonDotsoy/ndate/commit/c49ceace5cb14d9c2ea3f836dc73d7c31d88a738))
* add flag `--template` ([29fa11e](https://github.com/JonDotsoy/ndate/commit/29fa11e014e11bfd2f4e890906c6834fad47d127))
* add new flags to the template ([b85b5c9](https://github.com/JonDotsoy/ndate/commit/b85b5c9e7b43f6dab427912a578e006b4f98ae99))
* add support orender on sheet date ([a83a598](https://github.com/JonDotsoy/ndate/commit/a83a598f2d11d7d02b7c10741011a0d48cbada0a))
* configure executable script ([c6e5ae1](https://github.com/JonDotsoy/ndate/commit/c6e5ae12ce7d8debc762f721f0749658710104d8))
* configure utc flag ([42e3ade](https://github.com/JonDotsoy/ndate/commit/42e3adece8f984f4e8423338aef625b9c186bb34))
* initial commit ([c52bd93](https://github.com/JonDotsoy/ndate/commit/c52bd93256a4124e7399204ff67a9c0145d5f578))
* initialize version ([d59c863](https://github.com/JonDotsoy/ndate/commit/d59c86395e428ee0b072877943ae7036581593dd))
* rename main function per setup ([b4d8675](https://github.com/JonDotsoy/ndate/commit/b4d86755ce7cd38526bd90c723a6720d9cb9813d))
* upgrade ndate command ([4c198f1](https://github.com/JonDotsoy/ndate/commit/4c198f1fef1bb02dbe1676eeec93d28d2ad0075e))
* upgrade template and permisions ([c9e767a](https://github.com/JonDotsoy/ndate/commit/c9e767ae35860b8b001f22337f7e997701995afe))


### Bug Fixes

* alias template to Hour, Minute, Second and Millisecond. ([b7a87d7](https://github.com/JonDotsoy/ndate/commit/b7a87d7118f86b1467df3a472f9746a3d4ea655f))
* fix the padding start of the `{{ms}}` flag with a length of 3 ([a1733cf](https://github.com/JonDotsoy/ndate/commit/a1733cf52e99c8e5972f4244f651545d9047852e))
* upgrade values to and ([8896c75](https://github.com/JonDotsoy/ndate/commit/8896c75ff36e52e315c75d5d89e0fa62f6a3eb5e))


### Miscellaneous Chores

* release 0.4.0-alpha ([8d7039e](https://github.com/JonDotsoy/ndate/commit/8d7039e668904bc04a5710ff38636848776a6c66))
* release 0.4.0-morgan ([72a7460](https://github.com/JonDotsoy/ndate/commit/72a746054195df482f745f85351b5e0260908ce0))


### Build System

* configure strategy release by release name ([385d77d](https://github.com/JonDotsoy/ndate/commit/385d77d3b5527ff03efe033758dff23705e65d0f))

## [0.3.0](https://github.com/JonDotsoy/ndate/compare/v0.2.9...v0.3.0) (2023-07-22)


Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ Date format to console.
```shell
Usage: ndate [-] [--zero] [--date-style <full|long|medium|short|none>] [--time-style <full|long|medium|short|none>]
[--hour-cycles <h11|h12|h23|h24|none>] [--time-zone <time-zone>] [--local <locale>]
[--template <template>] [--json] [--utc] [--epoch] [--epoch-ms] [--date <date>]
[--help] [-j] [-d <date>] [-l <locale>] [-tz <time-zone>] [-z] [-h]
[--template <template>] [--json] [--sheet] [--utc] [--epoch] [--epoch-ms]
[--date <date>] [--help] [-j] [-d <date>] [-l <locale>] [-tz <time-zone>]
[-z] [-h]
```

Install with [homebrew](https://brew.sh/):

```shell
brew install jondotsoy/ndate/ndate
brew install jondotsoy/ndate/ndate@0.4
```

Or install with deno:

```shell
deno install --allow-env=TZ,LANG https://deno.land/x/ndate/ndate.ts
deno install --allow-env=TZ,LANG https://deno.land/x/ndate@morgan-v0.4.0-morgan/ndate.ts
```

Samples use:
Expand All @@ -38,6 +39,7 @@ ndate --epoch-ms # 1689255823663
ndate --epoch-ms -z # 1689255823663%
ndate --template '{{utc_full_year}}-{{utc_month:padStart:2:0}}-{{utc_date:padStart:2:0}}' # 2023-06-13
ndate --template '{{YYYY}}{{MM}}{{DD}}{{HH}}{{MM}}{{SS}}{{MS}}' # 202330142030590781
ndate --sheet # =DATE(2023;10;28)+TIME(1;17;12)
```

## Options to `--template`
Expand Down
112 changes: 109 additions & 3 deletions __snapshots__/ndate.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,116 @@ snapshot[`show help 1`] = `
" +
" [--hour-cycles <h11|h12|h23|h24|none>] [--time-zone <time-zone>] [--local <locale>]
" +
" [--template <template>] [--json] [--utc] [--epoch] [--epoch-ms] [--date <date>]
" [--template <template>] [--json] [--sheet] [--utc] [--epoch] [--epoch-ms]
" +
" [--help] [-j] [-d <date>] [-l <locale>] [-tz <time-zone>] [-z] [-h]
" [--date <date>] [--help] [-j] [-d <date>] [-l <locale>] [-tz <time-zone>]
" +
" [-z] [-h]
"
`;

snapshot[`render date 1`] = `"miércoles, 13 de diciembre de 2023, 00:00:00 hora de verano de Chile"`;
snapshot[`render date 1`] = `"miércoles, 13 de diciembre de 2023, 01:00:00 hora de verano de Chile"`;

snapshot[`format json 1`] = `"2023-08-08T03:38:58.570Z"`;

snapshot[`format utc 1`] = `"Tue, 08 Aug 2023 03:38:58 GMT"`;

snapshot[`format epoch 1`] = `"1691465938"`;

snapshot[`format epoch ms 1`] = `"1691465938570"`;

snapshot[`format sheet 1`] = `"=DATE(2023;8;7)+TIME(23;38;58)"`;

snapshot[`format template 1`] = `
"epoch:1691465938
" +
"epoch_ms:1691465938570
" +
"json:2023-08-08T03:38:58.570Z
" +
"iso:2023-08-08T03:38:58.570Z
" +
"iso8601:2023-08-08T03:38:58.570Z
" +
"utc:Tue, 08 Aug 2023 03:38:58 GMT
" +
"rfc7231:Tue, 08 Aug 2023 03:38:58 GMT
" +
"local:lunes, 7 de agosto de 2023, 23:38:58 hora estándar de Chile
" +
"time:1691465938570
" +
"YYYY:2023
" +
"MM:08
" +
"DD:07
" +
"hh:23
" +
"mm:38
" +
"ss:58
" +
"ms:570
" +
"full_year:2023
" +
"month:8
" +
"date:7
" +
"day:1
" +
"hours:23
" +
"minutes:38
" +
"seconds:58
" +
"milliseconds:570
" +
"timezone_offset:240
" +
"utc_full_year:2023
" +
"utc_month:8
" +
"utc_date:8
" +
"utc_day:2
" +
"utc_hours:3
" +
"utc_minutes:38
" +
"utc_seconds:58
" +
"utc_milliseconds:570
" +
"local_weekday:lunes
" +
"local_literal:
" +
"local_day:7
" +
"local_month:agosto
" +
"local_year:2023
" +
"local_hour:23
" +
"local_minute:38
" +
"local_second:58
" +
"local_timeZoneName:hora estándar de Chile"
`;

snapshot[`format template with transform 1`] = `'"Tue, 08 Aug 2023 03:38:58 GMT"'`;

snapshot[`format template with transform 2`] = `"Tue"`;

snapshot[`format template with transform 3`] = `"0008"`;

snapshot[`format template with transform 4`] = `"8000"`;
147 changes: 103 additions & 44 deletions handler.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,129 @@
import {
toDateStyle,
toTimeStyle,
dateParse,
toHourCycle,
} from "./lib/common.ts";
import {
dateParse,
toDateStyle,
toHourCycle,
toTimeStyle,
} from "./lib/common.ts";
import { renderDateTemplate } from "./lib/renderDateTemplate.ts";
import { makeFlags } from "./makeFlags.ts";

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, optionsLabels,
} = makeFlags(args);
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})`;
};

if(showHelp) {
const items = Object.keys(transformOptions)
.map(item => {
const label = optionsLabels[item]?.label;
return label ? `[${item} ${label}]` : `[${item}]`;
});
const makeHelpDialog = function* (
transformOptions: Record<
string,
(nextArgument: () => string | undefined) => void
>,
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 textUsage = `Usage: ndate`;

const lines: string[] = [];
let currentLine: string | undefined;
for(const item of items) {
if(!currentLine) {
currentLine = lines.length ? `${' '.repeat(textUsage.length)}` : `${textUsage}`;
}
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;
}
currentLine = `${currentLine} ${item}`;
if (currentLine.length > 80) {
lines.push(currentLine);
currentLine = undefined;
}
}

if(currentLine) lines.push(currentLine);
if (currentLine) lines.push(currentLine);

for(const line of lines) {
yield new TextEncoder().encode(line);
yield new TextEncoder().encode('\n');
}
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 (timeZone) Deno.env.set(`TZ`, timeZone);
if (local) Deno.env.set(`LANG`, local);

if(stdinReadable) {
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();
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();
return date.toLocaleString(local, { dateStyle: toDateStyle(dateStyle), timeStyle: toTimeStyle(timeStyle), timeZone, hourCycle: toHourCycle(hourCycle) });
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`);
if (insertFinalNewLine) yield new TextEncoder().encode(`\n`);
};
3 changes: 3 additions & 0 deletions makeFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function makeFlags(args: string[]) {
let outputAsEpochMS = false;
let outputAsJSON = false;
let outputAsUTC = false;
let outputAsSheet = false;
let stdinReadable = false;
let showHelp = false;
let template: string | undefined;
Expand All @@ -37,6 +38,7 @@ export function makeFlags(args: string[]) {
'--local': (nextArgument) => { local = nextArgument(); },
'--template': (nextArgument) => { template = nextArgument(); },
'--json': () => { outputAsJSON = true; },
'--sheet': () => { outputAsSheet = true; },
'--utc': () => { outputAsUTC = true; },
'--epoch': () => { outputAsEpoch = true; },
'--epoch-ms': () => { outputAsEpochMS = true; },
Expand Down Expand Up @@ -99,6 +101,7 @@ export function makeFlags(args: string[]) {
outputAsEpochMS,
outputAsJSON,
outputAsUTC,
outputAsSheet,
stdinReadable,
showHelp,
template,
Expand Down
Loading