PageRenderTime 27ms CodeModel.GetById 20ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/node_modules/merge-descriptors/index.js

https://bitbucket.org/coleman333/smartsite
JavaScript | 60 lines | 22 code | 12 blank | 26 comment | 5 complexity | b4d3859e603602c87a45682862055af0 MD5 | raw file
 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
15module.exports = merge
16
17/**
18 * Module variables.
19 * @private
20 */
21
22var 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
34function 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}