@critocrito/namefn

0.1.5 • Public • Published

namefn

Set the name property of a function.

Synopsis

License: GPL v3 npm version Build Status Coverage Status

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

npm install @critocrito/namefn

Usage

import namefn from "@critocrito/namefn";

const f = namefn("identity", x => x);
console.log(f.name); // identity
f; // [Function: identity]

Examples

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.

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

Package Sidebar

Install

npm i @critocrito/namefn

Weekly Downloads

0

Version

0.1.5

License

GPL-3.0

Unpacked Size

41 kB

Total Files

8

Last publish

Collaborators

  • critocrito