Skip to content

Commit

Permalink
refactor: separate modifyDate and shiftDate functions
Browse files Browse the repository at this point in the history
The hierarchical switch statements are hard to maintain.

The `mode` argument served as a routing param
to pick up necessary function behaviour.

It's better to get rid of it and to maintain 2 independent functions.
Each function can have its own limitations and time unit types.
  • Loading branch information
vvagaytsev committed May 14, 2024
1 parent 3aa1661 commit 1d52f3f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 46 deletions.
92 changes: 51 additions & 41 deletions core/src/template-string/date-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,59 +31,69 @@ export const dateHelperFunctionSpecs: TemplateHelperFunction[] = [
},
},
{
name: "modifyDate",
description: "Modifies or sets a unit of the given date to the specified amount based on the mode.",
name: "shiftDate",
description: "Shifts the date by the specified amount of time units.",
arguments: {
date: joi.string().required().description("The date to modify."),
amount: joi.number().required().description("The amount to modify or set the date by."),
date: joi.string().required().description("The date to shift."),
amount: joi.number().required().description("The amount of time units to shift 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"),
.description("The time unit to shift the date by."),
},
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" },
{ input: ["2021-01-01T00:00:00Z", 1, "days"], output: "2021-01-02T00:00:00.000Z" },
{ input: ["2021-01-01T00:00:00Z", -1, "days"], output: "2020-12-31T00:00:00.000Z" },
],
fn: (date: string, amount: number, unit: string, mode: string) => {
fn: (date: string, amount: number, unit: string) => {
const dateClone = new Date(date)
return add(dateClone, { [unit]: amount }).toISOString()
},
},
{
name: "modifyDate",
description: "Modifies the date by setting the specified amount of time units.",
arguments: {
date: joi.string().required().description("The date to modify."),
amount: joi.number().required().description("The amount of time units to set."),
unit: joi
.string()
.valid("years", "months", "weeks", "days", "hours", "minutes", "seconds", "milliseconds")
.required()
.description("The time unit to set."),
},
outputSchema: joi.string(),
exampleArguments: [{ input: ["2021-01-01T00:00:00Z", 30, "seconds"], output: "2021-01-01T00:00:30.000Z" }],
fn: (date: string, amount: number, unit: 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()
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 mode")
throw new Error("Invalid unit")
}
return dateClone.toISOString()
},
},
]
19 changes: 14 additions & 5 deletions docs/reference/template-strings/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,13 @@ Examples:

## modifyDate

Modifies or sets a unit of the given date to the specified amount based on the mode.
Modifies the date by setting the specified amount of time units.

Usage: `modifyDate(date, amount, unit, [mode])`
Usage: `modifyDate(date, amount, unit)`

Examples:

* `${modifyDate("2021-01-01T00:00:00Z", 1, "days", "add")}` -> `"2021-01-02T00:00:00.000Z"`
* `${modifyDate("2021-01-01T00:00:00Z", -1, "days", "add")}` -> `"2020-12-31T00:00:00.000Z"`
* `${modifyDate("2021-01-01T00:00:00Z", 30, "seconds", "set")}` -> `"2021-01-01T00:00:30.000Z"`
* `${modifyDate("2021-01-01T00:00:00Z", 30, "seconds")}` -> `"2021-01-01T00:00:30.000Z"`

## replace

Expand All @@ -178,6 +176,17 @@ Examples:

* `${sha256("Some String")}` -> `"7f0fd64653ba0bb1a579ced2b6bf375e916cc60662109ee0c0b24f0a750c3a6c"`

## shiftDate

Shifts the date by the specified amount of time units.

Usage: `shiftDate(date, amount, unit)`

Examples:

* `${shiftDate("2021-01-01T00:00:00Z", 1, "days")}` -> `"2021-01-02T00:00:00.000Z"`
* `${shiftDate("2021-01-01T00:00:00Z", -1, "days")}` -> `"2020-12-31T00:00:00.000Z"`

## slice

Slices a string or array at the specified start/end offsets. Note that you can use a negative number for the end offset to count backwards from the end.
Expand Down

0 comments on commit 1d52f3f

Please sign in to comment.