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 fields to iso string array handling #80

Merged
merged 4 commits into from
Nov 17, 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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-prettier": "^5.0.1",
"eslint-plugin-vitest": "^0.3.8",
"eslint-plugin-vitest": "^0.3.9",
"prettier": "^3.0.3",
"typescript": "^5.2.2",
"vitest": "^0.34.6",
"zod": "^3.22.4"
"zod": "^3.22.4",
"vite": "4.5.0"
}
}
42 changes: 40 additions & 2 deletions src/utils/objectUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, expectTypeOf } from 'vitest'
import { expect } from 'vitest'

import {
convertDateFieldsToIsoString,
Expand Down Expand Up @@ -542,7 +542,6 @@ describe('objectUtils', () => {
code: 100,
reason: 'reason',
})
expectTypeOf(output).toMatchTypeOf<TestExpectedType>()
})

it('simple array', () => {
Expand Down Expand Up @@ -608,6 +607,45 @@ describe('objectUtils', () => {
})
})

it('properly handles all types of arrays', () => {
const date = new Date()
const input = {
array1: [date, date],
array2: [1, 2],
array3: ['a', 'b'],
array4: [
{ id: 1, value: 'value', date, code: 100 } satisfies TestInputType,
{ id: 2, value: 'value2', date, code: 200 } satisfies TestInputType,
],
array5: [1, date, 'a', { id: 1, value: 'value', date, code: 100 } satisfies TestInputType],
}

type Expected = {
array1: string[]
array2: number[]
array3: string[]
array4: TestExpectedType[]
array5: (number | string | TestExpectedType)[]
}
const output: Expected = convertDateFieldsToIsoString(input)

expect(output).toStrictEqual({
array1: [date.toISOString(), date.toISOString()],
array2: [1, 2],
array3: ['a', 'b'],
array4: [
{ id: 1, value: 'value', date: date.toISOString(), code: 100 },
{ id: 2, value: 'value2', date: date.toISOString(), code: 200 },
],
array5: [
1,
date.toISOString(),
'a',
{ id: 1, value: 'value', date: date.toISOString(), code: 100 },
],
})
})

it('nested objects and array', () => {
const date1 = new Date()
const date2 = new Date()
Expand Down
28 changes: 17 additions & 11 deletions src/utils/objectUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,26 @@ export function convertDateFieldsToIsoString<Input extends object>(
object: Input | Input[],
): ExactlyLikeWithDateAsString<Input> | ExactlyLikeWithDateAsString<Input>[] {
if (Array.isArray(object)) {
return object.map((item) => convertDateFieldsToIsoString(item))
// @ts-ignore
return object.map(convertDateFieldsToIsoStringAux)
}

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

function convertDateFieldsToIsoStringAux<T>(item: T): DatesAsString<T> {
if (item instanceof Date) {
// @ts-ignore
return item.toISOString()
} else if (item && typeof item === 'object') {
// @ts-ignore
return convertDateFieldsToIsoString(item)
} else {
// @ts-ignore
return item
}
}