decontextify - nodef/extra-function GitHub Wiki

Decontextify a function by accepting this-object as the first argument.

Similar: contextify, decontextify.

function decontextify(x)
// x: a function
const xfunction = require('extra-function');


function count(value) {
  var a = 0;
  for (var v of this)
    if (v===value) ++a;
  return a;
}

var fn = xfunction.decontextify(count);

fn([6, 3, 4, 3], 3);
// → 2

fn([6, 1, 4, 3], 3);
// → 1