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