Skip to content
Snippets Groups Projects
Commit bd946c7a authored by Giildo's avatar Giildo
Browse files

:recycle:️ Constructor can receive a string

parent bdfbbbd5
Branches
Tags 1.1.5-sr3
No related merge requests found
Pipeline #262040 passed with stage
in 17 seconds
......@@ -26,7 +26,7 @@ export class Day implements DayInterface {
'December',
]
constructor(data?: DayInitObject) {
constructor(data?: DayInitObject | string) {
if (!data) {
const now = new Date()
this.#Y = now.getFullYear()
......@@ -36,10 +36,50 @@ export class Day implements DayInterface {
this.#m = now.getMinutes()
this.#s = now.getSeconds()
this.#ms = now.getMilliseconds()
this.#displayDate = now.toISOString()
return
}
if (typeof data === 'string') {
if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/.test(data)) {
const {
year,
month,
day,
hour,
minute,
second,
millisecond,
} = /^(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})T(?<hour>\d{2}):(?<minute>\d{2}):(?<second>\d{2}).(?<millisecond>\d{3})Z$/.exec(data)!.groups!
this.#Y = parseInt(year)
this.#M = parseInt(month)
this.#D = parseInt(day)
this.#H = parseInt(hour)
this.#m = parseInt(minute)
this.#s = parseInt(second)
this.#ms = parseInt(millisecond)
return
}
if (/^\d{4}-\d{2}-\d{2}$/.test(data)) {
const [year, month, day] = data.split('-')
this.#Y = parseInt(year)
this.#M = parseInt(month)
this.#D = parseInt(day)
return
}
if (/^\d{4}\/\d{2}\/\d{2}$/.test(data)) {
const [year, month, day] = data.split('/')
this.#Y = parseInt(year)
this.#M = parseInt(month)
this.#D = parseInt(day)
console.log(this.#Y, this.#M, this.#D)
return
}
throw new Error(`Invalid date format, data received: ${data}`)
}
if (!data.year) throw new Error('Year is required')
this.#Y = data.year
......
......@@ -20,6 +20,46 @@ describe('Day class', () => {
expect(day.displayDate).toBe('')
})
describe('constructor with a string', () => {
test('should parse the date if the constructor receives a string with ISO format', () => {
const day = new Day('2022-10-07T01:01:01.001Z')
expect(day.year).toBe(2022)
expect(day.month).toBe(10)
expect(day.day).toBe(7)
expect(day.hour).toBe(1)
expect(day.minute).toBe(1)
expect(day.second).toBe(1)
expect(day.millisecond).toBe(1)
})
test('should parse the date if the constructor receives a string with the format "YYYY-MM-DD', () => {
const day = new Day('2023-01-01')
expect(day.year).toBe(2023)
expect(day.month).toBe(1)
expect(day.day).toBe(1)
expect(day.hour).toBeUndefined()
expect(day.minute).toBeUndefined()
expect(day.second).toBeUndefined()
expect(day.millisecond).toBeUndefined()
})
test('should parse the date if the constructor receives a string with the format "YYYY/MM/DD', () => {
const day = new Day('2024-11-18')
expect(day.year).toBe(2024)
expect(day.month).toBe(11)
expect(day.day).toBe(18)
expect(day.hour).toBeUndefined()
expect(day.minute).toBeUndefined()
expect(day.second).toBeUndefined()
expect(day.millisecond).toBeUndefined()
})
test('should thrown an error if the date is not valid', () => {
expect(() => new Day('2022-10-01T12:45:35.001'))
.toThrowError('Invalid date format, data received: 2022-10-01T12:45:35.001')
})
})
test('should set all properties', () => {
const day = new Day({
year: 2021,
......
......@@ -22,7 +22,7 @@ export class Day {
readonly second: number
readonly year: number
constructor(data?: DayInitObject);
constructor(data?: DayInitObject | string);
static utc(data: string): Day;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment