Skip to content

Commit

Permalink
addressing PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cauemarcondes committed Jun 14, 2021
1 parent 2f3442d commit e923195
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,12 @@ export function getComparisonTypes({
const momentStart = moment(start);
const momentEnd = moment(end);

const dateDiff = Number(
getDateDifference({
start: momentStart,
end: momentEnd,
unitOfTime: 'days',
precise: true,
})
);
const dateDiff = getDateDifference({
start: momentStart,
end: momentEnd,
unitOfTime: 'days',
precise: true,
});

// Less than or equals to one day
if (dateDiff <= 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,24 @@ describe('url_params_context helpers', () => {
expect(helpers.getExactDate('2021-01-28T05:47:52.134Z')).toBeUndefined();
});

it('removes rounding option from relative time', () => {
['s', 'm', 'h', 'd', 'w'].map((roundingOption) =>
it(`removes /${roundingOption} rounding option from relative time`, () => {
const spy = jest.spyOn(datemath, 'parse');
helpers.getExactDate(`now/${roundingOption}`);
expect(spy).toHaveBeenCalledWith('now', {});
})
);

it('removes rounding option but keeps subtracting time', () => {
const spy = jest.spyOn(datemath, 'parse');
helpers.getExactDate('now/d');
expect(spy).toHaveBeenCalledWith('now', {});
helpers.getExactDate('now-24h/h');
expect(spy).toHaveBeenCalledWith('now-24h', {});
});

it('removes rounding option but keeps adding time', () => {
const spy = jest.spyOn(datemath, 'parse');
helpers.getExactDate('now+15m/h');
expect(spy).toHaveBeenCalledWith('now+15m', {});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export function getExactDate(rawDate?: string, options = {}) {
if (rawDate) {
const isRelativeDate = rawDate.substring(0, 3) === 'now';
if (isRelativeDate) {
const rawDateWithouRounding = rawDate.replace(/\/(\w)$/, '');
// remove rounding from relative dates "Today" (now/d) and "This week" (now/w)
const rawDateWithouRounding = rawDate.replace(/\/([smhdw])$/, '');
return getParsedDate(rawDateWithouRounding, options);
}
}
Expand Down Expand Up @@ -63,9 +64,8 @@ export function getDateRange({

// rounds down start to minute
const roundedStart = moment(start).startOf('minute');
// rounds down to minute
const exactStart = getExactDate(rangeFrom) || roundedStart;
const exactEnd = getExactDate(rangeTo, { roundUp: true }) || end;
const exactEnd = getExactDate(rangeTo) || end;

return {
start: roundedStart.toISOString(),
Expand Down

0 comments on commit e923195

Please sign in to comment.