Skip to content

Commit

Permalink
feat(dateHelpers): sw-2353 added setMillisecondsFromDate (#1342)
Browse files Browse the repository at this point in the history
* dateHelpers, add milliseconds from a date
* downloadHelpers, fix for revokeObjectURL
  • Loading branch information
cdcabrera committed Jun 23, 2024
1 parent dda0e96 commit be214e8
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* [~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>
* [~setMillisecondsFromDate(params)](#Helpers.module_Dates..setMillisecondsFromDate) ⇒ <code>Date</code>
* [~getRangedDateTime(granularity)](#Helpers.module_Dates..getRangedDateTime) ⇒ <code>Object</code>
* [~getRangedMonthDateTime(month, defaultLocale)](#Helpers.module_Dates..getRangedMonthDateTime) ⇒ <code>Object</code> \| <code>\*</code> \| <code>undefined</code>

Expand Down Expand Up @@ -193,6 +194,29 @@ Set a date range based on a granularity type.
</tr> </tbody>
</table>

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

### Dates~setMillisecondsFromDate(params) ⇒ <code>Date</code>
Set milliseconds from date. Defaults to internal current date.

**Kind**: inner method of [<code>Dates</code>](#Helpers.module_Dates)
<table>
<thead>
<tr>
<th>Param</th><th>Type</th><th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>params</td><td><code>object</code></td><td></td>
</tr><tr>
<td>params.date</td><td><code>Date</code></td><td><p>The date to add milliseconds towards. Defaults to current date.</p>
</td>
</tr><tr>
<td>params.ms</td><td><code>number</code></td><td></td>
</tr> </tbody>
</table>

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

### Dates~getRangedDateTime(granularity) ⇒ <code>Object</code>
Expand Down
3 changes: 3 additions & 0 deletions src/common/__tests__/__snapshots__/dateHelpers.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`DateHelpers should add milliseconds from date: add milliseconds 1`] = `2019-07-21T00:00:00.000Z`;

exports[`DateHelpers should have specific functions: dateHelpers 1`] = `
{
"currentDateTime": {
Expand Down Expand Up @@ -27,6 +29,7 @@ exports[`DateHelpers should have specific functions: dateHelpers 1`] = `
},
"setEndOfDay": [Function],
"setEndOfMonth": [Function],
"setMillisecondsFromDate": [Function],
"setRangedDateTime": [Function],
"setStartOfDay": [Function],
"timestampDayFormats": {
Expand Down
4 changes: 4 additions & 0 deletions src/common/__tests__/dateHelpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,8 @@ describe('DateHelpers', () => {

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

it('should add milliseconds from date', () => {
expect(dateHelpers.setMillisecondsFromDate({ ms: 86400000 })).toMatchSnapshot('add milliseconds');
});
});
13 changes: 13 additions & 0 deletions src/common/dateHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ const setRangedDateTime = ({ date = getCurrentDate(), subtract = 0, measurement
}
};

/**
* Set milliseconds from date. Defaults to internal current date.
*
* @param {object} params
* @param {Date} params.date The date to add milliseconds towards. Defaults to current date.
* @param {number} params.ms
* @returns {Date}
*/
const setMillisecondsFromDate = ({ date = getCurrentDate(), ms = 0 } = {}) =>
new Date(new Date(date).setUTCMilliseconds(ms));

/**
* Generates the date range, starting at the beginning of getCurrentDate, and ending at the end of getCurrentDate.
*
Expand Down Expand Up @@ -288,6 +299,7 @@ const dateHelpers = {
setStartOfDay,
setEndOfDay,
setEndOfMonth,
setMillisecondsFromDate,
getRangedMonthDateTime,
getRangedDateTime,
setRangedDateTime,
Expand All @@ -310,6 +322,7 @@ export {
setStartOfDay,
setEndOfDay,
setEndOfMonth,
setMillisecondsFromDate,
getRangedMonthDateTime,
getRangedDateTime,
setRangedDateTime,
Expand Down
2 changes: 1 addition & 1 deletion src/common/downloadHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ const downloadData = options => {
anchorTag.click();

setTimeout(() => {
URL.revokeObjectURL(anchorTag.href);
document.body.removeChild(anchorTag);
URL.revokeObjectURL(blob);
resolve({ fileName, data });
}, 250);
}
Expand Down

0 comments on commit be214e8

Please sign in to comment.