e93c66456115ddff4065ed5e421982a352c6336c8b597524ddd2eb6bc49a5ce8b08dbe31efcd50f394ba05825240cd5d8c4e19eccf616e5c6d6b3073e0a654 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # set-function-name <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
  2. [![github actions][actions-image]][actions-url]
  3. [![coverage][codecov-image]][codecov-url]
  4. [![License][license-image]][license-url]
  5. [![Downloads][downloads-image]][downloads-url]
  6. [![npm badge][npm-badge-png]][package-url]
  7. Set a function’s name.
  8. Arguments:
  9. - `fn`: the function
  10. - `name`: the new name
  11. - `loose`: Optional. If true, and the name fails to be set, do not throw. Default false.
  12. Returns `fn`.
  13. ## Usage
  14. ```javascript
  15. var setFunctionName = require('set-function-name');
  16. var assert = require('assert');
  17. const obj = {
  18. concise() {},
  19. arrow: () => {},
  20. named: function named() {},
  21. anon: function () {},
  22. };
  23. assert.equal(obj.concise.name, 'concise');
  24. assert.equal(obj.arrow.name, 'arrow');
  25. assert.equal(obj.named.name, 'named');
  26. assert.equal(obj.anon.name, 'anon');
  27. assert.equal(setFunctionName(obj.concise, 'brief'), obj.concise);
  28. assert.equal(setFunctionName(obj.arrow, 'pointy'), obj.arrow);
  29. assert.equal(setFunctionName(obj.named, ''), obj.named);
  30. assert.equal(setFunctionName(obj.anon, 'anonymous'), obj.anon);
  31. assert.equal(obj.concise.name, 'brief');
  32. assert.equal(obj.arrow.name, 'pointy');
  33. assert.equal(obj.named.name, '');
  34. assert.equal(obj.anon.name, 'anonymous');
  35. ```
  36. [package-url]: https://npmjs.org/package/set-function-name
  37. [npm-version-svg]: https://versionbadg.es/ljharb/set-function-name.svg
  38. [deps-svg]: https://david-dm.org/ljharb/set-function-name.svg
  39. [deps-url]: https://david-dm.org/ljharb/set-function-name
  40. [dev-deps-svg]: https://david-dm.org/ljharb/set-function-name/dev-status.svg
  41. [dev-deps-url]: https://david-dm.org/ljharb/set-function-name#info=devDependencies
  42. [npm-badge-png]: https://nodei.co/npm/set-function-name.png?downloads=true&stars=true
  43. [license-image]: https://img.shields.io/npm/l/set-function-name.svg
  44. [license-url]: LICENSE
  45. [downloads-image]: https://img.shields.io/npm/dm/set-function-name.svg
  46. [downloads-url]: https://npm-stat.com/charts.html?package=set-function-name
  47. [codecov-image]: https://codecov.io/gh/ljharb/set-function-name/branch/main/graphs/badge.svg
  48. [codecov-url]: https://app.codecov.io/gh/ljharb/set-function-name/
  49. [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/ljharb/set-function-name
  50. [actions-url]: https://github.com/ljharb/set-function-name/actions