Create a gist now

Instantly share code, notes, and snippets.

@jiggzson /Stepper.js
Last active May 14, 2017

What would you like to do?
Log your steps with nerdamer
var stepper = (function() {
"use strict";
var core = nerdamer.getCore(),
_ = core.PARSER,
//the container for the function steps
stepper = {},
//nerdamer makes recursive calls when adding, subtracting, etc. Making
//sure the stack is clear ensures that we're at the first call
stack = [];
//a function to add calls to the stack
var wrapper = function(f, a, b) {
stack.push('lock');
var r = f(a, b);
stack.pop();
return r;
};
//This logging function makes sure that there aren't any items on the stack
//before logging
var logger = function() {
if(stack.length === 0 && typeof this === 'function')
this.apply(undefined, arguments);
};
//the semi-globals
var add, subtract, divide, multiply, pow, fcall;
var load = function() {
//ADD
add = _.add;
var step_add = function(a, b) {
var result;
logger.call(stepper.pre_add, a, b);
var wrapper_result = wrapper(function(a, b){
result = add.call(_, a.clone(), b.clone());
return result;
}, a, b);
logger.call(stepper.post_add, result, a, b);
return wrapper_result;
};
_.add = step_add;
//SUBTRACT
subtract = _.subtract;
var step_subtract = function(a, b) {
var result;
logger.call(stepper.pre_subtract, a, b);
var wrapper_result = wrapper(function(a, b){
result = subtract.call(_, a.clone(), b.clone());
return result;
}, a, b);
logger.call(stepper.post_subtract, result, a, b);
return wrapper_result;
};
_.subtract = step_subtract;
//DIVIDE
divide = _.divide;
var step_divide = function(a, b) {
var result;
logger.call(stepper.pre_divide, a, b);
var wrapper_result = wrapper(function(a, b){
result = divide.call(_, a.clone(), b.clone());
return result;
}, a, b);
logger.call(stepper.post_divide, result, a, b);
return wrapper_result;
};
_.divide = step_divide;
//MULTIPLY
multiply = _.multiply;
var step_multiply = function(a, b) {
var result;
logger.call(stepper.pre_multiply, a, b);
var wrapper_result = wrapper(function(a, b){
result = multiply.call(_, a.clone(), b.clone());
return result;
}, a, b);
logger.call(stepper.post_multiply, result, a, b);
return wrapper_result;
};
_.multiply = step_multiply;
//POW
pow = _.pow;
var step_pow = function(a, b) {
var result;
logger.call(stepper.pre_pow, a, b);
var wrapper_result = wrapper(function(a, b){
result = pow.call(_, a.clone(), b.clone());
return result;
}, a, b);
logger.call(stepper.post_pow, result, a, b);
return wrapper_result;
};
_.pow = step_pow;
//CALLFUNCTION
//function calls are not recursive and can have more than one call on the stack
//because of this we don't use the wrapper
fcall = _.callfunction;
var step_fcall = function(fname, args) {
if(stepper.pre_function_call && typeof stepper.pre_function_call === 'function')
stepper.pre_function_call.call(undefined, fname, args);
var f = fcall.call(_, fname, args);
if(stepper.post_function_call && typeof stepper.post_function_call === 'function')
stepper.post_function_call.call(undefined, f, fname, args);
return f;
};
_.callfunction = step_fcall;
};
load(); //fire away
var xport = function(o, override) {
for(var x in o) {
if(!stepper[x] || stepper[x] && override)
stepper[x] = o[x];
}
};
xport.unload = function() {
_.add = add;
_.subtract = subtract;
_.multiply = multiply;
_.divide = divide;
_.pow = pow;
_.callfunction = fcall;
};
xport.clear = function() {
stepper = {};
};
xport.load = load;
return xport;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment