-
Notifications
You must be signed in to change notification settings - Fork 104
/
extend.js
42 lines (36 loc) · 1.13 KB
/
extend.js
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
import type from "./type.js";
// Copy properties from one object to another. Overwrites allowed.
// Subtle difference of array vs string whitelist: If property doesn't exist in from, array will not define it.
export default function extend(to, from, whitelist) {
var whitelistType = type(whitelist);
if (whitelistType === "string") {
// To copy gettters/setters, preserve flags etc
var descriptor = Object.getOwnPropertyDescriptor(from, whitelist);
if (descriptor && (!descriptor.writable || !descriptor.configurable || !descriptor.enumerable || descriptor.get || descriptor.set)) {
delete to[whitelist];
Object.defineProperty(to, whitelist, descriptor);
}
else {
to[whitelist] = from[whitelist];
}
}
else if (whitelistType === "array") {
whitelist.forEach(function(property) {
if (property in from) {
extend(to, from, property);
}
});
}
else {
for (var property in from) {
if (whitelist) {
if (whitelistType === "regexp" && !whitelist.test(property) ||
whitelistType === "function" && !whitelist.call(from, property)) {
continue;
}
}
extend(to, from, property);
}
}
return to;
}