1 | 'use strict';
|
---|
2 |
|
---|
3 | var $TypeError = require('es-errors/type');
|
---|
4 |
|
---|
5 | var callBound = require('call-bind/callBound');
|
---|
6 | var forEach = require('../helpers/forEach');
|
---|
7 | var every = require('../helpers/every');
|
---|
8 | var OwnPropertyKeys = require('../helpers/OwnPropertyKeys');
|
---|
9 |
|
---|
10 | var $isEnumerable = callBound('Object.prototype.propertyIsEnumerable');
|
---|
11 |
|
---|
12 | var CreateDataPropertyOrThrow = require('./CreateDataPropertyOrThrow');
|
---|
13 | var Get = require('./Get');
|
---|
14 | var IsArray = require('./IsArray');
|
---|
15 | var IsInteger = require('./IsInteger');
|
---|
16 | var IsPropertyKey = require('./IsPropertyKey');
|
---|
17 | var SameValue = require('./SameValue');
|
---|
18 | var ToNumber = require('./ToNumber');
|
---|
19 | var ToObject = require('./ToObject');
|
---|
20 | var Type = require('./Type');
|
---|
21 |
|
---|
22 | // https://262.ecma-international.org/11.0/#sec-copydataproperties
|
---|
23 |
|
---|
24 | module.exports = function CopyDataProperties(target, source, excludedItems) {
|
---|
25 | if (Type(target) !== 'Object') {
|
---|
26 | throw new $TypeError('Assertion failed: "target" must be an Object');
|
---|
27 | }
|
---|
28 |
|
---|
29 | if (!IsArray(excludedItems) || !every(excludedItems, IsPropertyKey)) {
|
---|
30 | throw new $TypeError('Assertion failed: "excludedItems" must be a List of Property Keys');
|
---|
31 | }
|
---|
32 |
|
---|
33 | if (typeof source === 'undefined' || source === null) {
|
---|
34 | return target;
|
---|
35 | }
|
---|
36 |
|
---|
37 | var from = ToObject(source);
|
---|
38 |
|
---|
39 | var sourceKeys = OwnPropertyKeys(from);
|
---|
40 | forEach(sourceKeys, function (nextKey) {
|
---|
41 | var excluded = false;
|
---|
42 |
|
---|
43 | forEach(excludedItems, function (e) {
|
---|
44 | if (SameValue(e, nextKey) === true) {
|
---|
45 | excluded = true;
|
---|
46 | }
|
---|
47 | });
|
---|
48 |
|
---|
49 | var enumerable = $isEnumerable(from, nextKey) || (
|
---|
50 | // this is to handle string keys being non-enumerable in older engines
|
---|
51 | typeof source === 'string'
|
---|
52 | && nextKey >= 0
|
---|
53 | && IsInteger(ToNumber(nextKey))
|
---|
54 | );
|
---|
55 | if (excluded === false && enumerable) {
|
---|
56 | var propValue = Get(from, nextKey);
|
---|
57 | CreateDataPropertyOrThrow(target, nextKey, propValue);
|
---|
58 | }
|
---|
59 | });
|
---|
60 |
|
---|
61 | return target;
|
---|
62 | };
|
---|