diff --git a/__snapshots__/ndate.spec.ts.snap b/__snapshots__/ndate.spec.ts.snap index d02ac0f..ceef966 100644 --- a/__snapshots__/ndate.spec.ts.snap +++ b/__snapshots__/ndate.spec.ts.snap @@ -11,4 +11,106 @@ snapshot[`show help 1`] = ` " `; -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 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"`; diff --git a/ndate.spec.ts b/ndate.spec.ts index b53c9ff..686c464 100644 --- a/ndate.spec.ts +++ b/ndate.spec.ts @@ -1,15 +1,21 @@ import { assertSnapshot } from "https://deno.land/std@0.195.0/testing/snapshot.ts"; import { handler } from "./handler.ts"; -const joinUint8ArrayGenerator = async ( - generator: AsyncGenerator, -) => { - let buffEnd = new Uint8Array(); - for await (const line of generator) { - buffEnd = new Uint8Array([...buffEnd, ...line]); +const ArrayFromAsync = async (iterator: AsyncIterable, mapFn?: (element: T, index: number) => R, thisArg?: any): Promise => { + const items: R[] = [] + let index = -1 + for await (const item of iterator) { + index = index + 1 + items.push(mapFn?.call(thisArg, item, index) ?? item as R) } - return buffEnd; -}; + return items +} + +const joinUint8ArrayGenerator = async ( + generator: AsyncIterable, +) => new Uint8Array( + (await ArrayFromAsync(generator, e => [...e])).flat() +); Deno.test("show help", async (t) => { await assertSnapshot( @@ -27,8 +33,195 @@ Deno.test("render date", async (t) => { t, new TextDecoder().decode( await joinUint8ArrayGenerator( - handler(["-z", "-d", "2023-12-13 00:00","-l","es-cl",'-tz',"America/Santiago"]), + handler(["-z", "-d", "2023-12-13 00:00", "-l", "es-cl", '-tz', "America/Santiago"]), ), ), ); }); + +Deno.test("format json", async (t) => { + await assertSnapshot( + t, + new TextDecoder().decode( + await joinUint8ArrayGenerator( + handler([ + "-z", + "-d", + "2023-08-08T03:38:58.570Z", + "-l", + "es-cl", + '-tz', + "America/Santiago", + '--json' + ]), + ), + ), + ); +}) + +Deno.test("format utc", async (t) => { + await assertSnapshot( + t, + new TextDecoder().decode( + await joinUint8ArrayGenerator( + handler([ + "-z", + "-d", + "2023-08-08T03:38:58.570Z", + "-l", + "es-cl", + '-tz', + "America/Santiago", + '--utc' + ]), + ), + ), + ); +}) + +Deno.test("format epoch", async (t) => { + await assertSnapshot( + t, + new TextDecoder().decode( + await joinUint8ArrayGenerator( + handler([ + "-z", + "-d", + "2023-08-08T03:38:58.570Z", + "-l", + "es-cl", + '-tz', + "America/Santiago", + '--epoch' + ]), + ), + ), + ); +}) + + +Deno.test("format epoch ms", async (t) => { + await assertSnapshot( + t, + new TextDecoder().decode( + await joinUint8ArrayGenerator( + handler([ + "-z", + "-d", + "2023-08-08T03:38:58.570Z", + "-l", + "es-cl", + '-tz', + "America/Santiago", + '--epoch-ms' + ]), + ), + ), + ); +}) + + +Deno.test("format template", async (t) => { + await assertSnapshot( + t, + new TextDecoder().decode( + await joinUint8ArrayGenerator( + handler([ + "-z", + "-d", + "2023-08-08T03:38:58.570Z", + "-l", + "es-cl", + '-tz', + "America/Santiago", + '--template', + 'epoch:{{epoch}}\nepoch_ms:{{epoch_ms}}\njson:{{json}}\niso:{{iso}}\niso8601:{{iso8601}}\nutc:{{utc}}\nrfc7231:{{rfc7231}}\nlocal:{{local}}\ntime:{{time}}\nYYYY:{{YYYY}}\nMM:{{MM}}\nDD:{{DD}}\nhh:{{hh}}\nmm:{{mm}}\nss:{{ss}}\nms:{{ms}}\nfull_year:{{full_year}}\nmonth:{{month}}\ndate:{{date}}\nday:{{day}}\nhours:{{hours}}\nminutes:{{minutes}}\nseconds:{{seconds}}\nmilliseconds:{{milliseconds}}\ntimezone_offset:{{timezone_offset}}\nutc_full_year:{{utc_full_year}}\nutc_month:{{utc_month}}\nutc_date:{{utc_date}}\nutc_day:{{utc_day}}\nutc_hours:{{utc_hours}}\nutc_minutes:{{utc_minutes}}\nutc_seconds:{{utc_seconds}}\nutc_milliseconds:{{utc_milliseconds}}\nlocal_weekday:{{local_weekday}}\nlocal_literal:{{local_literal}}\nlocal_day:{{local_day}}\nlocal_month:{{local_month}}\nlocal_year:{{local_year}}\nlocal_hour:{{local_hour}}\nlocal_minute:{{local_minute}}\nlocal_second:{{local_second}}\nlocal_timeZoneName:{{local_timeZoneName}}' + ]), + ), + ), + ); +}) + +Deno.test("format template with transform", async (t) => { + await assertSnapshot( + t, + new TextDecoder().decode( + await joinUint8ArrayGenerator( + handler([ + "-z", + "-d", + "2023-08-08T03:38:58.570Z", + "-l", + "es-cl", + '-tz', + "America/Santiago", + '--template', + '{{rfc7231:json}}' + ]), + ), + ), + ); +}) + +Deno.test("format template with transform", async (t) => { + await assertSnapshot( + t, + new TextDecoder().decode( + await joinUint8ArrayGenerator( + handler([ + "-z", + "-d", + "2023-08-08T03:38:58.570Z", + "-l", + "es-cl", + '-tz', + "America/Santiago", + '--template', + '{{rfc7231:sub:0:3}}' + ]), + ), + ), + ); +}) + +Deno.test("format template with transform", async (t) => { + await assertSnapshot( + t, + new TextDecoder().decode( + await joinUint8ArrayGenerator( + handler([ + "-z", + "-d", + "2023-08-08T03:38:58.570Z", + "-l", + "es-cl", + '-tz', + "America/Santiago", + '--template', + '{{month:padStart:4:0}}' + ]), + ), + ), + ); +}) + +Deno.test("format template with transform", async (t) => { + await assertSnapshot( + t, + new TextDecoder().decode( + await joinUint8ArrayGenerator( + handler([ + "-z", + "-d", + "2023-08-08T03:38:58.570Z", + "-l", + "es-cl", + '-tz', + "America/Santiago", + '--template', + '{{month:padEnd:4:0}}' + ]), + ), + ), + ); +})