Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert date to string #77

Merged
merged 9 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export {
pickWithoutUndefined,
copyWithoutUndefined,
isEmptyObject,
convertDatesToIsoString,
} from './src/utils/objectUtils'

export {
Expand Down
107 changes: 107 additions & 0 deletions src/utils/objectUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from 'vitest'

import {
convertDatesToIsoString,
copyWithoutUndefined,
groupBy,
groupByUnique,
Expand Down Expand Up @@ -371,4 +372,110 @@ describe('objectUtils', () => {
)
})
})

describe('convertDatesToIsoString', () => {
it('Empty object', () => {
expect(convertDatesToIsoString({})).toStrictEqual({})
})

type TestInputType = {
id: number
value: string
date: Date
code: number
reason?: string | null
other?: TestInputType
}

type TestExpectedType = {
id: number
value: string
date: string
code: number
other?: TestExpectedType
}

it('simple objects', () => {
const date = new Date()
const input: TestInputType = {
id: 1,
date,
value: 'test',
reason: 'reason',
code: 100,
}

const output: TestExpectedType = convertDatesToIsoString(input)

expect(output).toStrictEqual({
id: 1,
date: date.toISOString(),
value: 'test',
code: 100,
reason: 'reason',
})
})

it('handles undefined and null', () => {
const date = new Date()
const input: TestInputType = {
id: 1,
date,
value: 'test',
code: 100,
reason: null,
other: undefined,
}

const output: TestExpectedType = convertDatesToIsoString(input)

expect(output).toStrictEqual({
id: 1,
date: date.toISOString(),
value: 'test',
code: 100,
reason: null,
other: undefined,
})
})

it('nested objects', () => {
const date1 = new Date()
const date2 = new Date()
date2.setFullYear(1990)
const input: TestInputType = {
id: 1,
date: date1,
value: 'test',
code: 100,
reason: 'reason',
other: {
id: 2,
value: 'test 2',
date: date2,
code: 200,
reason: null,
other: undefined,
},
}

const output: TestExpectedType = convertDatesToIsoString(input)

expect(output).toMatchObject({
id: 1,
date: date1.toISOString(),
value: 'test',
code: 100,
reason: 'reason',
other: {
id: 2,
value: 'test 2',
date: date2.toISOString(),
code: 200,
reason: null,
other: undefined,
},
})
})
})
})
22 changes: 22 additions & 0 deletions src/utils/objectUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,25 @@ export function groupByUnique<
{} as Record<RecordKeyType, T>,
)
}

type DatesAsString<T> = T extends Date ? string : ExactlyLikeWithDateAsString<T>

type ExactlyLikeWithDateAsString<T> = T extends object ? { [K in keyof T]: DatesAsString<T[K]> } : T

export function convertDatesToIsoString<Input extends object>(
CarlosGamero marked this conversation as resolved.
Show resolved Hide resolved
object: Input,
): ExactlyLikeWithDateAsString<Input> {
return Object.entries(object).reduce((result, [key, value]) => {
if (value instanceof Date) {
// @ts-ignore
result[key] = value.toISOString()
} else if (value && typeof value === 'object') {
// @ts-ignore
result[key] = convertDatesToIsoString(value)
} else {
// @ts-ignore
result[key] = value
}
return result
}, {} as ExactlyLikeWithDateAsString<Input>)
}