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

add helper functions for language switcher #11

@landsman

Description

@landsman

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;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions