3 var typeOf = require('kind-of');
4 var copyDescriptor = require('copy-descriptor');
5 var define = require('define-property');
8 * Copy static properties, prototype properties, and descriptors from one object to another.
12 * var proto = App.prototype;
13 * App.prototype.set = function() {};
14 * App.prototype.get = function() {};
19 * @param {Object} `receiver`
20 * @param {Object} `provider`
21 * @param {String|Array} `omit` One or more properties to omit
26 function copy(receiver, provider, omit) {
27 if (!isObject(receiver)) {
28 throw new TypeError('expected receiving object to be an object.');
30 if (!isObject(provider)) {
31 throw new TypeError('expected providing object to be an object.');
34 var props = nativeKeys(provider);
35 var keys = Object.keys(provider);
36 var len = props.length;
37 omit = arrayify(omit);
43 define(receiver, key, provider[key]);
44 } else if (!(key in receiver) && !has(omit, key)) {
45 copyDescriptor(receiver, provider, key);
51 * Return true if the given value is an object or function
54 function isObject(val) {
55 return typeOf(val) === 'object' || typeof val === 'function';
59 * Returns true if an array has any of the given elements, or an
60 * object has any of the give keys.
63 * has(['a', 'b', 'c'], 'c');
66 * has(['a', 'b', 'c'], ['c', 'z']);
69 * has({a: 'b', c: 'd'}, ['c', 'z']);
72 * @param {Object} `obj`
73 * @param {String|Array} `val`
77 function has(obj, val) {
82 for (var key in obj) {
83 if (val.indexOf(key) > -1) {
88 var keys = nativeKeys(obj);
89 return has(keys, val);
92 if (Array.isArray(obj)) {
95 if (arr.indexOf(val[len]) > -1) {
102 throw new TypeError('expected an array or object.');
106 * Cast the given value to an array.
116 * @param {String|Array} `val`
120 function arrayify(val) {
121 return val ? (Array.isArray(val) ? val : [val]) : [];
125 * Returns true if a value has a `contructor`
128 * hasConstructor({});
131 * hasConstructor(Object.create(null));
134 * @param {Object} `value`
138 function hasConstructor(val) {
139 return isObject(val) && typeof val.constructor !== 'undefined';
143 * Get the native `ownPropertyNames` from the constructor of the
144 * given `object`. An empty array is returned if the object does
145 * not have a constructor.
148 * nativeKeys({a: 'b', b: 'c', c: 'd'})
149 * //=> ['a', 'b', 'c']
151 * nativeKeys(function(){})
152 * //=> ['length', 'caller']
155 * @param {Object} `obj` Object that has a `constructor`.
156 * @return {Array} Array of keys.
159 function nativeKeys(val) {
160 if (!hasConstructor(val)) return [];
161 return Object.getOwnPropertyNames(val);
168 module.exports = copy;
171 * Expose `copy.has` for tests
174 module.exports.has = has;