12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- 'use strict';
- var uncurryThis = require('../internals/function-uncurry-this');
- var fails = require('../internals/fails');
- var isCallable = require('../internals/is-callable');
- var hasOwn = require('../internals/has-own-property');
- var DESCRIPTORS = require('../internals/descriptors');
- var CONFIGURABLE_FUNCTION_NAME = require('../internals/function-name').CONFIGURABLE;
- var inspectSource = require('../internals/inspect-source');
- var InternalStateModule = require('../internals/internal-state');
-
- var enforceInternalState = InternalStateModule.enforce;
- var getInternalState = InternalStateModule.get;
- var $String = String;
- // eslint-disable-next-line es/no-object-defineproperty -- safe
- var defineProperty = Object.defineProperty;
- var stringSlice = uncurryThis(''.slice);
- var replace = uncurryThis(''.replace);
- var join = uncurryThis([].join);
-
- var CONFIGURABLE_LENGTH = DESCRIPTORS && !fails(function () {
- return defineProperty(function () { /* empty */ }, 'length', { value: 8 }).length !== 8;
- });
-
- var TEMPLATE = String(String).split('String');
-
- var makeBuiltIn = module.exports = function (value, name, options) {
- if (stringSlice($String(name), 0, 7) === 'Symbol(') {
- name = '[' + replace($String(name), /^Symbol\(([^)]*)\).*$/, '$1') + ']';
- }
- if (options && options.getter) name = 'get ' + name;
- if (options && options.setter) name = 'set ' + name;
- if (!hasOwn(value, 'name') || (CONFIGURABLE_FUNCTION_NAME && value.name !== name)) {
- if (DESCRIPTORS) defineProperty(value, 'name', { value: name, configurable: true });
- else value.name = name;
- }
- if (CONFIGURABLE_LENGTH && options && hasOwn(options, 'arity') && value.length !== options.arity) {
- defineProperty(value, 'length', { value: options.arity });
- }
- try {
- if (options && hasOwn(options, 'constructor') && options.constructor) {
- if (DESCRIPTORS) defineProperty(value, 'prototype', { writable: false });
- // in V8 ~ Chrome 53, prototypes of some methods, like `Array.prototype.values`, are non-writable
- } else if (value.prototype) value.prototype = undefined;
- } catch (error) { /* empty */ }
- var state = enforceInternalState(value);
- if (!hasOwn(state, 'source')) {
- state.source = join(TEMPLATE, typeof name == 'string' ? name : '');
- } return value;
- };
-
- // add fake Function#toString for correct work wrapped methods / constructors with methods like LoDash isNative
- // eslint-disable-next-line no-extend-native -- required
- Function.prototype.toString = makeBuiltIn(function toString() {
- return isCallable(this) && getInternalState(this).source || inspectSource(this);
- }, 'toString');
|