Skip to content
This repository has been archived by the owner on Feb 7, 2023. It is now read-only.

add helper functions for language switcher #11

Open
landsman opened this issue Jun 11, 2020 · 0 comments
Open

add helper functions for language switcher #11

landsman opened this issue Jun 11, 2020 · 0 comments
Labels
enhancement New feature or request

Comments

@landsman
Copy link
Member

landsman commented Jun 11, 2020

can be in langUtils


function findVariablesInRoute(route)
{
    let items = route.split('/');
    let dynamic = [];

    items.forEach(item => {
        if(item.includes(":")) {
            dynamic.push(item.replace(':', ''));
        }
    });

    return dynamic;
}

function stripTrailingSlash(str) {
    if(str.substr(-1) === '/') {
        return str.substr(0, str.length - 1);
    }
    return str;
}

function composeAddress(lang, route, query)
{
    const dynamic = findVariablesInRoute(route);
    let parsed = new URLSearchParams(query);
    let url = `/${lang}/`;

    dynamic.forEach(part => {
        if(parsed.has(part)) {
            url+=parsed.get(part);
            url+='/';
        }
    });

    url = stripTrailingSlash(url);

    return url;
}
@landsman landsman added the enhancement New feature or request label Jun 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant