export.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. var global = require('../internals/global');
  2. var getOwnPropertyDescriptor = require('../internals/object-get-own-property-descriptor').f;
  3. var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
  4. var redefine = require('../internals/redefine');
  5. var setGlobal = require('../internals/set-global');
  6. var copyConstructorProperties = require('../internals/copy-constructor-properties');
  7. var isForced = require('../internals/is-forced');
  8. /*
  9. options.target - name of the target object
  10. options.global - target is the global object
  11. options.stat - export as static methods of target
  12. options.proto - export as prototype methods of target
  13. options.real - real prototype method for the `pure` version
  14. options.forced - export even if the native feature is available
  15. options.bind - bind methods to the target, required for the `pure` version
  16. options.wrap - wrap constructors to preventing global pollution, required for the `pure` version
  17. options.unsafe - use the simple assignment of property instead of delete + defineProperty
  18. options.sham - add a flag to not completely full polyfills
  19. options.enumerable - export as enumerable property
  20. options.noTargetGet - prevent calling a getter on target
  21. */
  22. module.exports = function (options, source) {
  23. var TARGET = options.target;
  24. var GLOBAL = options.global;
  25. var STATIC = options.stat;
  26. var FORCED, target, key, targetProperty, sourceProperty, descriptor;
  27. if (GLOBAL) {
  28. target = global;
  29. } else if (STATIC) {
  30. target = global[TARGET] || setGlobal(TARGET, {});
  31. } else {
  32. target = (global[TARGET] || {}).prototype;
  33. }
  34. if (target) for (key in source) {
  35. sourceProperty = source[key];
  36. if (options.noTargetGet) {
  37. descriptor = getOwnPropertyDescriptor(target, key);
  38. targetProperty = descriptor && descriptor.value;
  39. } else targetProperty = target[key];
  40. FORCED = isForced(GLOBAL ? key : TARGET + (STATIC ? '.' : '#') + key, options.forced);
  41. // contained in target
  42. if (!FORCED && targetProperty !== undefined) {
  43. if (typeof sourceProperty === typeof targetProperty) continue;
  44. copyConstructorProperties(sourceProperty, targetProperty);
  45. }
  46. // add a flag to not completely full polyfills
  47. if (options.sham || (targetProperty && targetProperty.sham)) {
  48. createNonEnumerableProperty(sourceProperty, 'sham', true);
  49. }
  50. // extend global
  51. redefine(target, key, sourceProperty, options);
  52. }
  53. };