Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/twbs/rewire.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJohannes <mail@johannesewald.de>2012-06-15 17:48:01 +0400
committerJohannes <mail@johannesewald.de>2012-06-15 17:48:01 +0400
commit512548d888a8a34c3acf8b4c485579596812e506 (patch)
tree98cfacbdd7d85cb542f3912ea88ec26f6d89f6e2 /lib
parentd936fe1491409573a28cc3a981fc110f08bf6a4c (diff)
namespaced all variables within the __set__ method so you can really set all variables
Diffstat (limited to 'lib')
-rw-r--r--lib/__get__.js2
-rw-r--r--lib/__set__.js36
2 files changed, 20 insertions, 18 deletions
diff --git a/lib/__get__.js b/lib/__get__.js
index 64c467d..f6212d9 100644
--- a/lib/__get__.js
+++ b/lib/__get__.js
@@ -9,7 +9,7 @@
* @return {*}
*/
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");
}
diff --git a/lib/__set__.js b/lib/__set__.js
index 7de8eec..219b381 100644
--- a/lib/__set__.js
+++ b/lib/__set__.js
@@ -4,38 +4,40 @@
* This function will be stringified and then injected into every rewired module.
* Then you can set private variables by calling myModule.__set__("myPrivateVar", newValue);
*
+ * 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
* @param {String} varValue new value
* @throws {TypeError}
* @return {*}
*/
-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 {
throw new TypeError("__set__ expects an environment object or a non-empty string as a variable name");
}
- eval(src);
+ eval(arguments.src);
}; \ No newline at end of file