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