Skip to content

Commit f90809b

Browse files
committed
chore: deno fmt
1 parent 07dc5c9 commit f90809b

2 files changed

Lines changed: 127 additions & 100 deletions

File tree

src/Time.ts

Lines changed: 109 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -377,24 +377,27 @@ export const Time: TimeLanguage = createLanguage<TimeLanguage>({
377377
return any(str("/"), str(" "), str("-"), str("."));
378378
},
379379
PartialDateMonthYear(s) {
380-
return safe(map(
381-
any(
382-
seq(s.NumericMonth, s.DateSeparator, s.Year),
383-
seq(s.LiteralMonth, s.DateSeparator, s.Year),
380+
return safe(
381+
map(
382+
any(
383+
seq(s.NumericMonth, s.DateSeparator, s.Year),
384+
seq(s.LiteralMonth, s.DateSeparator, s.Year),
385+
),
386+
([month, separator, year], b, a) => {
387+
return time(
388+
{
389+
when: new Date(
390+
`01${separator}${month}${separator}${year}`,
391+
).toISOString(),
392+
grain: "day",
393+
},
394+
b,
395+
a,
396+
);
397+
},
384398
),
385-
([month, separator, year], b, a) => {
386-
return time(
387-
{
388-
when: new Date(
389-
`01${separator}${month}${separator}${year}`,
390-
).toISOString(),
391-
grain: "day",
392-
},
393-
b,
394-
a,
395-
);
396-
},
397-
), "valid date");
399+
"valid date",
400+
);
398401
},
399402
QualifiedDay(s) {
400403
return map(
@@ -435,82 +438,91 @@ export const Time: TimeLanguage = createLanguage<TimeLanguage>({
435438
);
436439
},
437440
PartialDateDayMonth(s) {
438-
return safe(map(
439-
seq(s.QualifiedDay, optional(__(str("of"))), s.LiteralMonth),
440-
([day, _of, month], b, a) => {
441-
const year = new Date().getFullYear();
442-
return time(
443-
{
444-
when: new Date(`${day} ${month} ${year}`).toISOString(),
445-
grain: "day",
446-
},
447-
b,
448-
a,
449-
);
450-
},
451-
), "valid date");
441+
return safe(
442+
map(
443+
seq(s.QualifiedDay, optional(__(str("of"))), s.LiteralMonth),
444+
([day, _of, month], b, a) => {
445+
const year = new Date().getFullYear();
446+
return time(
447+
{
448+
when: new Date(`${day} ${month} ${year}`).toISOString(),
449+
grain: "day",
450+
},
451+
b,
452+
a,
453+
);
454+
},
455+
),
456+
"valid date",
457+
);
452458
},
453459
FullDate(s) {
454460
return any(
455-
safe(map(
456-
seq(s.PartialDateDayMonth, space(), s.Year),
457-
([partialDayMonth, _sp, year], b, a) => {
458-
const original = partialDayMonth.value.when;
459-
if (typeof original !== "string") {
460-
throw new Error(`
461+
safe(
462+
map(
463+
seq(s.PartialDateDayMonth, space(), s.Year),
464+
([partialDayMonth, _sp, year], b, a) => {
465+
const original = partialDayMonth.value.when;
466+
if (typeof original !== "string") {
467+
throw new Error(`
461468
Unexpected partial date match:
462469
${JSON.stringify(partialDayMonth)}
463470
`);
464-
}
471+
}
465472

466-
const date = new Date(original);
467-
date.setFullYear(year);
473+
const date = new Date(original);
474+
date.setFullYear(year);
468475

469-
return time({ when: date.toISOString(), grain: "day" }, b, a);
470-
},
471-
), "valid date"),
472-
safe(map(
473-
any(
474-
seq(
475-
s.Day,
476-
s.DateSeparator,
477-
s.NumericMonth,
478-
s.DateSeparator,
479-
s.Year,
480-
),
481-
seq(
482-
s.Day,
483-
s.DateSeparator,
484-
s.LiteralMonth,
485-
s.DateSeparator,
486-
s.Year,
487-
),
488-
seq(
489-
s.NumericMonth,
490-
s.DateSeparator,
491-
s.Day,
492-
s.DateSeparator,
493-
s.Year,
494-
),
495-
seq(
496-
s.LiteralMonth,
497-
s.DateSeparator,
498-
s.Day,
499-
s.DateSeparator,
500-
s.Year,
476+
return time({ when: date.toISOString(), grain: "day" }, b, a);
477+
},
478+
),
479+
"valid date",
480+
),
481+
safe(
482+
map(
483+
any(
484+
seq(
485+
s.Day,
486+
s.DateSeparator,
487+
s.NumericMonth,
488+
s.DateSeparator,
489+
s.Year,
490+
),
491+
seq(
492+
s.Day,
493+
s.DateSeparator,
494+
s.LiteralMonth,
495+
s.DateSeparator,
496+
s.Year,
497+
),
498+
seq(
499+
s.NumericMonth,
500+
s.DateSeparator,
501+
s.Day,
502+
s.DateSeparator,
503+
s.Year,
504+
),
505+
seq(
506+
s.LiteralMonth,
507+
s.DateSeparator,
508+
s.Day,
509+
s.DateSeparator,
510+
s.Year,
511+
),
501512
),
513+
([first, s1, mid, s2, year], b, a) => {
514+
return time(
515+
{
516+
when: new Date(`${first}${s1}${mid}${s2}${year}`).toISOString(),
517+
grain: "day",
518+
},
519+
b,
520+
a,
521+
);
522+
},
502523
),
503-
([first, s1, mid, s2, year], b, a) => {
504-
return time(
505-
{
506-
when: new Date(`${first}${s1}${mid}${s2}${year}`).toISOString(),
507-
grain: "day",
508-
},
509-
b,
510-
a,
511-
);
512-
},
513-
), "valid date"),
524+
"valid date",
525+
),
514526
);
515527
},
516528
PartialDateMonthYearEra(s) {
@@ -582,17 +594,20 @@ export const Time: TimeLanguage = createLanguage<TimeLanguage>({
582594
s.GrainQuantity,
583595
s.UnspecifiedGrainAmount,
584596
s.YearEra,
585-
safe(map(s.LiteralMonth, (month, b, a) => {
586-
const year = new Date().getFullYear();
587-
return time(
588-
{
589-
when: new Date(`01 ${month} ${year}`).toISOString(),
590-
grain: "month",
591-
},
592-
b,
593-
a,
594-
);
595-
}), "valid date"),
597+
safe(
598+
map(s.LiteralMonth, (month, b, a) => {
599+
const year = new Date().getFullYear();
600+
return time(
601+
{
602+
when: new Date(`01 ${month} ${year}`).toISOString(),
603+
grain: "month",
604+
},
605+
b,
606+
a,
607+
);
608+
}),
609+
"valid date",
610+
),
596611
),
597612
);
598613
},

tests/Time.test.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,12 +413,19 @@ Deno.test("Circa time", () => {
413413
});
414414
Deno.test("FullDate: invalid date backtracks instead of throwing", () => {
415415
// 31st of February is not a real date — should not throw
416-
const res = Duckling([Time.parser]).extract("On 31/02/2024 something happened");
416+
const res = Duckling([Time.parser]).extract(
417+
"On 31/02/2024 something happened",
418+
);
417419
// Should not contain an entity for this invalid date
418420
for (const entity of res) {
419421
if (entity.kind === "time" && typeof entity.value.when === "string") {
420-
assertEquals(entity.value.when !== "Invalid Date", true,
421-
`Should not produce Invalid Date, got entity: ${JSON.stringify(entity)}`);
422+
assertEquals(
423+
entity.value.when !== "Invalid Date",
424+
true,
425+
`Should not produce Invalid Date, got entity: ${
426+
JSON.stringify(entity)
427+
}`,
428+
);
422429
}
423430
}
424431
});
@@ -444,9 +451,14 @@ Deno.test("FullDate: does not crash on nonsense date-like input", () => {
444451
// Just verify it doesn't throw and doesn't produce "Invalid Date"
445452
for (const entity of res) {
446453
if (entity.kind === "time" && typeof entity.value.when === "string") {
447-
assertEquals(entity.value.when !== "Invalid Date", true,
448-
`Should not produce Invalid Date for "${input}", got: ${JSON.stringify(entity)}`);
454+
assertEquals(
455+
entity.value.when !== "Invalid Date",
456+
true,
457+
`Should not produce Invalid Date for "${input}", got: ${
458+
JSON.stringify(entity)
459+
}`,
460+
);
449461
}
450462
}
451463
}
452-
});
464+
});

0 commit comments

Comments
 (0)