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 all 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
8 changes: 6 additions & 2 deletions docs/object-utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ Returns an object which contains fields specified in `propNames` with the same v

Returns true if `params` has no own properties or only own properties with value `undefined`, false otherwise.

`export function groupBy<T extends { [K in keyof T]: string | number | symbol | null | undefined }, K extends keyof T>(array: T[], selector: K): Record<string | number | symbol, T[]>`
`groupBy<T extends { [K in keyof T]: string | number | symbol | null | undefined }, K extends keyof T>(array: T[], selector: K): Record<string | number | symbol, T[]>`

The `groupBy` function takes an array of objects and a `selector`, groups the objects based on selected key and returns an object with unique keys from the selector and corresponding groups as arrays.

`export function groupByUnique<T extends { [K in keyof T]: string | number | symbol | null | undefined }, K extends keyof T>(array: T[], selector: K): Record<string | number | symbol, T>`
`groupByUnique<T extends { [K in keyof T]: string | number | symbol | null | undefined }, K extends keyof T>(array: T[], selector: K): Record<string | number | symbol, T>`

Similar to `groupBy`, but the value is a single element, in case of duplicated values for the same selector the method will throw a `InternalError`

`convertDateFieldsToIsoString<Input extends object>(object: Input | Input[],): ExactlyLikeWithDateAsString<Input> | ExactlyLikeWithDateAsString<Input>[]`

Transform an input object with `Date` members into an object with the corresponding `Date` members replaced by their ISO string representation. This transformation is done recursively, covering nested objects as well.
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,
convertDateFieldsToIsoString,
} from './src/utils/objectUtils'

export {
Expand Down
175 changes: 175 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 {
convertDateFieldsToIsoString,
copyWithoutUndefined,
groupBy,
groupByUnique,
Expand Down Expand Up @@ -371,4 +372,178 @@ describe('objectUtils', () => {
)
})
})

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

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

type TestExpectedType = {
id: number
value: string
date: string
code: number
other?: TestExpectedType
array?: {
id: number
createdAt: string
}[]
}

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

const output: TestExpectedType = convertDateFieldsToIsoString(input)
Copy link
Collaborator

@kibertoad kibertoad Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to do explicit type here? is it meant as a assert-like?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function is automatically inferring the type.

yes, it is like an assert to make sure the type is being inferred correctly


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

it('simple array', () => {
const date1 = new Date()
const date2 = new Date()
const input: TestInputType[] = [
{
id: 1,
date: date1,
value: 'test',
reason: 'reason',
code: 100,
},
{
id: 2,
date: date2,
value: 'test 2',
reason: 'reason 2',
code: 200,
},
]

const output: TestExpectedType[] = convertDateFieldsToIsoString(input)

expect(output).toStrictEqual([
{
id: 1,
date: date1.toISOString(),
value: 'test',
code: 100,
reason: 'reason',
},
{
id: 2,
date: date2.toISOString(),
value: 'test 2',
code: 200,
reason: 'reason 2',
},
])
})

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 = convertDateFieldsToIsoString(input)

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

it('nested objects and array', () => {
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,
},
array: [
{
id: 1,
createdAt: date1,
},
{
id: 2,
createdAt: date2,
},
],
}

const output: TestExpectedType = convertDateFieldsToIsoString(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,
},
array: [
{
id: 1,
createdAt: date1.toISOString(),
},
{
id: 2,
createdAt: date2.toISOString(),
},
],
})
})
})
})
32 changes: 32 additions & 0 deletions src/utils/objectUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,35 @@ 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 convertDateFieldsToIsoString<Input extends object>(
object: Input,
): ExactlyLikeWithDateAsString<Input>
export function convertDateFieldsToIsoString<Input extends object>(
object: Input[],
): ExactlyLikeWithDateAsString<Input>[]
export function convertDateFieldsToIsoString<Input extends object>(
object: Input | Input[],
): ExactlyLikeWithDateAsString<Input> | ExactlyLikeWithDateAsString<Input>[] {
if (Array.isArray(object)) {
return object.map((item) => convertDateFieldsToIsoString(item))
}

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] = convertDateFieldsToIsoString(value)
} else {
// @ts-ignore
result[key] = value
}
return result
}, {} as ExactlyLikeWithDateAsString<Input>)
}