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

overwrite - handle overwrite of non-empty objects with plain values #648

Merged
merged 1 commit into from
Sep 10, 2024
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
17 changes: 17 additions & 0 deletions src/utils/setByPath.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,21 @@ describe('setByPath', () => {
numbers: [98618922, -367]
})
})

it('should replace an existing object with a primitive value', () => {
const objUnderTest = {
event: {
startDateTime: {
format: 'date-time'
}
}
}

const result = setByPath(objUnderTest, 'event.startDateTime', '2002-12-01')
expect(result).toEqual({
event: {
startDateTime: '2002-12-01'
}
})
})
})
7 changes: 7 additions & 0 deletions src/utils/setByPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ export const setByPath = (
dot.keepArray = true
}

// Check if the path leads to an object that should be replaced
const currentValue = dot.pick(path, objectOrArray)
if (isObject(currentValue) && !isObject(newValue)) {
// Directly replace the object with the new value
dot.del(path, objectOrArray)
}

// Handle array of objects
if (Array.isArray(objectOrArray)) {
// Handle array definition (example: [0])
Expand Down
Loading