Skip to content

Commit

Permalink
feat(day): allow passing dayjs object to createYmd and createHms (#445)…
Browse files Browse the repository at this point in the history
… (#446)

close #445
  • Loading branch information
brc-dd committed Jan 22, 2024
1 parent c1b48cd commit 07b57d2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
38 changes: 29 additions & 9 deletions lib/support/Day.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import dayjs, { type ConfigType, type Dayjs } from 'dayjs'
import PluginRelativeTime from 'dayjs/plugin/relativeTime'
import PluginTimezone from 'dayjs/plugin/timezone'
import PluginUtc from 'dayjs/plugin/utc'
import { isNumber, isObject, isString } from './Utils'
import { isNullish, isNumber, isObject, isString } from './Utils'

dayjs.extend(PluginUtc)
dayjs.extend(PluginTimezone)
Expand Down Expand Up @@ -60,30 +60,50 @@ export function tz(input?: Input, timezone?: string): Day {
/**
* Creates a new `Ymd` object.
*/
export function createYmd(year?: number | null, month?: number | null, date?: number | null): Ymd
export function createYmd(day?: Day | null): Ymd
export function createYmd(
year: number | null = null,
yearOrDay: number | Day | null = null,
month: number | null = null,
date: number | null = null
): Ymd {
if (isNumber(yearOrDay) || isNullish(yearOrDay)) {
return {
year: yearOrDay,
month,
date
}
}

return {
year,
month,
date
year: yearOrDay.year(),
month: yearOrDay.month() + 1,
date: yearOrDay.date()
}
}

/**
* Creates a new `Hms` object.
*/
export function createHms(hour?: string | null, minute?: string | null, second?: string | null): Hms
export function createHms(day?: Day | null): Hms
export function createHms(
hour: string | null = null,
hourOrDay: string | Day | null = null,
minute: string | null = null,
second: string | null = null
): Hms {
if (isString(hourOrDay) || isNullish(hourOrDay)) {
return {
hour: hourOrDay,
minute,
second
}
}

return {
hour,
minute,
second
hour: hourOrDay.format('HH'),
minute: hourOrDay.format('mm'),
second: hourOrDay.format('ss')
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/support/Utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function isNullish(value: unknown): value is undefined | null {
return value === null || value === undefined
return value == null
}

export function isString(value: unknown): value is string {
Expand Down
24 changes: 22 additions & 2 deletions tests/support/Day.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('support/Day', () => {
})

describe('createYmd', () => {
test('creates a new ymd instance', () => {
test('creates a new ymd object', () => {
const expected = {
year: 1985,
month: 10,
Expand All @@ -37,6 +37,16 @@ describe('support/Day', () => {
expect(Day.createYmd(1985, 10, 10)).toEqual(expected)
})

test('creates a new ymd object from a day instance', () => {
const expected = {
year: 1985,
month: 10,
date: 10
}

expect(Day.createYmd(Day.day('1985-10-10'))).toEqual(expected)
})

test('all fields are `null` by default', () => {
const expected = {
year: null,
Expand All @@ -49,7 +59,7 @@ describe('support/Day', () => {
})

describe('createHms', () => {
test('creates a new hms instance', () => {
test('creates a new hms object', () => {
const expected = {
hour: '10',
minute: '20',
Expand All @@ -59,6 +69,16 @@ describe('support/Day', () => {
expect(Day.createHms('10', '20', '30')).toEqual(expected)
})

test('creates a new hms object from a day instance', () => {
const expected = {
hour: '10',
minute: '20',
second: '30'
}

expect(Day.createHms(Day.day('1985-10-10 10:20:30'))).toEqual(expected)
})

test('all fields are `null` by default', () => {
const expected = {
hour: null,
Expand Down

0 comments on commit 07b57d2

Please sign in to comment.