You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CopyDataProperties.js 1.7KB

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