Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit74dcfec

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 parentd4bc650 commit74dcfec

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+
varsnapshot={};
1820

1921
if(typeofarguments[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
}elseif(typeofarguments.varName==="string"&&arguments.length===2){
3134
if(!arguments.varName){
3235
thrownewTypeError("__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
thrownewTypeError("__set__ expects an environment object or a non-empty string as a variable name");
3741
}
3842

3943
eval(arguments.src);
44+
returnfunction(){
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(typeofundo).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+
varnewObj={hello:"hello"};
81+
82+
expect(moduleFake.getValue()).to.be(0);
83+
expect(moduleFake.getReference()).to.eql({});
84+
85+
varundo=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)

[8]ページ先頭

©2009-2025 Movatter.jp