[d565449] | 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 some = require('../helpers/some');
|
---|
| 9 | var OwnPropertyKeys = require('../helpers/OwnPropertyKeys');
|
---|
| 10 |
|
---|
| 11 | var $isEnumerable = callBound('Object.prototype.propertyIsEnumerable');
|
---|
| 12 |
|
---|
| 13 | var CreateDataPropertyOrThrow = require('./CreateDataPropertyOrThrow');
|
---|
| 14 | var Get = require('./Get');
|
---|
| 15 | var IsArray = require('./IsArray');
|
---|
| 16 | var IsIntegralNumber = require('./IsIntegralNumber');
|
---|
| 17 | var IsPropertyKey = require('./IsPropertyKey');
|
---|
| 18 | var SameValue = require('./SameValue');
|
---|
| 19 | var ToNumber = require('./ToNumber');
|
---|
| 20 | var ToObject = require('./ToObject');
|
---|
| 21 | var Type = require('./Type');
|
---|
| 22 |
|
---|
| 23 | // https://262.ecma-international.org/12.0/#sec-copydataproperties
|
---|
| 24 |
|
---|
| 25 | module.exports = function CopyDataProperties(target, source, excludedItems) {
|
---|
| 26 | if (Type(target) !== 'Object') {
|
---|
| 27 | throw new $TypeError('Assertion failed: "target" must be an Object');
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | if (!IsArray(excludedItems) || !every(excludedItems, IsPropertyKey)) {
|
---|
| 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 keys = OwnPropertyKeys(from);
|
---|
| 41 | forEach(keys, function (nextKey) {
|
---|
| 42 | var excluded = some(excludedItems, function (e) {
|
---|
| 43 | return SameValue(e, nextKey) === true;
|
---|
| 44 | });
|
---|
| 45 | /*
|
---|
| 46 | var excluded = false;
|
---|
| 47 |
|
---|
| 48 | forEach(excludedItems, function (e) {
|
---|
| 49 | if (SameValue(e, nextKey) === true) {
|
---|
| 50 | excluded = true;
|
---|
| 51 | }
|
---|
| 52 | });
|
---|
| 53 | */
|
---|
| 54 |
|
---|
| 55 | var enumerable = $isEnumerable(from, nextKey) || (
|
---|
| 56 | // this is to handle string keys being non-enumerable in older engines
|
---|
| 57 | typeof source === 'string'
|
---|
| 58 | && nextKey >= 0
|
---|
| 59 | && IsIntegralNumber(ToNumber(nextKey))
|
---|
| 60 | );
|
---|
| 61 | if (excluded === false && enumerable) {
|
---|
| 62 | var propValue = Get(from, nextKey);
|
---|
| 63 | CreateDataPropertyOrThrow(target, nextKey, propValue);
|
---|
| 64 | }
|
---|
| 65 | });
|
---|
| 66 |
|
---|
| 67 | return target;
|
---|
| 68 | };
|
---|