Node-Red configuration
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.

make-built-in.js 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict';
  2. var uncurryThis = require('../internals/function-uncurry-this');
  3. var fails = require('../internals/fails');
  4. var isCallable = require('../internals/is-callable');
  5. var hasOwn = require('../internals/has-own-property');
  6. var DESCRIPTORS = require('../internals/descriptors');
  7. var CONFIGURABLE_FUNCTION_NAME = require('../internals/function-name').CONFIGURABLE;
  8. var inspectSource = require('../internals/inspect-source');
  9. var InternalStateModule = require('../internals/internal-state');
  10. var enforceInternalState = InternalStateModule.enforce;
  11. var getInternalState = InternalStateModule.get;
  12. var $String = String;
  13. // eslint-disable-next-line es/no-object-defineproperty -- safe
  14. var defineProperty = Object.defineProperty;
  15. var stringSlice = uncurryThis(''.slice);
  16. var replace = uncurryThis(''.replace);
  17. var join = uncurryThis([].join);
  18. var CONFIGURABLE_LENGTH = DESCRIPTORS && !fails(function () {
  19. return defineProperty(function () { /* empty */ }, 'length', { value: 8 }).length !== 8;
  20. });
  21. var TEMPLATE = String(String).split('String');
  22. var makeBuiltIn = module.exports = function (value, name, options) {
  23. if (stringSlice($String(name), 0, 7) === 'Symbol(') {
  24. name = '[' + replace($String(name), /^Symbol\(([^)]*)\).*$/, '$1') + ']';
  25. }
  26. if (options && options.getter) name = 'get ' + name;
  27. if (options && options.setter) name = 'set ' + name;
  28. if (!hasOwn(value, 'name') || (CONFIGURABLE_FUNCTION_NAME && value.name !== name)) {
  29. if (DESCRIPTORS) defineProperty(value, 'name', { value: name, configurable: true });
  30. else value.name = name;
  31. }
  32. if (CONFIGURABLE_LENGTH && options && hasOwn(options, 'arity') && value.length !== options.arity) {
  33. defineProperty(value, 'length', { value: options.arity });
  34. }
  35. try {
  36. if (options && hasOwn(options, 'constructor') && options.constructor) {
  37. if (DESCRIPTORS) defineProperty(value, 'prototype', { writable: false });
  38. // in V8 ~ Chrome 53, prototypes of some methods, like `Array.prototype.values`, are non-writable
  39. } else if (value.prototype) value.prototype = undefined;
  40. } catch (error) { /* empty */ }
  41. var state = enforceInternalState(value);
  42. if (!hasOwn(state, 'source')) {
  43. state.source = join(TEMPLATE, typeof name == 'string' ? name : '');
  44. } return value;
  45. };
  46. // add fake Function#toString for correct work wrapped methods / constructors with methods like LoDash isNative
  47. // eslint-disable-next-line no-extend-native -- required
  48. Function.prototype.toString = makeBuiltIn(function toString() {
  49. return isCallable(this) && getInternalState(this).source || inspectSource(this);
  50. }, 'toString');