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

:sparkles: Add set & toISOString methods

parent 06a102d3
Branches
Tags
No related merge requests found
Pipeline #261480 failed with stage
in 14 seconds
......@@ -112,6 +112,22 @@ export class Day implements DayInterface {
})
}
set(type: SetValueType, value: number): Day {
return ({
year: () => new Day({ ...this.#getThisDayInitObject(), year: value }),
month: () => new Day({ ...this.#getThisDayInitObject(), month: value }),
day: () => new Day({ ...this.#getThisDayInitObject(), day: value }),
hour: () => new Day({ ...this.#getThisDayInitObject(), hour: value }),
minute: () => new Day({ ...this.#getThisDayInitObject(), minute: value }),
second: () => new Day({ ...this.#getThisDayInitObject(), second: value }),
millisecond: () => new Day({ ...this.#getThisDayInitObject(), millisecond: value }),
})[type]()
}
toISOString(): string {
return `${this.#Y}-${this.#M?.toString().padStart(2, '0') || '01'}-${this.#D?.toString().padStart(2, '0') || '01'}T${this.#H?.toString().padStart(2, '0') || '00'}:${this.#m?.toString().padStart(2, '0') || '00'}:${this.#s?.toString().padStart(2, '0') || '00'}.${this.#ms?.toString().padStart(3, '0') || '000'}Z`
}
add(value: number, unit: SetValueType): Day {
({
year: () => this.#Y += value,
......@@ -541,4 +557,16 @@ export class Day implements DayInterface {
#diffMillisecond(thisDay: Day, day: Day): number {
return (this.#diffSecond(thisDay, day) || 0) * 1000 + (thisDay.millisecond - day.millisecond)
}
#getThisDayInitObject(): DayInitObject {
const initObject: DayInitObject = { year: this.#Y }
if (this.#M) initObject.month = this.#M
if (this.#D) initObject.day = this.#D
if (this.#H) initObject.hour = this.#H
if (this.#m) initObject.minute = this.#m
if (this.#s) initObject.second = this.#s
if (this.#ms) initObject.millisecond = this.#ms
if (this.#displayDate) initObject.display_date = this.#displayDate
return initObject
}
}
\ No newline at end of file
import { describe, expect, test } from 'vitest'
import { Day } from '@/Day'
describe('set method', () => {
test('should set year', () => {
const day = new Day({ year: 2021 })
const newDay = day.set('year', 2022)
expect(newDay.year).toBe(2022)
expect(day.year).toBe(2021)
})
test('should set year with displayDate', () => {
const day = new Day({ year: 2021, display_date: '2021-01-01' })
const newDay = day.set('year', 2022)
expect(newDay.year).toBe(2022)
expect(newDay.displayDate).toBe('2021-01-01')
expect(day.year).toBe(2021)
expect(day.displayDate).toBe('2021-01-01')
})
test('should set month', () => {
const day = new Day({ year: 2021, month: 1 })
const newDay = day.set('month', 2)
expect(newDay.year).toBe(2021)
expect(newDay.month).toBe(2)
expect(day.year).toBe(2021)
expect(day.month).toBe(1)
})
test('should set day', () => {
const day = new Day({ year: 2021, month: 1, day: 1 })
const newDay = day.set('day', 2)
expect(newDay.year).toBe(2021)
expect(newDay.day).toBe(2)
expect(day.year).toBe(2021)
expect(day.day).toBe(1)
})
test('should set hour', () => {
const day = new Day({ year: 2021, month: 1, day: 1, hour: 1 })
const newDay = day.set('hour', 2)
expect(newDay.year).toBe(2021)
expect(newDay.hour).toBe(2)
expect(day.year).toBe(2021)
expect(day.hour).toBe(1)
})
test('should set minute', () => {
const day = new Day({ year: 2021, month: 1, day: 1, minute: 1 })
const newDay = day.set('minute', 2)
expect(newDay.year).toBe(2021)
expect(newDay.minute).toBe(2)
expect(day.year).toBe(2021)
expect(day.minute).toBe(1)
})
test('should set second', () => {
const day = new Day({ year: 2021, month: 1, day: 1, second: 1 })
const newDay = day.set('second', 2)
expect(newDay.year).toBe(2021)
expect(newDay.second).toBe(2)
expect(day.year).toBe(2021)
expect(day.second).toBe(1)
})
test('should set millisecond', () => {
const day = new Day({ year: 2021, month: 1, day: 1, millisecond: 1 })
const newDay = day.set('millisecond', 2)
expect(newDay.year).toBe(2021)
expect(newDay.millisecond).toBe(2)
expect(day.year).toBe(2021)
expect(day.millisecond).toBe(1)
})
})
\ No newline at end of file
import { describe, expect, test } from 'vitest'
import { Day } from '@/Day'
describe('toISOString method', () => {
test('should return a string', () => {
const day = new Day({ year: 2021, month: 1, day: 1, hour: 1, minute: 1, second: 1, millisecond: 1 })
expect(day.toISOString()).toMatch(/^2021-01-01T01:01:01.001Z$/)
})
test('should return a string when I do not have values', () => {
const day = new Day({ year: 2021 })
expect(day.toISOString()).toMatch(/^2021-01-01T00:00:00.000Z$/)
})
})
\ No newline at end of file
......@@ -43,4 +43,8 @@ export class Day {
diff(day: Day, unit: SetValueType): number;
format(format: string): string;
set(type: SetValueType, value: number): Day;
toISOString(): string;
}
\ No newline at end of file
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