Skip to content

Commit

Permalink
add some comment for hack and test is changed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomohito Nakayama committed Feb 21, 2016
1 parent 91dc233 commit abcc127
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 18 deletions.
55 changes: 44 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,11 @@ function pass( filePath, userProcess, jb, chainedProcess){

function filter( filePath, userProcess, jb, chainedProcess){
process(filePath,
function(obj,filePath){
if( userProcess( obj, filePath) ) return true;
else return false;
},
filternize(userProcess),
jb,
chainedProcess,
filterPostProcess,
raiseUnknownError //pass dows not create new file when not existed
raiseUnknownError //pass does not create new file when not existed
);
}

Expand Down Expand Up @@ -339,22 +336,58 @@ function process(
);
}

function filternize( userProcess ){
// return true/false according to result of user process.
let result =
wrapUserProcess(
userProcess,
function( process ){
return function(obj,filePath){
if( process ( obj, filePath) ) return true;
else return false;
};
}
);
return result;

//apply process function to json.
function apply( process, json, file, closeFile, jb, filePath, postProcess, chainedProcess){
}

function wrapUserProcess( userProcess, functionWrapper ){

//To keep _plannedExecuter hack working, this function is needed to use when wrapping userProcess.
let wrapped = functionWrapper( userProcess );
wrapped._plannedExecuter = userProcess._plannedExecuter;//hack to emit error from userProcess by executer.

return wrapped;

}

function guardProcess(userProcess){

let guardedUserProcess =
let guarded =
function( json, filePath){
try{
return process( json,filePath);
return userProcess( json,filePath);
}catch(err){
raiseError( process._plannedExecuter, 'User process error', err);
//walkaround for unclear this/caller problem in javascript.
//I just wanted to use something like "this".
raiseError( userProcess._plannedExecuter, 'User process error', err);

}
};

return guarded;

}

//apply process function to json.
function apply( process, json, file, closeFile, jb, filePath, postProcess, chainedProcess){

let guardedProcess = guardProcess(process);

if( chainedProcess == undefined ) chainedProcess = function(p1,p2){};

var result = guardedUserProcess(json,filePath);
var result = guardedProcess(json,filePath);

if(result != undefined && result != null){
//if result returned, executePostProcess
Expand Down
16 changes: 9 additions & 7 deletions test/unit/testError.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,30 @@ describe('Error ', function () {
jf.filed( testPath )
.io(
function( obj, filePath) {
throw {msg:"intended error"};
//throw {msg:"intended error"};
return testValue;
},
function(err){
console.error(err);
//console.error(err);
done();
}
).filter(
function(obj, filePath){
if (filePath == pathToFind)
return true;
else if(filePath == pathToNotFind)
return false;
throw {msg:"intended error"};

return true;
},
function(err){
done();
console.error(err);
}
).pass(
function(obj, filePath){
expect(filePath).to.eql(pathToFind);
//throw {msg:"intended error"};
expect(filePath).to.eql(testPath);
},
function(err){
done();
console.error(err);
}
).exec();
Expand Down

0 comments on commit abcc127

Please sign in to comment.