Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

set-function-name

ljharb122.6mMIT2.0.2TypeScript support: included

Set a function's name property

set, assign, function, name, function.name

readme

set-function-name Version Badge

github actions coverage License Downloads

npm badge

Set a function’s name.

Arguments:

  • fn: the function
  • name: the new name
  • loose: Optional. If true, and the name fails to be set, do not throw. Default false.

Returns fn.

Usage

var setFunctionName = require('set-function-name');
var assert = require('assert');

const obj = {
    concise() {},
    arrow: () => {},
    named: function named() {},
    anon: function () {},
};
assert.equal(obj.concise.name, 'concise');
assert.equal(obj.arrow.name, 'arrow');
assert.equal(obj.named.name, 'named');
assert.equal(obj.anon.name, 'anon');

assert.equal(setFunctionName(obj.concise, 'brief'), obj.concise);
assert.equal(setFunctionName(obj.arrow, 'pointy'), obj.arrow);
assert.equal(setFunctionName(obj.named, ''), obj.named);
assert.equal(setFunctionName(obj.anon, 'anonymous'), obj.anon);

assert.equal(obj.concise.name, 'brief');
assert.equal(obj.arrow.name, 'pointy');
assert.equal(obj.named.name, '');
assert.equal(obj.anon.name, 'anonymous');

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

v2.0.2 - 2024-02-19

Commits

  • [meta] add types ae747cd
  • [Dev Deps] update aud, npmignore, object-inspect, tape 01aafcb
  • [Deps] update define-data-property, has-property-descriptors 0ef6338
  • [Refactor] use es-errors 0b23e87

v2.0.1 - 2023-09-13

Commits

  • [Fix] move functions-have-names to runtime deps db2eda8

v2.0.0 - 2023-09-12

Commits

  • [eslint] add npm run lint 23e1fcd
  • [actions] add reused GHA 525127e
  • [meta] add .gitignore aa3abdf
  • [Tests] switch tests to use tape; add posttest 8ad6d30
  • [readme] add readme 732c46c
  • [New] add optional loose argument f5e4771
  • [meta] relicense package to MIT; fix repo URLs 13948f8
  • [meta] add auto-changelog 7ab201c
  • [Breaking] throw if a non-function is provided cf6fc8f
  • [Breaking] drop UMD, just use CJS 47abfe8
  • [Refactor] use define-data-property and has-property-descriptors 9921c2b
  • [meta] use npmignore to autogenerate an npmignore file c5dbe4f
  • Only apps should have lockfiles 98bbfa1
  • [meta] add safe-publish-latest 8916cd8
  • [meta] add engines 2427c8e

v1.0.0 - 2017-09-14

Commits

  • Initial commit. Including tests. f26a1f2
  • add .npmignore to ignore test directory 9cac96f