.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / copy-descriptor / index.js
1 /*!
2  * copy-descriptor <https://github.com/jonschlinkert/copy-descriptor>
3  *
4  * Copyright (c) 2015, Jon Schlinkert.
5  * Licensed under the MIT License.
6  */
7
8 'use strict';
9
10 /**
11  * Copy a descriptor from one object to another.
12  *
13  * ```js
14  * function App() {
15  *   this.cache = {};
16  * }
17  * App.prototype.set = function(key, val) {
18  *   this.cache[key] = val;
19  *   return this;
20  * };
21  * Object.defineProperty(App.prototype, 'count', {
22  *   get: function() {
23  *     return Object.keys(this.cache).length;
24  *   }
25  * });
26  *
27  * copy(App.prototype, 'count', 'len');
28  *
29  * // create an instance
30  * var app = new App();
31  *
32  * app.set('a', true);
33  * app.set('b', true);
34  * app.set('c', true);
35  *
36  * console.log(app.count);
37  * //=> 3
38  * console.log(app.len);
39  * //=> 3
40  * ```
41  * @name copy
42  * @param {Object} `receiver` The target object
43  * @param {Object} `provider` The provider object
44  * @param {String} `from` The key to copy on provider.
45  * @param {String} `to` Optionally specify a new key name to use.
46  * @return {Object}
47  * @api public
48  */
49
50 module.exports = function copyDescriptor(receiver, provider, from, to) {
51   if (!isObject(provider) && typeof provider !== 'function') {
52     to = from;
53     from = provider;
54     provider = receiver;
55   }
56   if (!isObject(receiver) && typeof receiver !== 'function') {
57     throw new TypeError('expected the first argument to be an object');
58   }
59   if (!isObject(provider) && typeof provider !== 'function') {
60     throw new TypeError('expected provider to be an object');
61   }
62
63   if (typeof to !== 'string') {
64     to = from;
65   }
66   if (typeof from !== 'string') {
67     throw new TypeError('expected key to be a string');
68   }
69
70   if (!(from in provider)) {
71     throw new Error('property "' + from + '" does not exist');
72   }
73
74   var val = Object.getOwnPropertyDescriptor(provider, from);
75   if (val) Object.defineProperty(receiver, to, val);
76 };
77
78 function isObject(val) {
79   return {}.toString.call(val) === '[object Object]';
80 }
81