For some reason I always have a negative feeling towards having many if statements in my JavaScript functions. But I've got some code that is processing an object that defines rules of an event. I need to determine one or many things to do based on the "rules" outlined in each object.
(Particularly four if statements deep within a for loop just gives me the heebie-jeebies for some reason.)
Is there a faster way to handle this type of rules-like processing? Clearly I could break this into some smaller functions to clean up the syntax, but I am mostly looking for feedback/suggestions around speed of processing the request.
The idea here is to capture customer conversion events throughout a user's interaction with a job application. Each conversion has an 'initiate' and a 'complete'. The "rules" which determine when to initiate and when to complete each conversion are setup in a JSON file which I then parse into a JavaScript object: applicationConversionEvents.
The JSON looks something like so:
[
{
"eventId": "view",
"eventName": "App View to Start",
"initiateOnStepNumber": 100,
"completeIfAuthenticated": true,
"completeOnElementClick": "first_name"
},
{
"eventId": "submit",
"eventName": "App Start to Submit",
"initiateOnEventComplete": "view",
"completeOnStepNumber": 450,
"completeOnEventInitiate": "decline"
},
{
"eventId": "decline",
"eventName": "App Decline",
"initiateOnStepNumber": 350,
"completeOnStepNumber": [
350,
375
]
}
]
I am then using the following function to parse the rules for each conversion event, and when appropriate fire off a new event either as an 'initiate' or a 'complete'. I also track what events fire in sessionStorage to avoid duplicates per application.
var _processApplicationConversionEvents = function(pageInfoArrayIndex, options) {
_log('Processing application conversion events.', LOG_LEVEL.INFO);
if(!Array.isArray(applicationConversionEvents)) return;
if(!_isValidPageInfoArrayIndex(pageInfoArrayIndex)) pageInfoArrayIndex = 0;
_log('Conversions for pageInfoArrayIndex: ' + pageInfoArrayIndex, applicationConversionEvents, LOG_LEVEL.DEBUG);
var pageInfo = _getPageInfo(pageInfoArrayIndex, options);
_log('pageInfo details for conversion events:', pageInfo, LOG_LEVEL.DEBUG);
for(var i = 0, len = applicationConversionEvents.length; i < len; i++) {
var conversionEvent = applicationConversionEvents[i];
if(typeof conversionEvent !== 'object') continue;
if(conversionEvent.initiateOnStepNumber) {
var appStepNumber = _asArray(conversionEvent.initiateOnStepNumber);
if(appStepNumber.indexOf(+pageInfo.appStepNumber) > -1) {
_newConversionEvent('initiate', conversionEvent, pageInfo);
if(_toLowerCaseString(conversionEvent.completeIfAuthenticated) === 'true'
&& pageInfo.authenticated) {
_newConversionEvent('complete', conversionEvent, pageInfo);
if(!conversionEvent.completeOnElementClick) continue;
_removeConversionOnClickListener(conversionEvent.completeOnElementClick);
}
}
}
if(conversionEvent.completeOnStepNumber) {
var appStepNumber = _asArray(conversionEvent.completeOnStepNumber);
if(appStepNumber.indexOf(+pageInfo.appStepNumber) > -1) {
_newConversionEvent('complete', conversionEvent, pageInfo);
continue;
}
}
if(conversionEvent.completeOnElementClick) {
_addConversionOnClickListener('complete', conversionEvent, pageInfo);
}
}
}