export.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. options.name - the .name of the function if it does not match the key
  22. */
  23. module.exports = function (options, source) {
  24. var TARGET = options.target;
  25. var GLOBAL = options.global;
  26. var STATIC = options.stat;
  27. var FORCED, target, key, targetProperty, sourceProperty, descriptor;
  28. if (GLOBAL) {
  29. target = global;
  30. } else if (STATIC) {
  31. target = global[TARGET] || setGlobal(TARGET, {});
  32. } else {
  33. target = (global[TARGET] || {}).prototype;
  34. }
  35. if (target) for (key in source) {
  36. sourceProperty = source[key];
  37. if (options.noTargetGet) {
  38. descriptor = getOwnPropertyDescriptor(target, key);
  39. targetProperty = descriptor && descriptor.value;
  40. } else targetProperty = target[key];
  41. FORCED = isForced(GLOBAL ? key : TARGET + (STATIC ? '.' : '#') + key, options.forced);
  42. // contained in target
  43. if (!FORCED && targetProperty !== undefined) {
  44. if (typeof sourceProperty == typeof targetProperty) continue;
  45. copyConstructorProperties(sourceProperty, targetProperty);
  46. }
  47. // add a flag to not completely full polyfills
  48. if (options.sham || (targetProperty && targetProperty.sham)) {
  49. createNonEnumerableProperty(sourceProperty, 'sham', true);
  50. }
  51. // extend global
  52. redefine(target, key, sourceProperty, options);
  53. }
  54. };