forked from sinonjs/sinon
/
extend.js
148 lines (135 loc) · 4.67 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
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
"use strict";
var arrayProto = require("@sinonjs/commons").prototypes.array;
var hasOwnProperty =
require("@sinonjs/commons").prototypes.object.hasOwnProperty;
var join = arrayProto.join;
var push = arrayProto.push;
// Adapted from https://developer.mozilla.org/en/docs/ECMAScript_DontEnum_attribute#JScript_DontEnum_Bug
var hasDontEnumBug = (function () {
var obj = {
constructor: function () {
return "0";
},
toString: function () {
return "1";
},
valueOf: function () {
return "2";
},
toLocaleString: function () {
return "3";
},
prototype: function () {
return "4";
},
isPrototypeOf: function () {
return "5";
},
propertyIsEnumerable: function () {
return "6";
},
hasOwnProperty: function () {
return "7";
},
length: function () {
return "8";
},
unique: function () {
return "9";
},
};
var result = [];
for (var prop in obj) {
if (hasOwnProperty(obj, prop)) {
push(result, obj[prop]());
}
}
return join(result, "") !== "0123456789";
})();
function extendCommon(target, sources, doCopy) {
var source, i, prop;
for (i = 0; i < sources.length; i++) {
source = sources[i];
for (prop in source) {
if (hasOwnProperty(source, prop)) {
doCopy(target, source, prop);
}
}
// Make sure we copy (own) toString method even when in JScript with DontEnum bug
// See https://developer.mozilla.org/en/docs/ECMAScript_DontEnum_attribute#JScript_DontEnum_Bug
if (
hasDontEnumBug &&
hasOwnProperty(source, "toString") &&
source.toString !== target.toString
) {
target.toString = source.toString;
}
}
return target;
}
/** Public: Extend target in place with all (own) properties, except 'name' when [[writable]] is false,
* from sources in-order. Thus, last source will override properties in previous sources.
*
* @param {object} target - The Object to extend
* @param {object[]} sources - Objects to copy properties from.
*
* @returns {object} the extended target
*/
module.exports = function extend(target, ...sources) {
return extendCommon(
target,
sources,
function copyValue(dest, source, prop) {
var destOwnPropertyDescriptor = Object.getOwnPropertyDescriptor(
dest,
prop
);
var sourceOwnPropertyDescriptor = Object.getOwnPropertyDescriptor(
source,
prop
);
if (prop === "name" && !destOwnPropertyDescriptor.writable) {
return;
}
const descriptors = {
configurable: sourceOwnPropertyDescriptor.configurable,
enumerable: sourceOwnPropertyDescriptor.enumerable
}
/*
if the sorce has an Accessor property copy over the accessor functions (get and set)
data properties has writable attribute where as acessor property don't
REF: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#properties
*/
if (hasOwnProperty(sourceOwnPropertyDescriptor, 'writable')) {
descriptors.writable = sourceOwnPropertyDescriptor.writable
descriptors.value = sourceOwnPropertyDescriptor.value
} else {
if (sourceOwnPropertyDescriptor.get) { descriptors.get = sourceOwnPropertyDescriptor.get.bind(dest) }
if (sourceOwnPropertyDescriptor.set) { descriptors.set = sourceOwnPropertyDescriptor.set.bind(dest) }
}
Object.defineProperty(dest, prop, descriptors);
}
);
};
/** Public: Extend target in place with all (own) properties from sources in-order. Thus, last source will
* override properties in previous sources. Define the properties as non enumerable.
*
* @param {object} target - The Object to extend
* @param {object[]} sources - Objects to copy properties from.
*
* @returns {object} the extended target
*/
module.exports.nonEnum = function extendNonEnum(target, ...sources) {
return extendCommon(
target,
sources,
function copyProperty(dest, source, prop) {
Object.defineProperty(dest, prop, {
value: source[prop],
enumerable: false,
configurable: true,
writable: true,
});
}
);
};