Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check multiple regex on every string in array

I have an array with roles which looks like this :

["ROLE_ADMIN", "ROLE_USER_ADMIN_MARKETING"]

What I need :
I need to check if in this array, a string match EXACTLY. If yes, return true, if no return false.

Warning:
As you can see below, there are roles that start with same text, so it is important to check the exact same string.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Why my console.log returns true here ? Maybe I shouldn’t use switch case ?

export const ROLE_ADMIN = "ROLE_ADMIN";
export const ROLE_USER_ADMIN = "ROLE_USER_ADMIN";
export const ROLE_MARKETING = "ROLE_USER_ADMIN_MARKETING";
export const ROLE_TECHNICAL = "ROLE_USER_ADMIN_TECHNICAL";

const roles: string[] = JSON.parse(localStorage.getItem("roles"));

class UserRole {
    static regexAdmin = new RegExp(`\\b${ROLE_ADMIN}\\b`);
    static regexUserAdmin = new RegExp(`\\b${ROLE_USER_ADMIN}\\b`);
    static regexMarketing = new RegExp(`\\b${ROLE_MARKETING}\\b`);
    static regexTechnical = new RegExp(`\\b${ROLE_TECHNICAL}\\b`);

    public static hasAuthorization(role: string): boolean {
        for (const role of roles) {
            switch (true) {
                case UserRole.regexAdmin.test(role):
                    return true;
                case UserRole.regexUserAdmin.test(role):
                    return true;
                case UserRole.regexMarketing.test(role):
                    return true;
                case UserRole.regexTechnical.test(role):
                    return true;
                default:
                    return false;
            }
        }
    }
}

export const hasAdmin = UserRole.hasAuthorization(ROLE_USER_ADMIN);

console.log(hasAdmin); // THIS RETURNS TRUE

>Solution :

The problems in your code:

for (const role of roles) { shadows the parameter role: string. You’re completely ignoring the argument and only comparing roles from local storage with the hard coded regexes. Your loop always returns in the first iteration.

My solution:

A regex isn’t necessary. It’s a simple

["ROLE_ADMIN", "ROLE_USER_ADMIN_MARKETING"].includes(ROLE_USER_ADMIN)

Example:

let ROLE_USER_ADMIN = "ROLE_ADMIN";

console.log(["ROLE_ADMIN", "ROLE_USER_ADMIN_MARKETING"].includes(ROLE_USER_ADMIN));

ROLE_USER_ADMIN = "ROLE_ADMIN2";

console.log(["ROLE_ADMIN", "ROLE_USER_ADMIN_MARKETING"].includes(ROLE_USER_ADMIN));

ROLE_USER_ADMIN = "ROLE_ADMI";

console.log(["ROLE_ADMIN", "ROLE_USER_ADMIN_MARKETING"].includes(ROLE_USER_ADMIN));

The full code could be

export const ROLE_ADMIN = "ROLE_ADMIN";
export const ROLE_USER_ADMIN = "ROLE_USER_ADMIN";
export const ROLE_MARKETING = "ROLE_USER_ADMIN_MARKETING";
export const ROLE_TECHNICAL = "ROLE_USER_ADMIN_TECHNICAL";

const roles: string[] = JSON.parse(localStorage.getItem("roles"));

class UserRole {
    public static hasAuthorization(role: string): boolean {
        return roles.includes(role);
    }
}

export const hasAdmin = UserRole.hasAuthorization(ROLE_USER_ADMIN);

console.log(hasAdmin); // THIS RETURNS TRUE

Example:

const ROLE_ADMIN = "ROLE_ADMIN";
const ROLE_USER_ADMIN = "ROLE_USER_ADMIN";
const ROLE_MARKETING = "ROLE_USER_ADMIN_MARKETING";
const ROLE_TECHNICAL = "ROLE_USER_ADMIN_TECHNICAL";
    
const roles = ["ROLE_ADMIN", "ROLE_USER_ADMIN_MARKETING"];
    
class UserRole {
    static hasAuthorization(role) {
        return roles.includes(role);
    }
}
    
const hasAdmin = UserRole.hasAuthorization(ROLE_USER_ADMIN);
const hasMarketing = UserRole.hasAuthorization(ROLE_MARKETING);
    
console.log(hasAdmin); // THIS RETURNS FALSE
console.log(hasMarketing); // THIS RETURNS TRUE
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading