rewire
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -12,3 +12,3 @@ "use strict"; // run code in ES5 strict mode | ||
module.exports = function __get__(name) { | ||
if (typeof name !== "string" || name == false) { | ||
if (typeof name !== "string" || name.length === 0) { | ||
throw new TypeError("__get__ expects a non-empty string"); | ||
@@ -15,0 +15,0 @@ } |
@@ -7,2 +7,5 @@ "use strict"; // run code in ES5 strict mode | ||
* | ||
* All variables within this function are namespaced in the arguments array because every | ||
* var declaration could possibly clash with a variable in the module scope. | ||
* | ||
* @param {!String|!Object} varName name of the variable to set | ||
@@ -13,26 +16,25 @@ * @param {String} varValue new value | ||
*/ | ||
module.exports = function __set__(varName, varValue) { | ||
var key, | ||
env, | ||
src = ""; | ||
function checkExistsSrc(varName) { | ||
module.exports = function __set__() { | ||
arguments.varName = arguments[0]; | ||
arguments.varValue = arguments[1]; | ||
arguments.src = ""; | ||
arguments.checkExistsSrc = function (varName) { | ||
return "if (typeof " + varName + " === 'undefined') { throw new ReferenceError('" + varName + " is not defined');} "; | ||
} | ||
}; | ||
if (typeof varName === "object") { | ||
env = varName; | ||
if (!env || Array.isArray(env)) { | ||
if (typeof arguments[0] === "object") { | ||
arguments.env = arguments.varName; | ||
if (!arguments.env || Array.isArray(arguments.env)) { | ||
throw new TypeError("__set__ expects an object as env"); | ||
} | ||
for (key in env) { | ||
if (env.hasOwnProperty(key)) { | ||
src += checkExistsSrc(key) + key + " = env." + key + ";"; | ||
for (arguments.key in arguments.env) { | ||
if (arguments.env.hasOwnProperty(arguments.key)) { | ||
arguments.src += arguments.checkExistsSrc(arguments.key) + arguments.key + " = arguments.env." + arguments.key + ";"; | ||
} | ||
} | ||
} else if (typeof varName === "string") { | ||
if (!varName) { | ||
} else if (typeof arguments.varName === "string") { | ||
if (!arguments.varName) { | ||
throw new TypeError("__set__ expects a non-empty string as a variable name"); | ||
} | ||
src = checkExistsSrc(varName) + varName + " = varValue;" | ||
arguments.src = arguments.checkExistsSrc(arguments.varName) + arguments.varName + " = arguments.varValue;"; | ||
} else { | ||
@@ -42,3 +44,3 @@ throw new TypeError("__set__ expects an environment object or a non-empty string as a variable name"); | ||
eval(src); | ||
eval(arguments.src); | ||
}; |
{ | ||
"name" : "rewire", | ||
"version" : "0.2.0", | ||
"version" : "0.2.1", | ||
"description" : "Dependency injection for node.js applications", | ||
@@ -30,3 +30,3 @@ "keywords" : [ | ||
"engines" : { | ||
"node" : "0.6.x" | ||
"node" : "0.8.x" | ||
}, | ||
@@ -33,0 +33,0 @@ "devDependencies": { |
@@ -5,4 +5,3 @@ rewire | ||
rewire adds a special setter and getter that allow you to modify the behaviour of modules | ||
for better unit testing. You may | ||
rewire adds a special setter and getter to modules so you can modify their behaviour for better unit testing. You may | ||
@@ -17,2 +16,4 @@ - introduce mocks for other modules | ||
[![Build Status](https://secure.travis-ci.org/jhnns/rewire.png?branch=master)](http://travis-ci.org/jhnns/rewire) | ||
----------------------------------------------------------------- | ||
@@ -27,3 +28,3 @@ <br /> | ||
**For older node versions:**<br /> | ||
rewire is tested with node 0.6.x. I recommend to run the unit tests via `mocha` in the rewire-folder before using rewire with older node versions. | ||
rewire is tested with node 0.7.x. I recommend to run the unit tests via `mocha` in the rewire-folder before using rewire with older node versions. | ||
@@ -30,0 +31,0 @@ ----------------------------------------------------------------- |
@@ -22,3 +22,10 @@ "use strict"; // run code in ES5 strict mode | ||
myNumber: 0, // copy by value | ||
myObj: {} // copy by reference | ||
myObj: {}, // copy by reference | ||
// these variables are used within the set method | ||
// because there is a eval() statement within the set method | ||
// these variables should not override same-named vars of the module | ||
key: "key", | ||
env: "env", | ||
src: "src" | ||
}; | ||
@@ -71,2 +78,5 @@ | ||
}); | ||
it("should not clash with vars used within the set method", function () { | ||
}); | ||
it("should throw a TypeError when passing misfitting params", function () { | ||
@@ -73,0 +83,0 @@ expect(function () { |
@@ -66,2 +66,3 @@ "use strict"; // run code in ES5 strict mode | ||
expect(rewiredModuleA.getMyObj()).to.be(newObj); | ||
rewiredModuleA.__set__("env", "ENVENV"); | ||
}); | ||
@@ -68,0 +69,0 @@ it("should provide the ability to get private vars", function () { |
@@ -6,2 +6,3 @@ "use strict"; // run code in ES5 strict mode | ||
myObj = {}, // copy by reference | ||
env = "bla", | ||
fs = require("fs"); | ||
@@ -8,0 +9,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
74783
33
641
180
0