impswitch

1.0.3 • Public • Published

ImpSwitch

npm version dependencies Build Status Pledge To Arupex!

Install

npm install impswitch --save

Switch Between Implementations

Assumptions:

    implementation functions should follow
        function(options, successCallback, failureCallback)

Example:

var ImpSwitch = require('impswitch');

var switch = ImpSwitch({
    real : {
        go: function go(options, success, failure) {
            success('Real Implementation');
        }
    },
    mock : {
        go : function go(options, success, failure) {
            success('Mock Implementation');
        }
    },
    {
        selector : function selector(options) {
            return options.id > 0 ? 'real' : 'mock';
        }
    }
});

Call

function success(data){
    console.log(data);
}

function failure(err){
    console.error(err);
}

switch('go')({ id : -1 }, success, failure)

switch('go')({ id : 1 }, success, failure)

Expected Output (ignoring any asynchronous operations):

Mock Implementation

Real Implementation

Alternative Calls! By Request

var superSwitch = switch('__SuperSwitch'); // '__SuperSwitch' create a switcher that doesnt require function name

superSwitch.go({ id : -1 }, success, failure)

superSwitch.go({ id : 1 }, success, failure)

Expected Output (ignoring any asynchronous operations):

Mock Implementation

Real Implementation

Or

var mock = switch('mock');
mock.go({ id : -1 });
mock.go({ id : 1 });

Expected Output:

Mock Implementation

Mock Implementation

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.3
    1
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.0.3
    1
  • 1.0.2
    0
  • 1.0.1
    0

Package Sidebar

Install

npm i impswitch

Weekly Downloads

1

Version

1.0.3

License

UNLICENSED

Last publish

Collaborators

  • dirwin517