-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add date template helper functions
Signed-off-by: Manuel Ruck <[email protected]>
- Loading branch information
Showing
3 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright (C) 2018-2024 Garden Technologies, Inc. <[email protected]> | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import type { TemplateHelperFunction } from "./functions.js" | ||
import { joi } from "../config/common.js" | ||
import { format as formatFns, add } from "date-fns" | ||
|
||
export const dateHelperFunctionSpecs: TemplateHelperFunction[] = [ | ||
{ | ||
name: "formatDate", | ||
description: "Formats the given date using the specified format.", | ||
arguments: { | ||
date: joi.string().required().description("The date to format."), | ||
format: joi | ||
.string() | ||
.required() | ||
.description("The format to use. See https://date-fns.org/v2.21.1/docs/format for details."), | ||
}, | ||
outputSchema: joi.string(), | ||
exampleArguments: [ | ||
{ input: ["2021-01-01T00:00:00Z", "yyyy-MM-dd"], output: "2021-01-01", skipTest: true }, | ||
{ input: ["2021-01-01T00:00:00Z", "yyyy-MM-dd HH:mm:ss"], output: "2021-01-01 00:00:00", skipTest: true }, | ||
], | ||
fn: (date: Date, format: string) => { | ||
return formatFns(date, format) | ||
}, | ||
}, | ||
{ | ||
name: "modifyDate", | ||
description: "Modifies or sets a unit of the given date to the specified amount based on the mode.", | ||
arguments: { | ||
date: joi.string().required().description("The date to modify."), | ||
amount: joi.number().required().description("The amount to modify or set the date by."), | ||
unit: joi | ||
.string() | ||
.valid("years", "months", "weeks", "days", "hours", "minutes", "seconds", "milliseconds") | ||
.required() | ||
.description("The unit to modify or set."), | ||
mode: joi.string().valid("add", "set").description("Mode to either add or set the unit.").default("add"), | ||
}, | ||
outputSchema: joi.string(), | ||
exampleArguments: [ | ||
{ input: ["2021-01-01T00:00:00Z", 1, "days", "add"], output: "2021-01-02T00:00:00.000Z" }, | ||
{ input: ["2021-01-01T00:00:00Z", -1, "days", "add"], output: "2020-12-31T00:00:00.000Z" }, | ||
{ input: ["2021-01-01T00:00:00Z", 30, "seconds", "set"], output: "2021-01-01T00:00:30.000Z" }, | ||
], | ||
fn: (date: string, amount: number, unit: string, mode: string) => { | ||
const dateClone = new Date(date) | ||
switch (mode) { | ||
case "add": | ||
return add(dateClone, { [unit]: amount }).toISOString() | ||
case "set": | ||
switch (unit) { | ||
case "years": | ||
dateClone.setFullYear(amount) | ||
break | ||
case "months": | ||
dateClone.setMonth(amount) | ||
break | ||
case "days": | ||
dateClone.setDate(amount) | ||
break | ||
case "hours": | ||
dateClone.setHours(amount) | ||
break | ||
case "minutes": | ||
dateClone.setMinutes(amount) | ||
break | ||
case "seconds": | ||
dateClone.setSeconds(amount) | ||
break | ||
case "milliseconds": | ||
dateClone.setMilliseconds(amount) | ||
break | ||
default: | ||
throw new Error("Invalid unit") | ||
} | ||
return dateClone.toISOString() | ||
default: | ||
throw new Error("Invalid mode") | ||
} | ||
}, | ||
}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters