JSFiddle - React, Tailwind, and code Playground

by FZambia

HTML

Paste current structure into textarea and press "Convert"
<textarea id="structure"></textarea>
<button id="submit" onClick="convert();">Convert</button>
<pre id="result"></pre>

CSS

#structure {
    width: 100%;
    height: 300px;
}

JavaScript

function convert() {
    var currentStructure = JSON.parse(document.getElementById("structure").value);
    var currentProjects = currentStructure["projects"] || [];
    var currentNamespaces = currentStructure["namespaces"] || [];
    var projectKeys = [
        "secret", "name",
        "connection_lifetime", "watch", "publish",
        "anonymous", "history_size", "history_lifetime", 
        "presence", "join_leave", "namespaces"
    ]
    var namespaceKeys = [
        "name", "watch", "publish", "anonymous",
        "history_size", "history_lifetime", "presence",
        "join_leave"
    ]
    var result = [];
    for (var i in currentProjects) {
        var project = currentProjects[i];
        project["namespaces"] = [];
        for (var j in currentNamespaces) {
            var namespace = currentNamespaces[j];
            if (namespace["project_id"] !== project["_id"]) {
                continue;
            }
            namespace["watch"] = namespace["is_watching"];
            if ("history_expire" in namespace) {
                namespace["history_lifetime"] = namespace["history_expire"];
            }
            if (namespace["history"] === false) {
                namespace["history_size"] = 0;
            }
            for (var key in namespace) {
                if (namespaceKeys.indexOf(key) === -1) {
                    delete namespace[key];
                }
            }
            project["namespaces"].push(namespace);
        }
        project["watch"] = project["is_watching"];
        project["secret"] = project["secret_key"];
        if (project["connection_check"] === false) {
            project["connection_lifetime"] = 0;
        }
        for (var key in project) {
            if (projectKeys.indexOf(key) === -1) {
                console.log(key);
                delete project[key];
            }
        }
        result.push(project);
    }
    document.getElementById("structure").value = JSON.stringify(result);
   ...