Git Product home page Git Product logo

demethodize's Introduction

demethodize Build Status

npm install demethodize

Demethodizing allows you to use methods as generics. You can just use call and apply to do this, like so:

Array.prototype.map.call('abcdef', function (letter) {
  return letter.toUpperCase();
}); // ['A', 'B', 'C', 'D', 'E', 'F']

This is kind of verbose if you're using it a lot. Array.prototype.map can be simply substituted with [].map. This gets even easier still with demethodize, which produces reusable functions.

var map = demethodize([].map);
map('abcdef', function (letter) {
  return letter.toUpperCase();
}); // ['A', 'B', 'C', 'D', 'E', 'F']

map('123456', function (n) {
  return Number(n) + 1;
}); // [2, 3, 4, 5, 6, 7]

There's an added method, demethodize.functional. This takes all the arguments that would be supplied to the method, then returns another function. Pass the context into that function. See below for an example. This is great for partial application and functional programming in general.

var slice = demethodize.functional([].slice);

// pass args here
var dropFirstAndLastTwo = slice(2, -2);

// then pass the context (an array) here
dropFirstAndLastTwo('abcdefgh'); // ['c', 'd', 'e', 'f']
dropFirstAndLastTwo('ijklm'); // ['k']

For brevity, the functional version is aliased.

var fdm = require('demethodize').f;
var hiToBye = fdm("".replace)(/hi/i, "Bye");
var greetings = ["Hi Pat", "Hi Max", "Hi Ali"];
greetings.map(hiToBye) // => ["Bye Pat", "Bye Max", "Bye Ali"]

I've benchmarked a number of equivalent implementations for speed, and am using an implementation with .apply() as it is fastest.

Example benchmark output (see bench.js for implementations)

Hang tight, running benchmarks...
........
apply 2 is fastest.
apply 2 x 1,373,646 ops/sec ±1.48% (91 runs sampled)
apply 1 is 3.98% slower than fastest.
apply 1 x 1,318,984 ops/sec ±2.58% (89 runs sampled)
call spread is 16.82% slower than fastest.
call spread x 1,142,603 ops/sec ±5.11% (75 runs sampled)
call bind 2 is 18.85% slower than fastest.
call bind 2 x 1,114,747 ops/sec ±1.47% (89 runs sampled)
call bind 1 is 20.31% slower than fastest.
call bind 1 x 1,094,665 ops/sec ±1.92% (91 runs sampled)
double bind is 21.23% slower than fastest.
double bind x 1,082,039 ops/sec ±2.07% (89 runs sampled)
call bind 3 is 23.46% slower than fastest.
call bind 3 x 1,051,395 ops/sec ±1.76% (89 runs sampled)
apply 3 is 54.54% slower than fastest.
apply 3 x 624,405 ops/sec ±4.53% (84 runs sampled)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.