index.js 623 B

12345678910111213141516171819202122
  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 = TypeError;
  6. module.exports = function setFunctionName(fn, name) {
  7. if (typeof fn !== 'function') {
  8. throw new $TypeError('`fn` is not a function');
  9. }
  10. var loose = arguments.length > 2 && !!arguments[2];
  11. if (!loose || functionsHaveConfigurableNames) {
  12. if (hasDescriptors) {
  13. define(fn, 'name', name, true, true);
  14. } else {
  15. define(fn, 'name', name);
  16. }
  17. }
  18. return fn;
  19. };