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
roleRegis one of the keys of the object; it's not one of the regular expressions your code creates. The variableregcontains the RegExp instance, notroleReg.