diff options
author | Bob Pace <bob.pace@gmail.com> | 2014-07-01 01:12:07 +0400 |
---|---|---|
committer | Bob Pace <bob.pace@gmail.com> | 2014-07-01 01:12:07 +0400 |
commit | 74dcfece99ff75e36a4ea544885f7aab888a82cf (patch) | |
tree | fd22f0de9c10dd85fa5e8d6ceffe83ad5faf3606 /lib | |
parent | d4bc6505c0c116074bc89697b553decfea54dec5 (diff) |
__set__ returns an 'undo' function that when invoked will restore the module to the values it had before it was called
Diffstat (limited to 'lib')
-rw-r--r-- | lib/__set__.js | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/lib/__set__.js b/lib/__set__.js index 66e8c0a..0fa88ac 100644 --- a/lib/__set__.js +++ b/lib/__set__.js @@ -11,10 +11,12 @@ * @throws {ReferenceError} When the variable is unknown * @return {*} */ + function __set__() { arguments.varName = arguments[0]; arguments.varValue = arguments[1]; arguments.src = ""; + var snapshot = {}; if (typeof arguments[0] === "object" && arguments.length === 1) { arguments.env = arguments.varName; @@ -25,6 +27,7 @@ function __set__() { if (arguments.env.hasOwnProperty(arguments.varName)) { arguments.varValue = arguments.env[arguments.varName]; arguments.src += arguments.varName + " = arguments.env." + arguments.varName + "; "; + snapshot[arguments.varName] = eval(arguments.varName); } } } else if (typeof arguments.varName === "string" && arguments.length === 2) { @@ -32,11 +35,15 @@ function __set__() { throw new TypeError("__set__ expects a non-empty string as a variable name"); } arguments.src = arguments.varName + " = arguments.varValue;"; + snapshot[arguments.varName] = eval(arguments.varName); } else { throw new TypeError("__set__ expects an environment object or a non-empty string as a variable name"); } eval(arguments.src); + return function() { + __set__(snapshot); + }; } -module.exports = __set__;
\ No newline at end of file +module.exports = __set__; |