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