0
\$\begingroup\$

I am attempting to validate a set of business rules in order for a user to successfully add to cart. The rules vary from requiting to add/decline a requested add-on to not having more then 60 items in your shopping cart.

For my end goal, I need to be able to have several dozen rule sets and if any return false then execute the needed function and prevent cart submit.

Separation of the rules logic seemed like a great thing so that I do not clutter my plugin or ajax call it's self, but I wonder if there is a better way because I tend to over engineer things.

JS Fiddle example

Here is the basic object:

var req = {
    type: function(name){
        switch(typeof name){
            case "function":
                return "function";
            break;
            case "object":
                if($.isArray(name)){
                    return "array";
                }else{
                    return "object";
                };
            break;
            case "string":
                return "string";
            break;
            case "number":
                return "number";
            break;
            case '':
            case "undefined":
            default:
                return "undefined";
            break;
        };
    },
    check: function(v){
        var valid = new Array();

        if(this.type(v) === "array"){
            if(v.length > 0){
                for(var i = 0; i < v.length; i++){
                    if(this.type(v[i]) === "function"){
                        valid.push(v[i]());
                    };
                };
            }else{
                return true;
            };
        }else{
            if(this.type(v) === "function"){
                valid.push(v());
            };
        };

        if(valid.indexOf(false) > -1){
            return false;
        }else{
            return true;
        };
    }
};

Here is how the basic functions are pushed into the array:

var addHelper = {
    invoke: function(type, fun){
        gb[type].push(fun);
    }
};

addHelper.invoke("validate",
    function(){
        if(parseInt($("#fakeQty").val()) > 59){
            var qtyMsg = "For bulk or commercial orders, please call our online support";
            alert(qtyMsg);
            return false;
        }else{
            return true;
        };
    }
);

Each function is stored in its own object, but for space sake I just simple passed it directly.

\$\endgroup\$
  • \$\begingroup\$ lol, thanks for the edit. The English syntax still eludes me because I have a habit of wring like I speak. \$\endgroup\$ – googabeast Sep 4 '16 at 0:32
1
\$\begingroup\$

The type function is definitely overengineered in this case because you only need two values. The entire first snippet boils down to a simple function and the logic is instantly observable.

var req = {
    check: function(v){
        if (typeof v === "function") {
            return e();
        }
        if (typeof v === "array" || $.isArray(v)) {
            return v.every(function(e) {
                return typeof e !== "function" || e();
            });
        }
        return true;
    }
}

Note, there's no need for semicolons after closing curly braces of if, for, so if anything it shows you don't understand the automatic semicolon insertion rules.

\$\endgroup\$
  • \$\begingroup\$ Yea, this example I did copy and paste the full function from my solution, but there are other instances where I do use req.type to return all the various types of strings, undefined and ints. Lastly, Its not that I don't understand the semicolon rules, its more of a personal preference. I have developed custom themes and modified language files for Sublime text 3 that allows me to view functions, if, & loops with very unique highlighting. That and because I use Code Kit to compile all of my LESS & JS I normally comment the heck out of my files so my .min output is always sanitized. \$\endgroup\$ – googabeast Sep 4 '16 at 19:32
  • \$\begingroup\$ @googabeast did you just say -but there are other instances where I do use req.type to return all the various types of strings, undefined and ints". The "ints" is very confusing . \$\endgroup\$ – Siobhan Sep 4 '16 at 21:20
  • \$\begingroup\$ return number -aka- integer. Just more human friendly and the "int" was pulled from parseInt(). So I guess ints' would have been better? \$\endgroup\$ – googabeast Sep 4 '16 at 21:34
  • \$\begingroup\$ @wOxxOm - cjihrig.com/blog/… No wonder my professors always told me to terminate my statements, because ASI is for cutting corners. \$\endgroup\$ – googabeast Nov 1 '16 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.