index.js 744 B

1234567891011121314151617181920212223
  1. 'use strict';
  2. var define = require('define-data-property');
  3. var hasDescriptors = require('has-property-descriptors')();
  4. var functionsHaveConfigurableNames = require('functions-have-names').functionsHaveConfigurableNames();
  5. var $TypeError = require('es-errors/type');
  6. /** @type {import('.')} */
  7. module.exports = function setFunctionName(fn, name) {
  8. if (typeof fn !== 'function') {
  9. throw new $TypeError('`fn` is not a function');
  10. }
  11. var loose = arguments.length > 2 && !!arguments[2];
  12. if (!loose || functionsHaveConfigurableNames) {
  13. if (hasDescriptors) {
  14. define(/** @type {Parameters<define>[0]} */ (fn), 'name', name, true, true);
  15. } else {
  16. define(/** @type {Parameters<define>[0]} */ (fn), 'name', name);
  17. }
  18. }
  19. return fn;
  20. };