npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

shifting

v1.2.6

Published

Create APIs that supports callback-style and Promises.

Downloads

2,559

Readme

Shifting

Shifting allows you to create APIs that support both callback-style and promises.

Build Status Coverage Status Known Vulnerabilities dependencies Status Downloads


Install

npm install --save shifting

Implementation

Wrapping a callback

function timesTwo(number, callback) {
  return shifting(callback).from(
    function (cb) {
      cb(null, number * 2);
    }
  )
}

timesTwo(4).then(console.log); // logs 8
timesTwo(4, function (err, result) {
  console.log(result); // logs 8
});

Wrapping a Promise

function timesTwo(number, callback) {
  return shifting(callback).from(
    Promise.resolve(number * 2)
  )
}

timesTwo(4).then(console.log); // logs 8
timesTwo(4, function (err, result) {
  console.log(result); // logs 8
});

Wrapping a value

function timesTwo(number, callback) {
  return shifting(callback).from(
    function () (
      if (typeof number !== 'number') {
        throw new TypeError('number is not a number');
      }
      return number;
    }
  )
}

timesTwo(4).then(console.log); // logs 8
timesTwo('a string', function (err) {
  console.log(err); // logs [Error: number is not a number]
});

The important part here is that throwing an Error will still callback or reject the Error.

Call / Apply

Shifting allows you to call a function of which you are not sure if it will take a callback arg, or return a Promise, or return synchronously any other value.

// Callback API
function sum(augend, addend, callback) {
  callback(null, augend + addend);
}

// Promise API
function sum(augend, addend) {
  return Promise.resolve(augend + addend);
}

// Synchronous API
function sum(augend, addend) {
  return augend + addend;
}

With any of the above implementations you can use shifting.apply / shifting.call as such.

shifting.apply(sum, [3, 4]).then(console.log); // logs 7
shifting.apply(sum, [3, 4], function (err, result) {
  console.log(result); // logs 7
});

shifting.call(sum, 3, 4).then(console.log); // logs 7
shifting.call(sum, 3, 4, function (err, result) {
  console.log(result); // logs 7
});

Binding

If the called function requires binding you can do it as such.

var myAPI = {
  hello: 'Bonjour',
  sayHello: function (name, callback) {
    callback(null, this.hello + ' ' + name + '!');
  }
}

say.call([myAPI, myAPI.sayHello], 'World')
  .then(console.log); // logs 'Bonjour World!'

API

shifting(callback:function|null|undefined) ▶︎ return object { from:function }

This function does nothing except returning a { from:function } object.

from(source:function|Promise) ▶︎ returns Promise|undefined

Pass a source to Shifting. If shifting() (see above) received a callback function it will return undefined, otherwise a Promise.

shifting.apply(function|array<context, function>[, array<args...>[, callback]]) ▶︎ return Promise|undefined

shifting.call(function|array<context, function>[, args...[, callback]]) ▶︎ return Promise|undefined

Troubleshooting

shifting(callback) : callback is not a function, nor null, nor undefined

You need to call shifting() with either a callback function or undefined|null. Otherwise this error will be logged. This is to loudly expose programming errors.

from(source) : source is not a function, nor a Promise

See implementation examples above.

any-promise

Shifting uses any promise to detect a Promise object. Check their README to plugin in your own Promise library.