Skip to content
Open
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
20 changes: 20 additions & 0 deletions src/utils/__tests__/transformObjectProperties.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ const snakeCaseObject = {
family_name: 'Pacino'
}
],
consents: {
optin_testing: {
granted: true,
consent_type: "opt-in",
consent_version: {
version_id: 1,
language: "fr"
}
}
},
tags: ['actor', 'american'],
is_alive: true,
test3: true
Expand All @@ -47,6 +57,16 @@ const camelCaseObject = {
familyName: 'Pacino'
}
],
consents: {
optin_testing: { // consent key should remain in snakecase
granted: true,
consentType: "opt-in",
consentVersion: {
versionId: 1,
language: "fr"
}
}
},
Comment on lines +60 to +69
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Jusqu'à présent toutes les clés enfants étaient aussi conservés en snakecase.
Or on veut que leurs lés clés des consentements restent en snakecase, mais les propriétés comme consentType, consentVersion et versionId peuvent et devraient être converti en camelcase.

tags: ['actor', 'american'],
isAlive: true,
test3: true
Expand Down
8 changes: 3 additions & 5 deletions src/utils/transformObjectProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,16 @@ type TransformObjectProperties<T> = T extends (infer U)[]
? { [K in keyof T]: TransformObjectProperties<T[K]> }
: T;

function transformObjectProperties<T>(input: T, transform: (path: string) => string): TransformObjectProperties<T> {
function transformObjectProperties<T>(input: T, transform: (path: string) => string, bypass: boolean = false): TransformObjectProperties<T> {
if (Array.isArray(input)) {
return input.map(value => transformObjectProperties(value, transform)) as TransformObjectProperties<T>
}
if (typeof input === "object" && input !== null) {
return Object.fromEntries(
Object.entries(input).map(([key, value]) => (
[
transform(key) as keyof T,
fieldsNotToConvert.find(s => s === snakeCase(key))
? value
: transformObjectProperties(value, transform)
bypass ? key : transform(key) as keyof T,
transformObjectProperties(value, transform, fieldsNotToConvert.includes(snakeCase(key)))
]
))
) as TransformObjectProperties<T>
Expand Down