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

This changes whether a property is writable or configurable #20

Closed
ehmicky opened this issue Jul 23, 2022 · 2 comments · Fixed by #22
Closed

This changes whether a property is writable or configurable #20

ehmicky opened this issue Jul 23, 2022 · 2 comments · Fixed by #22

Comments

@ehmicky
Copy link
Contributor

ehmicky commented Jul 23, 2022

The new properties returned by this library are always writable and configurable, even when the original properties were not. While non-writable or non-configurable properties are sometimes problematic, there are sometimes legitimate reasons for it. This seems to be an unintentional side effect.

const obj = Object.defineProperty({}, 'prop', { value: true, enumerable: true, writable: false, configurable: false })
const objCopy = includeKeys(obj, ['prop'])
console.log(Object.getOwnPropertyDescriptor(objCopy, 'prop'))
// { value: true, writable: true, enumerable: true, configurable: true }

Additionally, when a property is using get/set, they are currently removed.

const obj = Object.defineProperty({}, 'prop', { get: () => Math.random(), enumerable: true })
console.log(obj.prop, obj.prop) // Different random numbers
const objCopy = includeKeys(obj, ['prop'])
console.log(objCopy.prop, objCopy.prop) // Same random numbers

Instead of retrieving then copying the property values, the descriptors should be used instead.

const value = object[key];

const value = object[key];

Note: I can submit a PR.

@sindresorhus
Copy link
Owner

Thanks for catching that. PR welcome.

@ehmicky
Copy link
Contributor Author

ehmicky commented Jul 24, 2022

Done in #22.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants