https://github.com/critocrito/namefn
Set the name property of a function.
https://github.com/critocrito/namefn
functional-programming functions javascript repl
Last synced: 7 months ago
JSON representation
Set the name property of a function.
- Host: GitHub
- URL: https://github.com/critocrito/namefn
- Owner: critocrito
- License: gpl-3.0
- Created: 2018-05-08T21:52:07.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-11-07T22:11:58.000Z (over 6 years ago)
- Last Synced: 2025-08-04T05:18:21.868Z (8 months ago)
- Topics: functional-programming, functions, javascript, repl
- Language: JavaScript
- Size: 1.24 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# `namefn`
Set the name property of a function.
## Synopsis
[](https://www.gnu.org/licenses/gpl-3.0) [](https://www.npmjs.com/package/@critocrito/namefn) [](https://travis-ci.org/critocrito/namefn) [](https://coveralls.io/github/critocrito/namefn) [](https://greenkeeper.io/)
When programmatically generating functions in JavaScript, they often lack a
name. This reduces the usability of using those functions in a REPL. `namefn`
is a little helper to set the names of functions at construction time.
## Installation
```sh
npm install @critocrito/namefn
```
## Usage
```js
import namefn from "@critocrito/namefn";
const f = namefn("identity", x => x);
console.log(f.name); // identity
f; // [Function: identity]
```
## Examples
```js
import namefn from "@critocrito/namefn";
const f = x => () => x;
const g = f(23);
console.log(g.name); // ""
g; // ""
const constant23 = namefn("constant23", f(23));
console.log(constant23.name); // "constant23"
constant23; // "[Function: constant23]"
```
The following example implements a `curry` function, and includes the number
of missing arguments in the function name.
```js
import namefn from "@critocrito/namefn";
const curry = n => {
const localCurry = (name, f, ...args) => {
const g = (...largs) => {
const rest = args.concat(largs);
if (rest.length < n) return localCurry(name, f, ...rest);
return f(...rest);
};
return namefn(`${name}-${n - args.length}`, g);
};
return namefn(`curry${n}`, localCurry);
};
const curry2 = curry(2);
const map = curry2("map", (f, xs) => xs.map(f));
map; // [Function: map-2]
const addOne = map(i => i + 1);
addOne; // [Function: map-1]
```
## License
[GPL 3.0 licensed](LICENSE)