-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
43 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import dayjs from 'dayjs'; | ||
|
||
export const date_modify = (str: string, param?: string): string => { | ||
if (!param) { | ||
console.error('date_modify filter requires a parameter'); | ||
return str; | ||
} | ||
|
||
let date = dayjs(str); | ||
if (!date.isValid()) { | ||
console.error('Invalid date for date_modify filter:', str); | ||
return str; | ||
} | ||
|
||
// Remove any surrounding quotes and trim whitespace | ||
param = param.replace(/^["']|["']$/g, '').trim(); | ||
|
||
// Updated regex to allow for optional spaces and plural units | ||
const regex = /^([+-])\s*(\d+)\s*(\w+)s?$/; | ||
const match = param.match(regex); | ||
|
||
if (!match) { | ||
console.error('Invalid format for date_modify filter:', param); | ||
return str; | ||
} | ||
|
||
const [, operation, amount, unit] = match; | ||
const numericAmount = parseInt(amount, 10); | ||
|
||
if (operation === '+') { | ||
date = date.add(numericAmount, unit as any); | ||
} else { | ||
date = date.subtract(numericAmount, unit as any); | ||
} | ||
|
||
return date.format('YYYY-MM-DD'); | ||
}; |