1.0.1 • Published 6 years ago

inverse-params v1.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

inverse-params

NPM Package

Inspiration

Incidentally I think why don't we write a function to help execute a function with inverse params. It will help if we convert a callback-style function to Promise because Promise callback receives result as first params backwards callback use first param to get error.

Ex:

var p = new Promise((resolve, reject) => {
  something.async((err, result) => {
    if (err) {
      return reject(err);
    } else {
      return resolve(result);
    }
  })
})

But now it shorter:

var p = new Promise(callback => something.async(inverse(callback)));

Installation:

npm install inverse-params

How it work ?

Execute a function with it reversed params. This works with function has context (this).

Example:

// Normal function
function concatWord () {
    console.log(this.foo);
    const words = Array.prototype.slice.call(arguments);
    return words.reduce((acc, curr) => acc + curr, '');
}
    
concatWord('one', 'two', 'three'); // return 'onetwothree'

const concatWordV2 = inverse(concatWord)
concatWordV2('one', 'two', 'three'); // return 'threetwoone';

Enhance

Feel free to open a pull request. You're welcome :)