index.js 616 B

1234567891011121314151617181920212223242526
  1. 'use strict';
  2. function mergeDescriptors(destination, source, overwrite = true) {
  3. if (!destination) {
  4. throw new TypeError('The `destination` argument is required.');
  5. }
  6. if (!source) {
  7. throw new TypeError('The `source` argument is required.');
  8. }
  9. for (const name of Object.getOwnPropertyNames(source)) {
  10. if (!overwrite && Object.hasOwn(destination, name)) {
  11. // Skip descriptor
  12. continue;
  13. }
  14. // Copy descriptor
  15. const descriptor = Object.getOwnPropertyDescriptor(source, name);
  16. Object.defineProperty(destination, name, descriptor);
  17. }
  18. return destination;
  19. }
  20. module.exports = mergeDescriptors;