/unthis

Given a method, return a function that accept `this` as last argument

Primary LanguageJavaScriptMIT LicenseMIT

unthis

Travis Build Status NPM downloads

Given a method, return a function that accept this as last argument

The ideal complement for functional javascript, you get function that use same semntic as ramda library.

Usage

This example uses unthis to transform the match method of String prototype.

Original match method has one argument, the RegExp to match with. this context for the method must be a String to match against.

unthis return a function with two arguments: it pass the first to original method RE argument, and uses the second argument as this context.

import unthis from 'unthis';

const match = unthis(String.prototype.match);
const matchFinalS = match(/s$/);

console.log(matchFinalS("words") !== null); // --> true
console.log(matchFinalS("lollo") !== null); // --> false
console.log(match(/^w/, "words") !== null); // --> true

API

unthis

Given an object method, unthis return a function that accept all original arguments plus one. It forward all arguments to the method, and uses the last argument as this context for it.

Parameters

  • method Function The original method to transform

Returns Function The transformed function

unthisAll

Given an object, unthisAll apply unthis to all its methods, and return an object containing all resulting functions.

By default, all object methods are extracted. Otherwise, you can also specify which method to include or exclude.

Parameters

  • object Object The object containing methods to transform.
  • $1 Object (optional, default {})
    • $1.exclude (optional, default [])
    • $1.include (optional, default null)
  • exclude Array An array containing name of methods to transform.
  • include Array An array containing name of methods to exclude from transform.

Returns Object An object with all transformed methods.

Install

With npm installed, run

npm install --save unthis

See Also

License

MIT