From 1db486eb76e4b52f27602b0f2d221a40eef7a7e6 Mon Sep 17 00:00:00 2001 From: kevinlee11 Date: Tue, 29 Oct 2024 11:07:55 -0400 Subject: [PATCH] feat: add dateTime helpers (#20) --- .gitattributes | 2 +- src/helpers/datetime.ts | 6 + src/index.ts | 5 +- test/helpers.dateTime.test.ts | 47 +++++++ typedocs/classes/helpers_datetime.DateTime.md | 117 ++++++++++++++++++ typedocs/classes/index.default.md | 1 + typedocs/modules/helpers_datetime.md | 4 + 7 files changed, 180 insertions(+), 2 deletions(-) create mode 100644 test/helpers.dateTime.test.ts create mode 100644 typedocs/classes/helpers_datetime.DateTime.md diff --git a/.gitattributes b/.gitattributes index ac678de..40bdcc6 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,2 @@ # Hide generated files from PRs -typedocs/* linguist-generated=true +typedocs/**/* linguist-generated=true diff --git a/src/helpers/datetime.ts b/src/helpers/datetime.ts index b79ab58..c25f600 100644 --- a/src/helpers/datetime.ts +++ b/src/helpers/datetime.ts @@ -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 diff --git a/src/index.ts b/src/index.ts index dfdf15e..08ed32c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; @@ -29,6 +30,7 @@ class SiteThemeSDK { item: Item; location: Location; money: Money; + dateTime: DateTime; }; constructor(initObj: InitConfig) { @@ -56,7 +58,8 @@ class SiteThemeSDK { this.helpers = { item: new Item(), location: new Location(), - money: new Money() + money: new Money(), + dateTime: new DateTime(), }; } } diff --git a/test/helpers.dateTime.test.ts b/test/helpers.dateTime.test.ts new file mode 100644 index 0000000..35d0c47 --- /dev/null +++ b/test/helpers.dateTime.test.ts @@ -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'); + }); +}); \ No newline at end of file diff --git a/typedocs/classes/helpers_datetime.DateTime.md b/typedocs/classes/helpers_datetime.DateTime.md new file mode 100644 index 0000000..428e89c --- /dev/null +++ b/typedocs/classes/helpers_datetime.DateTime.md @@ -0,0 +1,117 @@ +[@square/site-theme-sdk](../GettingStarted.md) / [Modules](../modules.md) / [helpers/datetime](../modules/helpers_datetime.md) / DateTime + +# Class: DateTime + +[helpers/datetime](../modules/helpers_datetime.md).DateTime + +## Table of contents + +### Constructors + +- [constructor](helpers_datetime.DateTime.md#constructor) + +### Properties + +- [dateFormats](helpers_datetime.DateTime.md#dateformats) +- [localizeDate](helpers_datetime.DateTime.md#localizedate) + +## Constructors + +### constructor + +• **new DateTime**() + +## Properties + +### dateFormats + +• **dateFormats**: `Object` = `DateFormats` + +#### Type declaration + +| Name | Type | +| :------ | :------ | +| `weekdayShort` | { `weekday`: ``"short"`` = 'short' } | +| `weekdayShort.weekday` | ``"short"`` | +| `weekdayLong` | { `weekday`: ``"long"`` = 'long' } | +| `weekdayLong.weekday` | ``"long"`` | +| `hourNminuteN` | { `hour`: ``"numeric"`` = 'numeric'; `minute`: ``"numeric"`` = 'numeric' } | +| `hourNminuteN.hour` | ``"numeric"`` | +| `hourNminuteN.minute` | ``"numeric"`` | +| `hourNminuteNsecondN` | { `hour`: ``"numeric"`` = 'numeric'; `minute`: ``"numeric"`` = 'numeric'; `second`: ``"numeric"`` = 'numeric' } | +| `hourNminuteNsecondN.hour` | ``"numeric"`` | +| `hourNminuteNsecondN.minute` | ``"numeric"`` | +| `hourNminuteNsecondN.second` | ``"numeric"`` | +| `yearNmonth2day2` | { `year`: ``"numeric"`` = 'numeric'; `month`: ``"2-digit"`` = '2-digit'; `day`: ``"2-digit"`` = '2-digit' } | +| `yearNmonth2day2.year` | ``"numeric"`` | +| `yearNmonth2day2.month` | ``"2-digit"`` | +| `yearNmonth2day2.day` | ``"2-digit"`` | +| `yearNmonthNdayN` | { `year`: ``"numeric"`` = 'numeric'; `month`: ``"numeric"`` = 'numeric'; `day`: ``"numeric"`` = 'numeric' } | +| `yearNmonthNdayN.year` | ``"numeric"`` | +| `yearNmonthNdayN.month` | ``"numeric"`` | +| `yearNmonthNdayN.day` | ``"numeric"`` | +| `yearNmonthLdayN` | { `year`: ``"numeric"`` = 'numeric'; `month`: ``"long"`` = 'long'; `day`: ``"numeric"`` = 'numeric' } | +| `yearNmonthLdayN.year` | ``"numeric"`` | +| `yearNmonthLdayN.month` | ``"long"`` | +| `yearNmonthLdayN.day` | ``"numeric"`` | +| `yearNmonthSdayN` | { `year`: ``"numeric"`` = 'numeric'; `month`: ``"short"`` = 'short'; `day`: ``"numeric"`` = 'numeric' } | +| `yearNmonthSdayN.year` | ``"numeric"`` | +| `yearNmonthSdayN.month` | ``"short"`` | +| `yearNmonthSdayN.day` | ``"numeric"`` | +| `yearNmonthLdayNhourNminuteN` | { `year`: ``"numeric"`` = 'numeric'; `month`: ``"long"`` = 'long'; `day`: ``"numeric"`` = 'numeric'; `hour`: ``"numeric"`` = 'numeric'; `minute`: ``"numeric"`` = 'numeric' } | +| `yearNmonthLdayNhourNminuteN.year` | ``"numeric"`` | +| `yearNmonthLdayNhourNminuteN.month` | ``"long"`` | +| `yearNmonthLdayNhourNminuteN.day` | ``"numeric"`` | +| `yearNmonthLdayNhourNminuteN.hour` | ``"numeric"`` | +| `yearNmonthLdayNhourNminuteN.minute` | ``"numeric"`` | +| `yearNmonthSdayNhourNminuteN` | { `year`: ``"numeric"`` = 'numeric'; `month`: ``"short"`` = 'short'; `day`: ``"numeric"`` = 'numeric'; `hour`: ``"numeric"`` = 'numeric'; `minute`: ``"numeric"`` = 'numeric' } | +| `yearNmonthSdayNhourNminuteN.year` | ``"numeric"`` | +| `yearNmonthSdayNhourNminuteN.month` | ``"short"`` | +| `yearNmonthSdayNhourNminuteN.day` | ``"numeric"`` | +| `yearNmonthSdayNhourNminuteN.hour` | ``"numeric"`` | +| `yearNmonthSdayNhourNminuteN.minute` | ``"numeric"`` | +| `weekdayLyearNmonthLdayNhourNminuteN` | { `weekday`: ``"long"`` = 'long'; `year`: ``"numeric"`` = 'numeric'; `month`: ``"long"`` = 'long'; `day`: ``"numeric"`` = 'numeric'; `hour`: ``"numeric"`` = 'numeric'; `minute`: ``"numeric"`` = 'numeric' } | +| `weekdayLyearNmonthLdayNhourNminuteN.weekday` | ``"long"`` | +| `weekdayLyearNmonthLdayNhourNminuteN.year` | ``"numeric"`` | +| `weekdayLyearNmonthLdayNhourNminuteN.month` | ``"long"`` | +| `weekdayLyearNmonthLdayNhourNminuteN.day` | ``"numeric"`` | +| `weekdayLyearNmonthLdayNhourNminuteN.hour` | ``"numeric"`` | +| `weekdayLyearNmonthLdayNhourNminuteN.minute` | ``"numeric"`` | +| `weekdaySyearNmonthSdayNhourNminuteN` | { `weekday`: ``"short"`` = 'short'; `year`: ``"numeric"`` = 'numeric'; `month`: ``"short"`` = 'short'; `day`: ``"numeric"`` = 'numeric'; `hour`: ``"numeric"`` = 'numeric'; `minute`: ``"numeric"`` = 'numeric' } | +| `weekdaySyearNmonthSdayNhourNminuteN.weekday` | ``"short"`` | +| `weekdaySyearNmonthSdayNhourNminuteN.year` | ``"numeric"`` | +| `weekdaySyearNmonthSdayNhourNminuteN.month` | ``"short"`` | +| `weekdaySyearNmonthSdayNhourNminuteN.day` | ``"numeric"`` | +| `weekdaySyearNmonthSdayNhourNminuteN.hour` | ``"numeric"`` | +| `weekdaySyearNmonthSdayNhourNminuteN.minute` | ``"numeric"`` | +| `weekdayLhourNminuteN` | { `weekday`: ``"long"`` = 'long'; `hour`: ``"numeric"`` = 'numeric'; `minute`: ``"numeric"`` = 'numeric' } | +| `weekdayLhourNminuteN.weekday` | ``"long"`` | +| `weekdayLhourNminuteN.hour` | ``"numeric"`` | +| `weekdayLhourNminuteN.minute` | ``"numeric"`` | + +___ + +### localizeDate + +• **localizeDate**: (`dateObj`: `Date`, `locale`: `string`, `format`: [`localizeDateFormats`](../modules/types_helpers_datetime.md#localizedateformats), `timezone`: `string`, `hour12?`: `boolean`) => `string` = `localizeDate` + +#### Type declaration + +▸ (`dateObj`, `locale`, `format`, `timezone`, `hour12?`): `string` + +Transform native JS date objects into a localized string, based on the locale and the format +specified. + +##### Parameters + +| Name | Type | Description | +| :------ | :------ | :------ | +| `dateObj` | `Date` | Date object to be localized | +| `locale` | `string` | Hyphenized language/country locale combo, e.g. en-US (BCP 47). | +| `format` | [`localizeDateFormats`](../modules/types_helpers_datetime.md#localizedateformats) | Options object specifying the date/time parts to be included in the formatted string | +| `timezone` | `string` | string specifying a timezone offset the time into | +| `hour12?` | `boolean` | boolean specifying whether to use 12-hour time format | + +##### Returns + +`string` diff --git a/typedocs/classes/index.default.md b/typedocs/classes/index.default.md index 7123035..7ae71ac 100644 --- a/typedocs/classes/index.default.md +++ b/typedocs/classes/index.default.md @@ -88,3 +88,4 @@ ___ | `item` | [`Item`](helpers_item.Item.md) | | `location` | [`Location`](helpers_location.Location.md) | | `money` | [`Money`](helpers_money.Money.md) | +| `dateTime` | [`DateTime`](helpers_datetime.DateTime.md) | diff --git a/typedocs/modules/helpers_datetime.md b/typedocs/modules/helpers_datetime.md index 0600e23..4b1cd34 100644 --- a/typedocs/modules/helpers_datetime.md +++ b/typedocs/modules/helpers_datetime.md @@ -4,6 +4,10 @@ ## Table of contents +### Classes + +- [DateTime](../classes/helpers_datetime.DateTime.md) + ### Functions - [getNumericTime](helpers_datetime.md#getnumerictime)