Comparing version 0.1.1 to 0.1.2
/** | ||
* Fix the number of arguments returned in a callback to N. | ||
* Takes a function (usually callbacks), and fix the number of arguments that it returns on execution to N arguments. | ||
* If the original function had return more parameters, they are ignored and just firsts N arguments are returned. | ||
* If the original function had return less parameters, all of them are returned along with null values until complete N arguments. | ||
* | ||
* @param callback | ||
* @param N | ||
* @returns {Function} | ||
* @constructor | ||
*/ | ||
var Fixa = function Fixa(callback, N) { | ||
return function executeCallback() { | ||
var newArguments = []; | ||
for (var i = 0; i < N; i++) { | ||
newArguments.push(arguments[i] || null); | ||
} | ||
// Send limited arguments | ||
callback.apply(this, newArguments); | ||
} | ||
}; | ||
/** | ||
* Fix the number of arguments returned in a callback to N. | ||
* | ||
* @param N The number of arguments to return in callback | ||
@@ -17,11 +36,5 @@ * @returns {*} | ||
// Rewrite callback | ||
originalArgs[originalArgs.length - 1] = function handleCallback() { | ||
var newArguments = []; | ||
for (var i = 0; i < N; i++) { | ||
newArguments.push(arguments[i] || null); | ||
} | ||
// Send limited arguments | ||
callback.apply(originalFunction, newArguments); | ||
}; | ||
originalFunction.apply(this, originalArgs) | ||
originalArgs[originalArgs.length - 1] = Fixa(callback, N); | ||
originalFunction.apply(this, originalArgs); | ||
} else { | ||
@@ -86,2 +99,4 @@ // No callback sent, return results | ||
return this.fixa(4).apply(this, arguments); | ||
}; | ||
}; | ||
module.exports = Fixa; |
{ | ||
"name": "fixa", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Fixa (from Fix Arguments) is a Javascript Library to fix the number of arguments returned on callbacks.", | ||
@@ -5,0 +5,0 @@ "main": "lib/fixa.js", |
@@ -28,5 +28,13 @@ # Fixa | ||
**Fixa(callback, N)** | ||
Fix to N the number of arguments executed in the callback. | ||
N must be 0 or a positive integer. | ||
``` | ||
Fixa(callback, N); | ||
``` | ||
**fixa(N)** | ||
Fix to N the number of arguments sent to the callback. | ||
Fix to N the number of arguments executed in the callback. | ||
N must be 0 or a positive integer. | ||
@@ -89,3 +97,3 @@ ``` | ||
``` | ||
require("fixa"); | ||
var Fixa = require("fixa"); | ||
@@ -103,2 +111,3 @@ // Sample functions | ||
// Limiting arguments. | ||
@@ -125,3 +134,3 @@ // Execute sample function normally. | ||
// Adding arguments | ||
// Fix to 2 arguments. | ||
// Execute sample function normally. | ||
sampleFunction2Arguments(3, 4, function(one, two, three, four, five) { | ||
@@ -143,2 +152,11 @@ console.log(one, two, three, four, five); | ||
}); | ||
// Use the library. Useful when it can't be used from functions prototype. | ||
var callback = function(one, two, three, four, five) { | ||
console.log(one, two, three, four, five); | ||
// Prints: 1 2 undefined undefined undefined | ||
} | ||
sampleFunction5Arguments(Fixa(callback, 2)); | ||
``` | ||
@@ -145,0 +163,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33312
170
182