Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/critocrito/namefn
Set the name property of a function.
https://github.com/critocrito/namefn
functional-programming functions javascript repl
Last synced: about 1 month 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 (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-11-07T22:11:58.000Z (about 5 years ago)
- Last Synced: 2024-09-12T21:06:27.559Z (2 months ago)
- Topics: functional-programming, functions, javascript, repl
- Language: JavaScript
- Size: 1.24 MB
- Stars: 0
- Watchers: 3
- 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
[![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) [![npm version](https://img.shields.io/npm/v/@critocrito/namefn.svg?style=flat)](https://www.npmjs.com/package/@critocrito/namefn) [![Build Status](https://travis-ci.org/critocrito/namefn.svg?branch=master)](https://travis-ci.org/critocrito/namefn) [![Coverage Status](https://coveralls.io/repos/github/critocrito/namefn/badge.svg)](https://coveralls.io/github/critocrito/namefn) [![Greenkeeper badge](https://badges.greenkeeper.io/critocrito/namefn.svg)](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)