index.js
3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict';
var typeOf = require('kind-of');
var copyDescriptor = require('copy-descriptor');
var define = require('define-property');
/**
* Copy static properties, prototype properties, and descriptors from one object to another.
*
* ```js
* function App() {}
* var proto = App.prototype;
* App.prototype.set = function() {};
* App.prototype.get = function() {};
*
* var obj = {};
* copy(obj, proto);
* ```
* @param {Object} `receiver`
* @param {Object} `provider`
* @param {String|Array} `omit` One or more properties to omit
* @return {Object}
* @api public
*/
function copy(receiver, provider, omit) {
if (!isObject(receiver)) {
throw new TypeError('expected receiving object to be an object.');
}
if (!isObject(provider)) {
throw new TypeError('expected providing object to be an object.');
}
var props = nativeKeys(provider);
var keys = Object.keys(provider);
var len = props.length;
omit = arrayify(omit);
while (len--) {
var key = props[len];
if (has(keys, key)) {
define(receiver, key, provider[key]);
} else if (!(key in receiver) && !has(omit, key)) {
copyDescriptor(receiver, provider, key);
}
}
};
/**
* Return true if the given value is an object or function
*/
function isObject(val) {
return typeOf(val) === 'object' || typeof val === 'function';
}
/**
* Returns true if an array has any of the given elements, or an
* object has any of the give keys.
*
* ```js
* has(['a', 'b', 'c'], 'c');
* //=> true
*
* has(['a', 'b', 'c'], ['c', 'z']);
* //=> true
*
* has({a: 'b', c: 'd'}, ['c', 'z']);
* //=> true
* ```
* @param {Object} `obj`
* @param {String|Array} `val`
* @return {Boolean}
*/
function has(obj, val) {
val = arrayify(val);
var len = val.length;
if (isObject(obj)) {
for (var key in obj) {
if (val.indexOf(key) > -1) {
return true;
}
}
var keys = nativeKeys(obj);
return has(keys, val);
}
if (Array.isArray(obj)) {
var arr = obj;
while (len--) {
if (arr.indexOf(val[len]) > -1) {
return true;
}
}
return false;
}
throw new TypeError('expected an array or object.');
}
/**
* Cast the given value to an array.
*
* ```js
* arrayify('foo');
* //=> ['foo']
*
* arrayify(['foo']);
* //=> ['foo']
* ```
*
* @param {String|Array} `val`
* @return {Array}
*/
function arrayify(val) {
return val ? (Array.isArray(val) ? val : [val]) : [];
}
/**
* Returns true if a value has a `contructor`
*
* ```js
* hasConstructor({});
* //=> true
*
* hasConstructor(Object.create(null));
* //=> false
* ```
* @param {Object} `value`
* @return {Boolean}
*/
function hasConstructor(val) {
return isObject(val) && typeof val.constructor !== 'undefined';
}
/**
* Get the native `ownPropertyNames` from the constructor of the
* given `object`. An empty array is returned if the object does
* not have a constructor.
*
* ```js
* nativeKeys({a: 'b', b: 'c', c: 'd'})
* //=> ['a', 'b', 'c']
*
* nativeKeys(function(){})
* //=> ['length', 'caller']
* ```
*
* @param {Object} `obj` Object that has a `constructor`.
* @return {Array} Array of keys.
*/
function nativeKeys(val) {
if (!hasConstructor(val)) return [];
return Object.getOwnPropertyNames(val);
}
/**
* Expose `copy`
*/
module.exports = copy;
/**
* Expose `copy.has` for tests
*/
module.exports.has = has;