Skip to content

Commit

Permalink
feat: create a runSafe method on the util module that executes middle…
Browse files Browse the repository at this point in the history
…wares and controllers safely, s
  • Loading branch information
teclone committed Jul 18, 2018
1 parent d6f743a commit 17293c6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/modules/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,29 @@ export default {
};
},

/**
* runs the executable and supresses all runtime errors
*@param {Function} executable - function to execute
*@param {Scope} [scope] - runtime scope object - defaults to the host object
*@param {Array} [parameters=null] - array of parameters to pass in to executable
*@param {number} [runAfter=0] - least number of time in milliseconds to wait before
* starting the execution
*@throws {TypeError} if argument one is not a function
*@returns {mixed} this will return a promise if runAfter parameter is given else it will
* return the execution control
*/
runSafe(executable, scope, parameters, runAfter) {
let callback = this.generateCallback(executable, scope, parameters);
if (runAfter) {
return new Promise(function(resolve) {
setTimeout(() => {
resolve(callback()); // pass in the return value to the resolve method
}, runAfter);
});
}
return callback();
},

/**
* converts the letters into camel like cases
*@param {string} value - the string word to convert
Expand Down
45 changes: 45 additions & 0 deletions test/modules/Util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,51 @@ describe('Util module', function() {
});
});

describe('.runSafe(executable, scope?, parameters?, runAfter?)', function() {
it('should run the callback function safely by surpressing any runtime error', function() {
expect(function() {
Util.runSafe(function() {
throw new Error('this error should be surpressed');
});
}).to.not.throw();
});

it('should throw error if argument one is not a function', function() {
expect(function() {
Util.runSafe(null);
}).to.throw('argument one is not a function');
});

it('should accept an optional execution scope object as a second argument', function() {
let scope = {
name: 'ForensicJS'
},
result = Util.runSafe(function() {
return this.name;
}, scope);
expect(result).to.equals('ForensicJS');
});

it('should accept an optional parameter or array of parameters to pass in to executable during execution as a third argument', function() {
let parameters = ['1.0.0', 'ForensicJS'],
result = Util.runSafe(function(version, name) {
return {version, name};
}, null, parameters);
expect(result.version).to.equals('1.0.0');
});

it('should accept an optional wait before execution parameter in milliseconds as a fourth parameter', function() {
let startTime = Date.now();

return Util.runSafe(() => Date.now() - startTime, null, null, 1000)
.then(result => expect(result).to.be.at.least(999));
});

it('should return a promise if given a wait parameter as fourth argument', function() {
expect(Util.runSafe(function() {}, null, null, 1000)).to.be.a('promise');
});
});

describe('.camelCase(value, delimiter?)', function() {

it('should apply camel like casing on the argument and return the result. default delimiter used is dash or underscore characters', function() {
Expand Down

0 comments on commit 17293c6

Please sign in to comment.