<commit_before>/**
 * Created by xgfd on 10/05/2016.
 */

function normaliseUrl(url) {
    //prepend http:// if abscent
    if (url.indexOf('http') !== 0) {
        url = 'http://' + url;
    }

    //append / if abscent
    if (url[url.length - 1] !== '/') {
        url += '/';
    }
    return url;
}

function origin(url, name, doc) {
    return `${url}${name}/${doc._id}`;
}

function linkToRemote(url, name, local) {
    let remote = DDP.connect(url),
        col = new Mongo.Collection(name, {connection: remote});

    col.find().observe({
        added(doc) {
            doc.origin = origin(url, name, doc);
            //TODO rewrite id refs using url as prefix
            delete doc._id;
            delete doc.isBasedOnUrl;
            delete doc.comments;
            local.insert(doc);
        },
        removed(doc) {
            let origin = origin(url, name, doc);
            local.remove({origin});
        },
        changed(doc, oldDoc) {
            let origin = origin(url, name, oldDoc);
            local.remove({origin});

            doc.origin = origin(url, name, doc);
            delete doc._id;
            delete doc.isBasedOnUrl;
            local.insert(doc);
        }
    });
    return col;
}

function regRemoteColls(remote_name, urls, local) {
    return urls.reduce((map, url) => {
        url = normaliseUrl(url);
        if (!map[url]) {
            map[url] = linkToRemote(url, remote_name, local);
        }
        return map;
    }, {});
}

function updateSub(remoteColls) {
    Object.keys(remoteColls)
        .map(url => remoteColls[normaliseUrl(url)])
        .forEach(col => {
            if (col) {
                let name = col._name,
                    remote = col._connection;
                remote.subscribe(name);
            }
        });
}

let woUrls = orion.config.get('wo_urls');
let remoteColls = {};

if (woUrls) {
    RemoteApps.remove({});
    RemoteDatasets.remove({});

    remoteColls[RemoteDatasets.pluralName] = regRemoteColls('datasets', woUrls, RemoteDatasets);
    remoteColls[RemoteApps.pluralName] = regRemoteColls('apps', woUrls, RemoteApps);
}

pullRemoteColls = function () {
    [RemoteApps, RemoteDatasets]
        .map(coll=>remoteColls[coll.pluralName])
        .forEach(updateSub);
};
<commit_msg>checking undefinded value in updateSub<commit_after>/**
 * Created by xgfd on 10/05/2016.
 */

function normaliseUrl(url) {
    //prepend http:// if abscent
    if (url.indexOf('http') !== 0) {
        url = 'http://' + url;
    }

    //append / if abscent
    if (url[url.length - 1] !== '/') {
        url += '/';
    }
    return url;
}

function origin(url, name, doc) {
    return `${url}${name}/${doc._id}`;
}

function linkToRemote(url, name, local) {
    let remote = DDP.connect(url),
        col = new Mongo.Collection(name, {connection: remote});

    col.find().observe({
        added(doc) {
            doc.origin = origin(url, name, doc);
            //TODO rewrite id refs using url as prefix
            delete doc._id;
            delete doc.isBasedOnUrl;
            delete doc.comments;
            local.insert(doc);
        },
        removed(doc) {
            let origin = origin(url, name, doc);
            local.remove({origin});
        },
        changed(doc, oldDoc) {
            let origin = origin(url, name, oldDoc);
            local.remove({origin});

            doc.origin = origin(url, name, doc);
            delete doc._id;
            delete doc.isBasedOnUrl;
            local.insert(doc);
        }
    });
    return col;
}

function regRemoteColls(remote_name, urls, local) {
    return urls.reduce((map, url) => {
        url = normaliseUrl(url);
        if (!map[url]) {
            map[url] = linkToRemote(url, remote_name, local);
        }
        return map;
    }, {});
}

function updateSub(remoteColls) {
    if (remoteColls) {
        Object.keys(remoteColls)
            .map(url => remoteColls[normaliseUrl(url)])
            .forEach(col => {
                if (col) {
                    let name = col._name,
                        remote = col._connection;
                    remote.subscribe(name);
                }
            });
    }
}

let woUrls = orion.config.get('wo_urls');
let remoteColls = {};

if (woUrls) {
    RemoteApps.remove({});
    RemoteDatasets.remove({});

    remoteColls[RemoteDatasets.pluralName] = regRemoteColls('datasets', woUrls, RemoteDatasets);
    remoteColls[RemoteApps.pluralName] = regRemoteColls('apps', woUrls, RemoteApps);
}

pullRemoteColls = function () {
    [RemoteApps, RemoteDatasets]
        .map(coll=>remoteColls[coll.pluralName])
        .forEach(updateSub);
};

{'fraction_non_alphanumeric': 0.1453064723531933, 'fraction_numerical': 0.0042863266180882984, 'mean_word_length': 1.9707192870782941, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1339299', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1465, 'n_words': 298}