Coder Social home page Coder Social logo

funcualizer's Introduction

Funcualizer

Small Javascript node library for converting methods into performant functions.

const funcualizer = require('funcualizer');

const slice = funcualizer(Array.prototype.slice);

slice([1, 2, 3], 2) === [3]
slice("abc", 0, 2) === ['a', 'b']

Functionizing a method allows it to be used and passed around as a true function, without having to .call it.

The library provides helpers to create functions that take this as either an explicit first or last parameter:

const post_slice = funcualizer.post(Array.prototype.slice);

post_slice(2, [1, 2, 3]) === [3]
post_slice(0, 2, "abc") === ['a', 'b']

The library allows you to choose between more generic behavior and a more performant implementation, with some of the resulting functions having overhead comparable to a handwritten method-to-function implementation.

Documentation

funcualizer(method)

funcualizer.pre(method)

Convert method into a function that takes this as a first argument. Forwards all other arguments to method.

const slice = funcualizer.pre(Array.prototype.slice);

slice([1, 2, 3], 2) === [3]
slice([1, 2, 3], 0, 2) === [1, 2]

This is usually the slowest implementation. Try using pre$ for better performance if perfect forwarding is not needed.

pre is also the top level export of the package:

funcualizer(Array.prototype.slice) === funcualizer.pre(Array.prototype.slice)

funcualizer.pre$(method [, arity])

Same as pre but for methods with a fixed number of arguments.

arity is the number of arguments that method expects. If arity is not provided, it is inferred from method.length.

// infers `arity === 2` from `Array.prototype.slice.length`.
const slice = funcualizer.pre$(Array.prototype.slice);

slice([1, 2, 3], 2) === [3]
slice([1, 2, 3], 0, 2) === [1, 2]

This is usually much faster than pre, but cannot forward arbitrary argument sets like pre can.

funcualizer.dynamic_pre(methodName)

Same general behavior as pre but looks up a method by name on the this argument. This means that the actual method implementation is not known until the function is invoked:

const toString = funcualizer.dynamic_pre('toString');

toString([1, 2, 3]) === "1,2,3"
toString({}) === "[object Object]"
toString({ toString: () => "bla" }) === 'bla'

funcualizer.dynamic_pre$(methodName, arity)

Fixed argument version of dynamic_pre.

Unlike pre$, since there is no method object to worth with here, the arity must be provided in order to gain any performance over regular dynamic_pre.


funcualizer.post(method)

Convert method into a function that takes this as a last argument. Forwards all other arguments to method.

const slice_post = funcualizer.post(Array.prototype.slice);

slice_post(2, [1, 2, 3]) === [3]
slice_post(0, 2, "abc") === ['a', 'b']

This is usually the slowest implementation. Try using post$ for better performance if perfect forwarding is not needed.

funcualizer.post$(method [, arity])

Same as post but for methods with a fixed number of arguments.

arity is the number of arguments that method expects.

// infers `arity === 2` from `Array.prototype.slice.length`.
const slice_post = funcualizer.post$(Array.prototype.slice);

slice_post(2, [1, 2, 3]) === [3]
slice_post(0, 2, "abc") === ['a', 'b']

Unlike pre$ however, the this argument is always at index arity, even if you invoke the function with less than the number of expected arguments.

This is usually much faster than post, but cannot forward arbitrary argument sets like post can.

funcualizer.dynamic_post(methodName)

Same general behavior as post but looks up a method by name on the this argument. See dynamic_pre.

funcualizer.dynamic_post$(methodName, arity)

Fixed argument version of dynamic_post.

funcualizer's People

Contributors

mattbierner avatar

Stargazers

Tommi Kaikkonen avatar

Watchers

 avatar James Cloos avatar  avatar

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.