index.js (1215B)
1 /*! 2 * merge-descriptors 3 * Copyright(c) 2014 Jonathan Ong 4 * Copyright(c) 2015 Douglas Christopher Wilson 5 * MIT Licensed 6 */ 7 8 'use strict' 9 10 /** 11 * Module exports. 12 * @public 13 */ 14 15 module.exports = merge 16 17 /** 18 * Module variables. 19 * @private 20 */ 21 22 var hasOwnProperty = Object.prototype.hasOwnProperty 23 24 /** 25 * Merge the property descriptors of `src` into `dest` 26 * 27 * @param {object} dest Object to add descriptors to 28 * @param {object} src Object to clone descriptors from 29 * @param {boolean} [redefine=true] Redefine `dest` properties with `src` properties 30 * @returns {object} Reference to dest 31 * @public 32 */ 33 34 function merge(dest, src, redefine) { 35 if (!dest) { 36 throw new TypeError('argument dest is required') 37 } 38 39 if (!src) { 40 throw new TypeError('argument src is required') 41 } 42 43 if (redefine === undefined) { 44 // Default to true 45 redefine = true 46 } 47 48 Object.getOwnPropertyNames(src).forEach(function forEachOwnPropertyName(name) { 49 if (!redefine && hasOwnProperty.call(dest, name)) { 50 // Skip desriptor 51 return 52 } 53 54 // Copy descriptor 55 var descriptor = Object.getOwnPropertyDescriptor(src, name) 56 Object.defineProperty(dest, name, descriptor) 57 }) 58 59 return dest 60 }