0

I'm creating some descriptions with Regex using the name of the privileges I have, that are these:

var privileges = [
    "APP_SOMEONE_SRV",
    "APP_ANYONE_SRV",
    "ADM_AD_XX"
];

var descriptions = {
    "NETWORK": {
        "GROUP": {
            "^APP_(.*)$": {
                "^APP_" : "PATH",
                "^APP_A05_(.*)" : "$1",
                "_SRV" : "ON SERVER"
            },
            "^A05_(.*)" : "Group $1 at AD",
            "^ADM_AD_(.*)": "Administrator $1 at AD",       
        }
    }
}

I'm debugging the code with Chrome's Developer Tool, printing some vars with window.console.log:

privileges.forEach(function(role) {
    var base = "NETWORK";
    var type = "GROUP";
    Object.keys(descriptions).forEach(function(baseReg) {
        if (base.match(new RegExp(baseReg))) {
            Object.keys(descriptions[baseReg]).forEach(function(typeReg) {
                if (type.match(new RegExp(typeReg))) {
                    Object.keys(descriptions[baseReg][typeReg]).forEach(function(roleReg) {

                    var reg = RegExp(roleReg);
                    window.console.log("role: " + role);
                    window.console.log("roleReg: " + typeof roleReg + " >> " + roleReg);
                    if (role.match(reg)) {
                        var suggestDescription = role.replace(reg, descriptions[baseReg][typeReg][roleReg]);
                        window.console.log("description: " + suggestDescription);    
                    }
                    if (roleReg == Object.prototype.toString.call(roleReg) === '[object Object]'){
                        window.console.log(roleReg + " is object");
                    }
                    window.console.log("");


                    });
                }
            });
        }
    });
});

On console, I get that "^APP_(.*)$" is always a string, when it should be an object:

VM107:31 role: APP_SOMEONE_SRV
VM107:32 roleReg: string >> ^APP_(.*)$
VM107:35 description: [object Object]
VM107:40 
VM107:31 role: APP_SOMEONE_SRV
VM107:32 roleReg: string >> ^A05_(.*)
VM107:40 
VM107:31 role: APP_SOMEONE_SRV
VM107:32 roleReg: string >> ^ADM_AD_(.*)
VM107:40 
VM107:31 role: APP_ANYONE_SRV
VM107:32 roleReg: string >> ^APP_(.*)$
VM107:35 description: [object Object]
VM107:40 
VM107:31 role: APP_ANYONE_SRV
VM107:32 roleReg: string >> ^A05_(.*)
VM107:40 
VM107:31 role: APP_ANYONE_SRV
VM107:32 roleReg: string >> ^ADM_AD_(.*)
VM107:40 
VM107:31 role: ADM_AD_XX
VM107:32 roleReg: string >> ^APP_(.*)$
VM107:40 
VM107:31 role: ADM_AD_XX
VM107:32 roleReg: string >> ^A05_(.*)
VM107:40 
VM107:31 role: ADM_AD_XX
VM107:32 roleReg: string >> ^ADM_AD_(.*)
VM107:35 description: Administrator XX at AD

I already tried to change the comparison to check that if the actual roleReg is a object, but it didn't change anything. Does it have other way to iterate over the descriptions, checking before that if the actual one is a object? Because, for example, "^A05_(.*)" is not a object

3
  • 1
    Why would you expect it to be an object? In your code, roleReg is one of the keys of the object; it's not one of the regular expressions your code creates. The variable reg contains the RegExp instance, not roleReg. Commented Jan 6, 2017 at 14:40
  • @Pointy i saw here > developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/… that it probably be an object, at myHonda var Commented Jan 6, 2017 at 14:43
  • "myHonda"?? What? Object property keys are always strings. (Well, they can be Symbol instances in ES2015 but that's not relevant here.) Commented Jan 6, 2017 at 14:48

1 Answer 1

1

roleReg is the property value (string), which is why. reg is the object you are making with that value, and that is an object. ;)

To clarify Object.keys(descriptions[baseReg][typeReg]) is iterating over descriptions['NETWORK']['GROUP'] and that will give you the property NAMEs (strings), not the value of the property. var reg = RegExp(roleReg); creates a regex with the property NAME, which is ^APP_(.*)$.

descriptions[baseReg][typeReg][roleReg] will give the you property object you are looking for.

Try this:

privileges.forEach(function(role) {
    var base = "NETWORK";
    var type = "GROUP";
    Object.keys(descriptions).forEach(function(baseReg) {
        if (base.match(new RegExp(baseReg))) {
            Object.keys(descriptions[baseReg]).forEach(function(typeReg) {
                if (type.match(new RegExp(typeReg))) {
                    Object.keys(descriptions[baseReg][typeReg]).forEach(function(roleReg) {
                    var reg = RegExp(roleReg);
                    var roleRegValue = descriptions[baseReg][typeReg][roleReg];
                    window.console.log("role: " + role);
                    window.console.log("roleReg Property: " + typeof roleReg + " >> " + roleReg);
                    window.console.log("roleReg Value: " + typeof roleRegValue + " >> " + roleRegValue);
                    if (role.match(reg)) {
                        var suggestDescription = role.replace(reg, descriptions[baseReg][typeReg][roleReg]);
                        window.console.log("description: " + suggestDescription);    
                    }
                    if (roleRegValue == Object.prototype.toString.call(roleRegValue) === '[object Object]'){
                        window.console.log(roleRegValue + " is object");
                    }
                    window.console.log("");


                    });
                }
            });
        }
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

i saw right now that you have to change (roleRegValue == Object.prototype.toString.call(roleRegValue) === '[object Object]') to (Object.prototype.toString.call(roleRegValue) === '[object Object]') to go inside this if
I didn't really analyze the rest of the program, as I wasn't sure what you were trying to accomplish, but I figured it was enough to get you going. Glad it helped point you in the right direction. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.