Projekt

Obecné

Profil

Stáhnout (1.68 KB) Statistiky
| Větev: | Revize:
1
'use strict';
2

    
3
var callBound = require('../helpers/callBound');
4
var forEach = require('../helpers/forEach');
5
var OwnPropertyKeys = require('../helpers/OwnPropertyKeys');
6

    
7
var $isEnumerable = callBound('Object.prototype.propertyIsEnumerable');
8

    
9
var IsArray = require('./IsArray');
10
var IsPropertyKey = require('./IsPropertyKey');
11
var Type = require('./Type');
12

    
13
// https://www.ecma-international.org/ecma-262/9.0/#sec-copydataproperties
14

    
15
module.exports = function CopyDataProperties(target, source, excludedItems) {
16
	if (Type(target) !== 'Object') {
17
		throw new TypeError('Assertion failed: "target" must be an Object');
18
	}
19

    
20
	if (!IsArray(excludedItems)) {
21
		throw new TypeError('Assertion failed: "excludedItems" must be a List of Property Keys');
22
	}
23
	for (var i = 0; i < excludedItems.length; i += 1) {
24
		if (!IsPropertyKey(excludedItems[i])) {
25
			throw new TypeError('Assertion failed: "excludedItems" must be a List of Property Keys');
26
		}
27
	}
28

    
29
	if (typeof source === 'undefined' || source === null) {
30
		return target;
31
	}
32

    
33
	var ES = this;
34

    
35
	var fromObj = ES.ToObject(source);
36

    
37
	var sourceKeys = OwnPropertyKeys(fromObj);
38
	forEach(sourceKeys, function (nextKey) {
39
		var excluded = false;
40

    
41
		forEach(excludedItems, function (e) {
42
			if (ES.SameValue(e, nextKey) === true) {
43
				excluded = true;
44
			}
45
		});
46

    
47
		var enumerable = $isEnumerable(fromObj, nextKey) || (
48
		// this is to handle string keys being non-enumerable in older engines
49
			typeof source === 'string'
50
            && nextKey >= 0
51
            && ES.IsInteger(ES.ToNumber(nextKey))
52
		);
53
		if (excluded === false && enumerable) {
54
			var propValue = ES.Get(fromObj, nextKey);
55
			ES.CreateDataProperty(target, nextKey, propValue);
56
		}
57
	});
58

    
59
	return target;
60
};
(11-11/121)