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