export.js 2.6 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 defineBuiltIn = require('../internals/define-built-in');
  5. var defineGlobalProperty = require('../internals/define-global-property');
  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.dontCallGetSet - 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] || defineGlobalProperty(TARGET, {});
  32. } else {
  33. target = (global[TARGET] || {}).prototype;
  34. }
  35. if (target) for (key in source) {
  36. sourceProperty = source[key];
  37. if (options.dontCallGetSet) {
  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. defineBuiltIn(target, key, sourceProperty, options);
  52. }
  53. };