/* leny/anye
 *
 * /src/anye.js - Main entry point
 *
 * coded by leny@flatLand!
 * refactored at 01/04/2019
 */

import QS from "qs";

let oDataStore = {},
    rMatchURLParam = /:[a-z0-9_]+/gi,
    fClear,
    fSet,
    fRaw,
    fGet,
    fGenerate,
    fCount,
    fAll;

fClear = function() {
    oDataStore = {};
};

fSet = function(sName, sURL) {
    oDataStore[sName] = sURL;
    return oDataStore[sName];
};

fRaw = function(sName) {
    if (!oDataStore[sName]) {
        throw new Error(`Unknown URL '${sName}'!`);
    }
    return oDataStore[sName];
};

fGet = function(sName, oParams, bDecode) {
    let sURL;

    if (!(sURL = oDataStore[sName])) {
        throw new Error(`Unknown URL '${sName}'!`);
    }
    return fGenerate(sURL, oParams, bDecode);
};

fGenerate = function(sURL, oParams, bDecode) {
    let aMatches = sURL.match(rMatchURLParam) || [],
        oAdditionalParams = {},
        sQueryString,
        sAssembledURL = sURL;

    for (let sMatch of aMatches) {
        let mValue;

        if (!(mValue = oParams[sMatch.slice(1)])) {
            throw new Error(`Undefined param '${sMatch}'!`);
        }
        sAssembledURL = sAssembledURL.replace(
            sMatch,
            encodeURIComponent(mValue),
        );
    }

    for (let sParam in oParams) {
        if (aMatches.indexOf(`:${sParam}`) === -1) {
            oAdditionalParams[sParam] = oParams[sParam];
        }
    }

    if ((sQueryString = QS.stringify(oAdditionalParams))) {
        sAssembledURL += `?${sQueryString}`;
    }

    return bDecode ? decodeURIComponent(sAssembledURL) : sAssembledURL;
};

fCount = function() {
    return Object.keys(oDataStore).length;
};

fAll = function() {
    return oDataStore;
};

export {
    fClear as clear,
    fGenerate as generate,
    fSet as set,
    fSet as store,
    fGet as get,
    fGet as retrieve,
    fGet as build,
    fRaw as raw,
    fRaw as url,
    fAll as all,
    fCount as count,
};

/* leny/anye
 *
 * /src/anye.js - Main entry point
 *
 * coded by leny@flatLand!
 * refactored at 01/04/2019
 */

import QS from "qs";

let datastore = {};

const matchURLParam = /:[a-z0-9_]+/gi;

const clear = () => {
    datastore = {};
};

const set = (name, url) => {
    datastore[name] = url;
    return datastore[name];
};

const raw = name => {
    if (!datastore[name]) {
        throw new Error(`Unknown URL '${name}'!`);
    }
    return datastore[name];
};

const generate = (url, params = {}, decode = false) => {
    let matches = url.match(matchURLParam) || [],
        additionalParams = {},
        queryString,
        result;

    result = matches.reduce((acc, match) => {
        let value;

        if (!(value = params[match.slice(1)])) {
            throw new Error(`Undefined param '${match}'!`);
        }

        return acc.replace(match, encodeURIComponent(value));
    }, url);

    Object.keys(params).forEach(param => {
        if (!matches.includes(`:${param}`)) {
            additionalParams[param] = params[param];
        }
    });

    if ((queryString = QS.stringify(additionalParams))) {
        result += `?${queryString}`;
    }

    return decode ? decodeURIComponent(result) : result;
};

const get = (name, params, decode = false) => {
    let url;

    if (!(url = datastore[name])) {
        throw new Error(`Unknown URL '${name}'!`);
    }
    return generate(url, params, decode);
};

const count = () => Object.keys(datastore).length;

const all = () => datastore;

export {
    clear,
    generate,
    set,
    set as store,
    get,
    get as retrieve,
    get as build,
    raw,
    raw as url,
    all,
    count,
};

Refactor anye