Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 74dcfec

Browse files
committed
__set__ returns an 'undo' function that when invoked will restore the module to the values it had before it was called
1 parent d4bc650 commit 74dcfec

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

lib/__set__.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
* @throws {ReferenceError} When the variable is unknown
1212
* @return {*}
1313
*/
14+
1415
function __set__() {
1516
arguments.varName = arguments[0];
1617
arguments.varValue = arguments[1];
1718
arguments.src = "";
19+
var snapshot = {};
1820

1921
if (typeof arguments[0] === "object" && arguments.length === 1) {
2022
arguments.env = arguments.varName;
@@ -25,18 +27,23 @@ function __set__() {
2527
if (arguments.env.hasOwnProperty(arguments.varName)) {
2628
arguments.varValue = arguments.env[arguments.varName];
2729
arguments.src += arguments.varName + " = arguments.env." + arguments.varName + "; ";
30+
snapshot[arguments.varName] = eval(arguments.varName);
2831
}
2932
}
3033
} else if (typeof arguments.varName === "string" && arguments.length === 2) {
3134
if (!arguments.varName) {
3235
throw new TypeError("__set__ expects a non-empty string as a variable name");
3336
}
3437
arguments.src = arguments.varName + " = arguments.varValue;";
38+
snapshot[arguments.varName] = eval(arguments.varName);
3539
} else {
3640
throw new TypeError("__set__ expects an environment object or a non-empty string as a variable name");
3741
}
3842

3943
eval(arguments.src);
44+
return function() {
45+
__set__(snapshot);
46+
};
4047
}
4148

42-
module.exports = __set__;
49+
module.exports = __set__;

test/__set__.test.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,31 @@ describe("__set__", function () {
6969
expect(moduleFake.getValue()).to.be(2);
7070
expect(moduleFake.getReference()).to.be(newObj);
7171
});
72-
it("should return undefined", function () {
73-
expect(moduleFake.__set__("myValue", 4)).to.be(undefined);
72+
it("should return a function that when invoked reverts to the values before set was called", function () {
73+
undo = moduleFake.__set__("myValue", 4)
74+
expect(typeof undo).to.be("function");
75+
expect(moduleFake.getValue()).to.be(4);
76+
undo()
77+
expect(moduleFake.getValue()).to.be(0);
78+
});
79+
it("should be able to revert when calling with an env-obj", function () {
80+
var newObj = { hello: "hello" };
81+
82+
expect(moduleFake.getValue()).to.be(0);
83+
expect(moduleFake.getReference()).to.eql({});
84+
85+
var undo = moduleFake.__set__({
86+
myValue: 2,
87+
myReference: newObj
88+
});
89+
90+
expect(moduleFake.getValue()).to.be(2);
91+
expect(moduleFake.getReference()).to.be(newObj);
92+
93+
undo();
94+
95+
expect(moduleFake.getValue()).to.be(0);
96+
expect(moduleFake.getReference()).to.eql({});
7497
});
7598
it("should throw a TypeError when passing misfitting params", function () {
7699
expect(function () {
@@ -101,4 +124,4 @@ describe("__set__", function () {
101124
moduleFake.__set__("someVar"); // misfitting number of params
102125
}).to.throwException(expectTypeError);
103126
});
104-
});
127+
});

0 commit comments

Comments
 (0)