Skip to content

Commit

Permalink
feat: add dateTime helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinlee11 committed Oct 29, 2024
1 parent 094cf30 commit bad53dd
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Hide generated files from PRs
typedocs/* linguist-generated=true
typedocs/**/* linguist-generated=true
6 changes: 6 additions & 0 deletions src/helpers/datetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import {
DateFormats,
} from '../types/helpers/datetime';

export class DateTime {
dateFormats = DateFormats;

localizeDate = localizeDate;
}

/**
* Returns the time provided as a number
* e.g) "22:00:00" to 220000
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Location } from './helpers/location';
import { Customers } from './api/customers';
import { Item } from './helpers/item';
import { Money } from './helpers/money';
import { DateTime } from './helpers/datetime';

export interface InitConfig {
userId: number;
Expand All @@ -29,6 +30,7 @@ class SiteThemeSDK {
item: Item;
location: Location;
money: Money;
dateTime: DateTime;
};

constructor(initObj: InitConfig) {
Expand Down Expand Up @@ -56,7 +58,8 @@ class SiteThemeSDK {
this.helpers = {
item: new Item(),
location: new Location(),
money: new Money()
money: new Money(),
dateTime: new DateTime(),
};
}
}
Expand Down
47 changes: 47 additions & 0 deletions test/helpers.dateTime.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { describe, expect, it } from 'vitest';
import { getTestSiteThemeSDK } from './helpers';
import {
DateFormats,
} from '../src/types/helpers/datetime';

const sdk = getTestSiteThemeSDK();

describe('provideDateFormats', () => {
it('should provide date formats', () => {
expect(sdk.helpers.dateTime.dateFormats).toBe(DateFormats);
expect(sdk.helpers.dateTime.dateFormats.weekdaySyearNmonthSdayNhourNminuteN)
.toBe(DateFormats.weekdaySyearNmonthSdayNhourNminuteN);
});
});

describe('localizeDate', () => {
it('should localize date', () => {
const date = new Date('2024-10-01T08:00:00.000-04:00');
let localizedDate = sdk.helpers.dateTime.localizeDate(date, 'en-US', sdk.helpers.dateTime.dateFormats.weekdayShort, 'America/New_York');
expect(localizedDate).toBe('Tue');
localizedDate = sdk.helpers.dateTime.localizeDate(date, 'en-US', sdk.helpers.dateTime.dateFormats.weekdayLong, 'America/New_York');
expect(localizedDate).toBe('Tuesday');
localizedDate = sdk.helpers.dateTime.localizeDate(date, 'en-US', sdk.helpers.dateTime.dateFormats.hourNminuteN, 'America/New_York');
expect(localizedDate).toBe('8:00 AM');
localizedDate = sdk.helpers.dateTime.localizeDate(date, 'en-US', sdk.helpers.dateTime.dateFormats.hourNminuteNsecondN, 'America/New_York');
expect(localizedDate).toBe('8:00:00 AM');
localizedDate = sdk.helpers.dateTime.localizeDate(date, 'en-US', sdk.helpers.dateTime.dateFormats.yearNmonth2day2, 'America/New_York');
expect(localizedDate).toBe('10/01/2024');
localizedDate = sdk.helpers.dateTime.localizeDate(date, 'en-US', sdk.helpers.dateTime.dateFormats.yearNmonthNdayN, 'America/New_York');
expect(localizedDate).toBe('10/1/2024');
localizedDate = sdk.helpers.dateTime.localizeDate(date, 'en-US', sdk.helpers.dateTime.dateFormats.yearNmonthLdayN, 'America/New_York');
expect(localizedDate).toBe('October 1, 2024');
localizedDate = sdk.helpers.dateTime.localizeDate(date, 'en-US', sdk.helpers.dateTime.dateFormats.yearNmonthSdayN, 'America/New_York');
expect(localizedDate).toBe('Oct 1, 2024');
localizedDate = sdk.helpers.dateTime.localizeDate(date, 'en-US', sdk.helpers.dateTime.dateFormats.yearNmonthLdayNhourNminuteN, 'America/New_York');
expect(localizedDate).toBe('October 1, 2024 at 8:00 AM');
localizedDate = sdk.helpers.dateTime.localizeDate(date, 'en-US', sdk.helpers.dateTime.dateFormats.yearNmonthSdayNhourNminuteN, 'America/New_York');
expect(localizedDate).toBe('Oct 1, 2024, 8:00 AM');
localizedDate = sdk.helpers.dateTime.localizeDate(date, 'en-US', sdk.helpers.dateTime.dateFormats.weekdayLyearNmonthLdayNhourNminuteN, 'America/New_York');
expect(localizedDate).toBe('Tuesday, October 1, 2024 at 8:00 AM');
localizedDate = sdk.helpers.dateTime.localizeDate(date, 'en-US', sdk.helpers.dateTime.dateFormats.weekdaySyearNmonthSdayNhourNminuteN, 'America/New_York');
expect(localizedDate).toBe('Tue, Oct 1, 2024, 8:00 AM');
localizedDate = sdk.helpers.dateTime.localizeDate(date, 'en-US', sdk.helpers.dateTime.dateFormats.weekdayLhourNminuteN, 'America/New_York');
expect(localizedDate).toBe('Tuesday 8:00 AM');
});
});
117 changes: 117 additions & 0 deletions typedocs/classes/helpers_datetime.DateTime.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions typedocs/classes/index.default.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions typedocs/modules/helpers_datetime.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bad53dd

Please sign in to comment.