-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[UXE-2304] refactor: isolate beholder date proto (#935)
* refactor: export minute/hour constants * refactor: move date prototype to metrics module * refactor: simplify convert-date file in helpers * test: add tests to convert-date in helper * test: adjust tests in metrics module helper * refactor: remove unused import methods in filters metrics/events * chore: import date proto in main file * refactor: move personal tokens date methosd to convert-date in helpers * refactor: inject convertDateToLocalTimezone in personal tokens * refactor: improve date proto file * refactor: inject convertDateToLocalTimezone in personal tokens * chore: import date proto in metricsView file * refactor: remove unused import * test: create make-domains-url test * test: create search-domains test * test: update make-domains-url test * test: create edge-dns test * test: create make edge-functions test * test: create edge-dns test * test: create edge-functions test * feat: move module import to App.vue
- Loading branch information
1 parent
7d920a8
commit d17e05d
Showing
18 changed files
with
518 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { TIME_INTERVALS } from '@modules/real-time-metrics/constants' | ||
|
||
/** | ||
* Remove the specified amount of hours from the given date | ||
* | ||
* @param {number} pOffset - The number of hours to remove | ||
* @returns {Date} The new date after removing the specified hours | ||
*/ | ||
Date.prototype.removeSelectedAmountOfHours = function (pOffset) { | ||
const offset = Number(pOffset) | ||
const offsetTimestamp = offset * TIME_INTERVALS.HOUR_IN_MILLISECONDS | ||
const calculatedTimestamp = this.getTime() - offsetTimestamp | ||
const calculatedDate = new Date(calculatedTimestamp) | ||
|
||
return calculatedDate | ||
} | ||
|
||
/** | ||
* Set the current date to UTC0 and remove Timezone tag | ||
* | ||
* @returns {string} Date without timezone | ||
*/ | ||
Date.prototype.removeZone = function () { | ||
const dateWithUserTimezone = this.toISOString() | ||
const dateWithoutZone = dateWithUserTimezone.replace(/(\..+)/, '') | ||
return dateWithoutZone | ||
} | ||
|
||
/** | ||
* Convert current date to the UTC informed | ||
* | ||
* @param {number} userUTC - The UTC offset to convert the date to | ||
* @returns {Date} The new date converted to the specified UTC | ||
*/ | ||
Date.prototype.toUTC = function (userUTC = 0) { | ||
const tz = Number(userUTC) | ||
const tzTimeStamp = (tz / 100) * TIME_INTERVALS.HOUR_IN_MILLISECONDS | ||
const dateWithUserTimezone = new Date(this.getTime() + tzTimeStamp) | ||
const dateWithoutZone = dateWithUserTimezone.toBeholderFormat() | ||
return new Date(dateWithoutZone) | ||
} | ||
|
||
/** | ||
* Reset the current date according to the specified UTC offset | ||
* | ||
* @param {number} userUTC - The UTC offset to reset the date to | ||
* @returns {Date} The new date reset to the specified UTC | ||
*/ | ||
Date.prototype.resetUTC = function (userUTC = 0) { | ||
const regexpChangeUTC = /(.+)([+|-]\d+)(.+)/g | ||
const injectUserUTC = this.toString().replace(regexpChangeUTC, `$1${userUTC}$3`) | ||
|
||
return new Date(injectUserUTC) | ||
} | ||
|
||
/** | ||
* Format date as the Beholder standard | ||
* | ||
* @returns {string} Date in Beholder format | ||
*/ | ||
Date.prototype.toBeholderFormat = function () { | ||
return this.toISOString().replace(/(\..+)/, '') | ||
} | ||
|
||
/** | ||
* Convert the date from the local format to the Beholder format | ||
* | ||
* @returns {string} Date in Beholder format | ||
*/ | ||
Date.prototype.fromLocaletoBeholderFormat = function () { | ||
const toLocale = this.toLocaleString('en-GB') | ||
const parts = toLocale.split(/\/|, /) | ||
const [day, month, year, time] = parts | ||
|
||
return `${year}-${month}-${day}T${time}` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,21 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import '@/helpers/convert-date' | ||
import { | ||
removeSelectedAmountOfHours, | ||
formatToEndOfDayIsoDate, | ||
convertOffsetToDecimal | ||
} from '@/helpers/convert-date' | ||
import { describe, it, expect } from 'vitest' | ||
import { convertValueToDate, convertDateToLocalTimezone } from '@/helpers/convert-date' | ||
|
||
describe('convertDate', () => { | ||
describe('removeSelectedAmountOfHours', () => { | ||
it('should remove the specified amount of hours from a date', () => { | ||
const date = new Date('2022-01-01T05:00:00Z') | ||
const hoursToRemove = 2 | ||
const expectedDate = new Date('2022-01-01T03:00:00Z') | ||
it('should convert a given value to a date string in a specific format', () => { | ||
const value = '2022-01-01T00:00:00' | ||
const expectedDate = 'January 1, 2022 at 12:00 AM' | ||
|
||
expect(removeSelectedAmountOfHours(hoursToRemove, date)).toEqual(expectedDate) | ||
}) | ||
expect(convertValueToDate(value)).toBe(expectedDate) | ||
}) | ||
|
||
describe('formatToEndOfDayIsoDate', () => { | ||
it('should format a date to ISO format without milliseconds', () => { | ||
const date = new Date('2022-01-01T23:59:59.999Z') | ||
const expectedFormattedDate = '2022-01-01T23:59:59' | ||
it('should convert a date to the local timezone', () => { | ||
const utcOffset = '+0300' | ||
const dateInUtcFormat = new Date('2022-01-01T00:00:00Z') | ||
|
||
expect(formatToEndOfDayIsoDate(date)).toBe(expectedFormattedDate) | ||
}) | ||
}) | ||
|
||
describe('convertOffsetToDecimal', () => { | ||
it('should convert a UTC offset to decimal format', () => { | ||
const offset = '+0300' | ||
const expectedDecimal = '+03.00' | ||
|
||
expect(convertOffsetToDecimal(offset)).toBe(expectedDecimal) | ||
}) | ||
}) | ||
|
||
describe('Date.prototype methods', () => { | ||
it('should convert date to UTC based on user offset', () => { | ||
const date = new Date('2022-01-01T00:00:00Z') | ||
const expectedDate = new Date('2022-01-01T00:00:00.000Z') | ||
|
||
expect(date.toUTC(0)).toEqual(expectedDate) | ||
}) | ||
|
||
it('should reset date to specified UTC offset', () => { | ||
const date = new Date('2022-01-01T00:00:00Z') | ||
const userUTC = '+0200' | ||
const expectedDate = new Date('2021-12-31T22:00:00Z') | ||
|
||
expect(date.resetUTC(userUTC)).toEqual(expectedDate) | ||
}) | ||
|
||
it('should format date to Beholder format', () => { | ||
const date = new Date('2022-01-01T00:00:00.000Z') | ||
const expectedFormattedDate = '2022-01-01T00:00:00' | ||
|
||
expect(date.toBeholderFormat()).toBe(expectedFormattedDate) | ||
}) | ||
|
||
it('should convert local date to Beholder format', () => { | ||
const date = new Date('2022-01-01T00:00:00') | ||
date.toLocaleString = () => '01/01/2022, 00:00:00' | ||
const expectedFormattedDate = '2022-01-01T00:00:00' | ||
|
||
expect(date.fromLocaletoBeholderFormat()).toBe(expectedFormattedDate) | ||
}) | ||
|
||
it('should convert a date to the local timezone', () => { | ||
const utcOffset = '+0200' | ||
const dateInUtcFormat = new Date('2022-01-01T00:00:00Z') | ||
|
||
const convertedDate = dateInUtcFormat.convertDateToLocalTimezone(utcOffset) | ||
const expectedDate = '2022-01-01T23:59:59' | ||
const convertedDate = convertDateToLocalTimezone(dateInUtcFormat, utcOffset) | ||
const expectedDate = '2022-01-01T23:59:59' | ||
|
||
expect(convertedDate).toBe(expectedDate) | ||
}) | ||
expect(convertedDate).toBe(expectedDate) | ||
}) | ||
}) |
Oops, something went wrong.