Skip to content

Commit

Permalink
Add date_modify filter
Browse files Browse the repository at this point in the history
  • Loading branch information
kepano committed Sep 17, 2024
1 parent e4d0091 commit e74acfd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ Filters allow you to modify variables in a template. Filters are applied to vari
- `date` converts a date to the specified format, [see reference](https://day.js.org/docs/en/display/format).
- `date:"YYYY-MM-DD"` converts a date to "YYYY-MM-DD".
- Use `date:("outputFormat", "inputFormat")` to specify the input format, e.g. `"12/01/2024"|date:("YYYY-MM-DD", "MM/DD/YYYY")` parses "12/01/2024" and returns `"2024-12-01"`.
- `date_modify` modifies a date by adding or subtracting a specified amount of time, [see reference](https://day.js.org/docs/en/manipulate/add).
- `"2024-12-01"|date_modify:"+1 year"` returns `"2025-12-01"`
- `"2024-12-01"|date_modify:"- 2 months"` returns `"2024-10-01"`
- `first` returns the first element of an array as a string.
- `["a","b","c"]|first` returns `"a"`.
- If the input is not an array, it returns the input unchanged.
Expand Down
4 changes: 3 additions & 1 deletion src/utils/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ import { wikilink } from './filters/wikilink';
import { template } from './filters/template';
import { map } from './filters/map';
import { strip_tags } from './filters/strip_tags';
import { date_modify } from './filters/date_modify';

export const filters: { [key: string]: FilterFunction } = {
blockquote,
camel,
capitalize,
callout,
date,
date_modify,
first,
footnote,
image,
Expand All @@ -55,7 +57,7 @@ export const filters: { [key: string]: FilterFunction } = {
split,
strip_attr,
strip_md,
stripmd: strip_md, // stripmd is an alias for strip_md
stripmd: strip_md, // an alias for strip_md
strip_tags,
table,
template,
Expand Down
37 changes: 37 additions & 0 deletions src/utils/filters/date_modify.ts
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');
};

0 comments on commit e74acfd

Please sign in to comment.