redefine.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334
  1. var global = require('../internals/global');
  2. var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
  3. var has = require('../internals/has');
  4. var setGlobal = require('../internals/set-global');
  5. var inspectSource = require('../internals/inspect-source');
  6. var InternalStateModule = require('../internals/internal-state');
  7. var getInternalState = InternalStateModule.get;
  8. var enforceInternalState = InternalStateModule.enforce;
  9. var TEMPLATE = String(String).split('String');
  10. (module.exports = function (O, key, value, options) {
  11. var unsafe = options ? !!options.unsafe : false;
  12. var simple = options ? !!options.enumerable : false;
  13. var noTargetGet = options ? !!options.noTargetGet : false;
  14. if (typeof value == 'function') {
  15. if (typeof key == 'string' && !has(value, 'name')) createNonEnumerableProperty(value, 'name', key);
  16. enforceInternalState(value).source = TEMPLATE.join(typeof key == 'string' ? key : '');
  17. }
  18. if (O === global) {
  19. if (simple) O[key] = value;
  20. else setGlobal(key, value);
  21. return;
  22. } else if (!unsafe) {
  23. delete O[key];
  24. } else if (!noTargetGet && O[key]) {
  25. simple = true;
  26. }
  27. if (simple) O[key] = value;
  28. else createNonEnumerableProperty(O, key, value);
  29. // add fake Function#toString for correct work wrapped methods / constructors with methods like LoDash isNative
  30. })(Function.prototype, 'toString', function toString() {
  31. return typeof this == 'function' && getInternalState(this).source || inspectSource(this);
  32. });