Skip to content

Commit

Permalink
refactor(dateHelpers): sw-260 remove moment.js (#1220)
Browse files Browse the repository at this point in the history
  • Loading branch information
vbusch authored and cdcabrera committed Nov 7, 2023
1 parent 2ca7797 commit 801d081
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 42 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ The dotenv files are structured to cascade each additional dotenv file settings
| DEV_PORT | A local proxy build modification for running against a custom port |
| DEV_BRANCH | A local proxy build modification for running against a custom environment branch. Available options include `stage-beta`, `stage-stable`, `prod-beta`, `prod-stable` |
| GENERATE_SOURCEMAP | A static boolean that disables local run source map generation only. May speed up local development re-compiles. May eventually be moved into `.env.development`. |
| REACT_APP_DEBUG_DEFAULT_DATETIME | A static string associated with overriding the assumed UI/application date in the form of `YYYYMMDD` |
| REACT_APP_DEBUG_DEFAULT_DATETIME | A static string associated with overriding the assumed UI/application date in the form of `YYYY-MM-DD` |
| REACT_APP_DEBUG_MIDDLEWARE | A static boolean that activates the console state debugging messages associated with Redux. |
| REACT_APP_DEBUG_ORG_ADMIN | A static boolean associated with local development only that overrides the organization admin. Useful in determining UI/application behavior when permissions are missing. |
| REACT_APP_DEBUG_PERMISSION_APP_ONE | A static string associated with local development only that overrides RBAC associated permissions. Useful in determining UI/application behavior when permissions are missing. |
Expand Down
117 changes: 111 additions & 6 deletions src/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,62 @@
## Dates

* [Dates](#Helpers.module_Dates)
* [~currentDateTime](#Helpers.module_Dates..currentDateTime) : <code>Object</code>
* [~defaultDateTime](#Helpers.module_Dates..defaultDateTime) : <code>Object</code>
* [~weeklyDateTime](#Helpers.module_Dates..weeklyDateTime) : <code>Object</code>
* [~monthlyDateTime](#Helpers.module_Dates..monthlyDateTime) : <code>Object</code>
* [~quarterlyDateTime](#Helpers.module_Dates..quarterlyDateTime) : <code>Object</code>
* [~rangedYearDateTime](#Helpers.module_Dates..rangedYearDateTime) : <code>Object</code>
* [~timestampDayFormats](#Helpers.module_Dates..timestampDayFormats) : <code>Object</code>
* [~timestampMonthFormats](#Helpers.module_Dates..timestampMonthFormats) : <code>Object</code>
* [~timestampQuarterFormats](#Helpers.module_Dates..timestampQuarterFormats) : <code>Object</code>
* [~timestampTimeFormats](#Helpers.module_Dates..timestampTimeFormats) : <code>Object</code>
* [~timestampUTCTimeFormats](#Helpers.module_Dates..timestampUTCTimeFormats) : <code>Object</code>
* [~getCurrentDate()](#Helpers.module_Dates..getCurrentDate) ⇒ <code>string</code> \| <code>Date</code>
* [~setEndOfDay(date)](#Helpers.module_Dates..setEndOfDay) ⇒ <code>Date</code>
* [~setStartOfDay(date)](#Helpers.module_Dates..setStartOfDay) ⇒ <code>Date</code>
* [~setEndOfMonth(date)](#Helpers.module_Dates..setEndOfMonth) ⇒ <code>Date</code>
* [~setRangedDateTime(params)](#Helpers.module_Dates..setRangedDateTime) ⇒ <code>Object</code>
* [~getRangedDateTime(granularity)](#Helpers.module_Dates..getRangedDateTime) ⇒ <code>Object</code>
* [~getRangedMonthDateTime(month)](#Helpers.module_Dates..getRangedMonthDateTime) ⇒ <code>Object</code> \| <code>\*</code> \| <code>undefined</code>
* [~getRangedMonthDateTime(month, defaultLocale)](#Helpers.module_Dates..getRangedMonthDateTime) ⇒ <code>Object</code> \| <code>\*</code> \| <code>undefined</code>

<a name="Helpers.module_Dates..currentDateTime"></a>

### Dates~currentDateTime : <code>Object</code>
Generates the date range, starting at the beginning of getCurrentDate, and ending at the end of getCurrentDate.

**Kind**: inner constant of [<code>Dates</code>](#Helpers.module_Dates)
<a name="Helpers.module_Dates..defaultDateTime"></a>

### Dates~defaultDateTime : <code>Object</code>
Generates the date range, starting 30 days prior to getCurrentDate, and ending at the end of the getCurrentDate.

**Kind**: inner constant of [<code>Dates</code>](#Helpers.module_Dates)
<a name="Helpers.module_Dates..weeklyDateTime"></a>

### Dates~weeklyDateTime : <code>Object</code>
Generates the date range, starting on Sunday 12 weeks prior to getCurrentDate,
and ending at the end of the previous Saturday.

**Kind**: inner constant of [<code>Dates</code>](#Helpers.module_Dates)
<a name="Helpers.module_Dates..monthlyDateTime"></a>

### Dates~monthlyDateTime : <code>Object</code>
Generates the date range, starting 12 months prior to getCurrentDate, and ending at the end of the getCurrentDate.

**Kind**: inner constant of [<code>Dates</code>](#Helpers.module_Dates)
<a name="Helpers.module_Dates..quarterlyDateTime"></a>

### Dates~quarterlyDateTime : <code>Object</code>
Generates the date range, starting 36 months prior to getCurrentDate, and ending at the end of getCurrentDate.

**Kind**: inner constant of [<code>Dates</code>](#Helpers.module_Dates)
<a name="Helpers.module_Dates..rangedYearDateTime"></a>

### Dates~rangedYearDateTime : <code>Object</code>
Generates the date range, starting a year prior, and ending at the end of the previous month.

**Kind**: inner constant of [<code>Dates</code>](#Helpers.module_Dates)
<a name="Helpers.module_Dates..timestampDayFormats"></a>

### Dates~timestampDayFormats : <code>Object</code>
Expand Down Expand Up @@ -60,6 +106,66 @@ Consistent UTC timestamp time formats.
Return a date.

**Kind**: inner method of [<code>Dates</code>](#Helpers.module_Dates)
<a name="Helpers.module_Dates..setEndOfDay"></a>

### Dates~setEndOfDay(date) ⇒ <code>Date</code>
Sets the UTC time to the end of day.

**Kind**: inner method of [<code>Dates</code>](#Helpers.module_Dates)
**Returns**: <code>Date</code> - The date with the time set to the last millisecond of that day.
<table>
<thead>
<tr>
<th>Param</th><th>Type</th><th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>date</td><td><code>Date</code></td><td><p>The date tp use</p>
</td>
</tr> </tbody>
</table>

<a name="Helpers.module_Dates..setStartOfDay"></a>

### Dates~setStartOfDay(date) ⇒ <code>Date</code>
Sets UTC time to beginning of the day.

**Kind**: inner method of [<code>Dates</code>](#Helpers.module_Dates)
**Returns**: <code>Date</code> - Returns the date with the time set to the start of that day.
<table>
<thead>
<tr>
<th>Param</th><th>Type</th><th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>date</td><td><code>Date</code></td><td><p>The date tp use</p>
</td>
</tr> </tbody>
</table>

<a name="Helpers.module_Dates..setEndOfMonth"></a>

### Dates~setEndOfMonth(date) ⇒ <code>Date</code>
Sets the UTC date and time to the end of tha month.

**Kind**: inner method of [<code>Dates</code>](#Helpers.module_Dates)
**Returns**: <code>Date</code> - The date with the date and time set to the last millisecond of that month.
<table>
<thead>
<tr>
<th>Param</th><th>Type</th><th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>date</td><td><code>Date</code></td><td><p>The date tp use</p>
</td>
</tr> </tbody>
</table>

<a name="Helpers.module_Dates..setRangedDateTime"></a>

### Dates~setRangedDateTime(params) ⇒ <code>Object</code>
Expand All @@ -82,10 +188,7 @@ Set a date range based on a granularity type.
<td>params.subtract</td><td><code>number</code></td><td><p>Number of granularity type to subtract from the current date.</p>
</td>
</tr><tr>
<td>params.measurement</td><td><code>string</code></td><td><p>Granularity type.</p>
</td>
</tr><tr>
<td>params.endOfMeasurement</td><td><code>string</code></td><td><p>Granularity type.</p>
<td>params.measurement</td><td><code>&#x27;days&#x27;</code> | <code>&#x27;weeks&#x27;</code> | <code>&#x27;months&#x27;</code> | <code>&#x27;years&#x27;</code></td><td><p>Granularity type .</p>
</td>
</tr> </tbody>
</table>
Expand All @@ -110,7 +213,7 @@ Return a range of time based on known granularity types.

<a name="Helpers.module_Dates..getRangedMonthDateTime"></a>

### Dates~getRangedMonthDateTime(month) ⇒ <code>Object</code> \| <code>\*</code> \| <code>undefined</code>
### Dates~getRangedMonthDateTime(month, defaultLocale) ⇒ <code>Object</code> \| <code>\*</code> \| <code>undefined</code>
Generate a list of months for use in a select list.

**Kind**: inner method of [<code>Dates</code>](#Helpers.module_Dates)
Expand All @@ -123,6 +226,8 @@ Generate a list of months for use in a select list.
<tbody>
<tr>
<td>month</td><td><code>string</code></td>
</tr><tr>
<td>defaultLocale</td><td><code>string</code></td>
</tr> </tbody>
</table>

Expand Down
10 changes: 10 additions & 0 deletions src/common/__tests__/__snapshots__/dateHelpers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ exports[`DateHelpers should have specific functions: dateHelpers 1`] = `
"endDate": 2019-07-31T23:59:59.999Z,
"startDate": 2018-08-01T00:00:00.000Z,
},
"setEndOfDay": [Function],
"setEndOfMonth": [Function],
"setRangedDateTime": [Function],
"setStartOfDay": [Function],
"timestampDayFormats": {
"long": "MMMM D",
"short": "MMM D",
Expand Down Expand Up @@ -434,3 +437,10 @@ exports[`DateHelpers should return a predictable range of time: range of time 1`
},
}
`;

exports[`DateHelpers should return a predictable range of time: range of time for today 1`] = `
{
"endDate": 2019-07-20T23:59:59.999Z,
"startDate": 2019-07-20T00:00:00.000Z,
}
`;
50 changes: 49 additions & 1 deletion src/common/__tests__/dateHelpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,39 @@ describe('DateHelpers', () => {

it('should return a predictable range of time', () => {
const currentDate = getCurrentDate();
const rangeDateTime = setRangedDateTime({ date: getCurrentDate(), subtract: 5, measurement: 'days' });

expect(dateHelpers.setRangedDateTime()).toMatchSnapshot('range of time for today');

const rangeDateTime = setRangedDateTime({ subtract: 5, measurement: 'days' });

expect({
currentDate,
rangeDateTime
}).toMatchSnapshot('range of time');

const expectedProvidedDate = new Date('2023-9-24');
const rangeDateTimeWithProvidedDate = setRangedDateTime({
date: expectedProvidedDate,
subtract: 0,
measurement: 'days'
});

expect(rangeDateTimeWithProvidedDate).toEqual({
endDate: new Date(expectedProvidedDate.setUTCHours(23, 59, 59, 999)),
startDate: new Date(expectedProvidedDate.setUTCHours(0, 0, 0, 0))
});

const nonSundayStartDate = new Date(Date.UTC(2023, 9, 24, 18, 6, 8, 3));
const expectedSundayStartDate = new Date(Date.UTC(2023, 6, 30, 0, 0, 0, 0));
const expectedSaturdayEndDate = new Date(Date.UTC(2023, 9, 22, 23, 59, 59, 999));

expect(
dateHelpers.setRangedDateTime({
date: nonSundayStartDate,
subtract: 12,
measurement: 'weeks'
})
).toEqual({ startDate: expectedSundayStartDate, endDate: expectedSaturdayEndDate });
});

it('should return a predictable range based on granularity', () => {
Expand All @@ -53,4 +80,25 @@ describe('DateHelpers', () => {
expect(getRangedMonthDateTime('april')).toMatchSnapshot('get a specific month by name');
expect(getRangedMonthDateTime(3)).toMatchSnapshot('get a specific month by number');
});

it('should change date to end of day', () => {
const startDate = new Date(Date.UTC(2020, 7, 15, 3, 6, 8, 3));
const expectedDate = new Date(Date.UTC(2020, 7, 15, 23, 59, 59, 999));

expect(dateHelpers.setEndOfDay(startDate).getTime()).toEqual(expectedDate.getTime());
});

it('should change date to start of day', () => {
const startDate = new Date(Date.UTC(2020, 7, 15, 3, 6, 8, 3));
const expectedDate = new Date(Date.UTC(2020, 7, 15, 0, 0, 0, 0));

expect(dateHelpers.setStartOfDay(startDate).getTime()).toEqual(expectedDate.getTime());
});

it('should change date to end of Month', () => {
const startDate = new Date(Date.UTC(2020, 7, 15, 3, 6, 8, 3));
const expectedDate = new Date(Date.UTC(2020, 7, 31, 23, 59, 59, 999));

expect(dateHelpers.setEndOfMonth(startDate).getTime()).toEqual(expectedDate.getTime());
});
});
Loading

0 comments on commit 801d081

Please sign in to comment.