Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding in date parsing for the date helper #83

Merged
merged 1 commit into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@
"arr-flatten": "^1.1.0",
"array-sort": "^1.0.0",
"cacheable": "^1.8.5",
"chrono-node": "^2.7.7",
"create-frame": "^1.0.0",
"dayjs": "^1.11.13",
"define-property": "^2.0.2",
"ent": "^2.2.1",
"extend-shallow": "^3.0.2",
Expand Down
23 changes: 23 additions & 0 deletions src/helpers/date.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
import {parseDate} from 'chrono-node';
import dayjs from 'dayjs';
import {type Helper} from '../helper-registry.js';

const year = () => new Date().getFullYear().toString();

const date = (humanReadableDate: string, formatString: string): string => {
const parsedDate = parseDate(humanReadableDate);
if (!parsedDate) {
throw new Error(`Unable to parse date: ${humanReadableDate}`);
}

// Format the parsed date using `dayjs`
return dayjs(parsedDate).format(formatString);
};

export const helpers: Helper[] = [
{
name: 'year',
category: 'date',
fn: year,
},
{
name: 'date',
category: 'date',
fn: date,
},
{
/* Adding this in for legacy support */
name: 'moment',
category: 'date',
fn: date,
},
];
33 changes: 32 additions & 1 deletion test/helpers/date.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {describe, it, expect} from 'vitest';
import dayjs from 'dayjs';
import {parseDate} from 'chrono-node';
import {helpers} from '../../src/helpers/date.js';

describe('helpers', () => {
describe('year', () => {
it('should have a "year" helper with the correct properties', () => {
// Find the 'year' helper in the array
const yearHelper = helpers.find(helper => helper.name === 'year');
Expand All @@ -25,3 +27,32 @@ describe('helpers', () => {
expect(yearHelper?.fn()).toBe(currentYear);
});
});

describe('date', () => {
it('should correctly calculate and format "5 years ago"', () => {
const dateHelper = helpers.find(helper => helper.name === 'date');
expect(dateHelper).toBeDefined();

const formattedDate = dateHelper?.fn('5 years ago', 'YYYY');
const expectedDate = dayjs().subtract(5, 'years').format('YYYY');
expect(formattedDate).toBe(expectedDate);
});

it('should correctly calculate and format "next Friday"', () => {
const dateHelper = helpers.find(helper => helper.name === 'date');
expect(dateHelper).toBeDefined();

const formattedDate = dateHelper?.fn('next Friday', 'MM/DD/YYYY');
const expectedDate = dayjs(parseDate('next Friday')).format('MM/DD/YYYY');
expect(formattedDate).toBe(expectedDate);
});

it('should throw an error for an invalid date format', () => {
const dateHelper = helpers.find(helper => helper.name === 'date');
expect(dateHelper).toBeDefined();

expect(() => dateHelper?.fn('invalid date', 'YYYY')).toThrowError(
'Unable to parse date: invalid date',
);
});
});
Loading